Building Hello World Service Manually

Last updated: 2019-10-18 15:59:57

PDF

Operation Scenario

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/
  2. Execute the following commands to create and open server.js file.
    vim server.js
  3. 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 commands.
      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
  2. 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 .
  4. 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

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.
  2. In the left sidebar, click Clusters to go to the Cluster Management page.
  3. Click on the cluster ID for which the service is to be created, and go to the workload deployment details page. Select Create. See the figure below:
  4. 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: Select a namespace based on your requirements.
    • Type: Select a type based on your requirements.
    • Volume: Set up the workload volumes to mount based on your requirements. For more details, see Volume Management.
  5. Set up Containers in the pod. Enter the pod container name. In this example, helloworld is used.
  6. Click Select Image (keep other settings as default). See the figure below:

    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:
  7. Set the number of pods. See the figure 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 Auto Scaling.
  8. Set up the workload access settings 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.

      You need to open this node and container network, Port 30000-32768 to Internet. Otherwise the container service will not be available. For more details, see TKE Security Group Settings.

  9. 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.