Building Hello World Service Manually

Last updated: 2020-02-24 17:43:31

PDF

Operation Scenarios

This document shows how to quickly create a Node.js Hello World Service in a container cluster. For more information on how to build a Docker image, please see How to Build Docker Image .

Prerequisites

  • Create a cluster. For more information, see Creating a cluster .
  • Log in to node. This node has Node.js installed.

Steps

Writing Codes to Create an Image

Write application

  1. Execute the following commands in sequence to create and enter hellonode folder.
mkdir hellonode
cd hellonode/
  1. Execute the following commands to create and open server.js file.
vim server.js
  1. Press I Or INSERT To switch to editing mode, and enter the following contents into server.js.
var http = require('http');
var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(80);

Press Esc And enter : wq To save the file and return.
4. Execute the following commands to execute the server.js file.

node server.js

You can test the Hello World program in the following two ways.

  • Log in to the node again and execute the following command.
curl 127.0.0.1:80

The following display indicates that the Hello World program is running successfully.

  • Open the browser and access it by entering IP address:port. The port is 80.
    The following display indicates that the Hello World program is running successfully.

Creating Docker Image

For more information on docker images, see How to Build a Docker Image .

  1. Execute the following commands in sequence to create the Dockerfile file under the hellonode folder.
cd /hellonode
vim Dockerfile
  1. Press I Or INSERT To switch to editing mode, and enter the following contents into the Dockerfile file.
FROM node:4.4
EXPOSE 80
COPY server.js .
CMD node server.js

Press Esc And enter : wq To save the file and return.
3. Execute the following commands to build the image.

docker build -t hello-node:v1 .
  1. Execute the following command to view the hello-node image that was built.
docker images 

The following display indicates that the hellonode image was successfully built. Note down its IMAGE ID. See the figure below:

Uploading Image to Tencent Cloud Image Registry

The following conditions must be met for uploading Image:

Execute the following commands in sequence to upload the image to the Tencent Cloud registry.

sudo docker tag IMAGEID ccr.ccs.tencentyun.com/namespace/helloworld:v1
sudo docker push ccr.ccs.tencentyun.com/namespace/helloworld:v1
  • Replace IMAGEID in the command with the IMAGEID noted down in Step 4 above .
  • Replace the namespace in the command with the namespace that you created.

The following display indicates that the image was successfully uploaded.

Creating Hello World Service Using This Image

You must already have the following before creating and using the Hello World service:

  • A registered Tencent Cloud account. Go to the Registration Page And fill out the related information to register a Tencent Cloud account.
  • Created a cluster. For more information, see Creating a cluster .
  1. Log in to the TKE console and select Clusters In the left sidebar.
  2. On the "Cluster Management" page, select the cluster ID, for which you want to create the service to enter the "Deployment" page of the cluster workload and click "Create". as shown below:
  3. Set up the workload basic information on the Create Workload Page according to the following instructions. See the figure below:
  • Workload Name : Enter the name of the workload to be created. Here, helloworld is used as an example.
  • DESCRIPTION : Fill in the related workload information.
  • tag A "key = value" pair. In this example, the tag is "k8s-app = helloworld".
  • Namespace : To be selected based on your requirements.
  • Type : To be selected based on your requirements.
  • Volume : Set up the workload volumes mounted based on your requirements. For more details, see Volume Management .
  1. Refer to the following information to set the "container in the instance". as shown below:
    Enter the name of the container in the instance. This article takes helloworld as an example.

    select My Images From the pop-up box and find the helloworld image by using the search box feature. Click OK . See the figure below:

    Main parameters include:
  • Image Tag Use the default value v1.
  • Image's pull strategy The following three policies are provided. Please select them as needed. This article takes using the default policy without setting as an example.
    If Image's pull strategy is not set, when Image Tag is empty or latest The Always policy is used, otherwise the IfNotPresent policy is used.
    • ALWAYS Always pull the Image remotely.
    • IfNotPresent Local Image is used by default. If there is no local Image, the Image will be pulled remotely.
    • NEVER Only use local Image. If you don't have the local Image, you will report an exception.
  1. In the number of instances, set the number of instances of the service based on the following information. as shown below:
  • Manual adjustment : Set the number of pods. The number of pods in this example is set as 1. You can click + Or - To change the number of pods.
  • Auto Adjustment : Automatically adjust the number of pods if any of the setting conditions are met. For more details, see Service Auto Scaling .
  1. Set up the workload access according to the following instructions. See the figure below:
  • Service : Check Enable .
  • Service Access : Select Via Internet .
  • Load Balancer : Select according to your requirements.
  • Port Mapping : Select TCP protocol, and set both the container port and service port to 80.

The security group of the service's cluster must open the node network and container network to the Internet. It is also required to open ports 30000 to 32768 to the Internet. Otherwise, the problem of TKE being unusable could occur. For more details, see TKE Security Group Settings .

  1. Click Create Workload To complete the creation of the HelloWorld service.

Accessing HelloWorld Service

HelloWorld service can be accessed using the following two methods.

Accessing via Load Balancer IP

  1. In the left sidebar, click Clusters To go to the Cluster Management Page。
  2. Click on the HelloWorld service's cluster ID and select Service > Service .
  3. Copy the Nginx service's cloud load balancer IP from the service management page. See the figure below:

Access Using Service Name

Other services or containers in the cluster can be accessed directly by the service name.

Verifying Hello World Service

When accessing the service, the following display indicates that the Hello World service was successfully created.

If creation of the container failed, you can read the Event FAQs .