编写HelloWorld程序

最近更新时间: 2024-06-12 15:06:00

本文档旨在帮助大家了解如何快速创建一个容器集群内的 Hello World 的 Node.js 版的服务。

第一步:编写代码制作镜像

编写应用程序

  1. 创建一个 hellonode 的文件夹,加入 server.js 文件。

    [root@VM_88_88_centos ~]# mkdir hellonode  
    [root@VM_88_88_centos ~]# cd hellonode/  
    [root@VM_88_88_centos hellonode]# vim server.js  
    [root@VM_88_88_centos hellonode]# ls  
    server.js  

    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(8080);  
  2. 测试 Hello World 程序。

    [root@VM_88_88_centos ~]# node server.js  

    打开新终端使用 curl 测试应用程序,或在浏览器以 IP 地址:端口的形式访问,端口为 8080 。

    [root@VM_88_88_centos ~]# curl 127.0.0.1:8080  
    Hello World!  

创建 Docker 镜像

构建 Docker 镜像更多详情见:如何构建 Docker 镜像。

  1. 在 hellonode 文件夹下,创建 Dockerfile 文件:

    FROM node:4.4  
    EXPOSE 8080  
    COPY server.js .  
    CMD node server.js  
  2. 通过 Docker build 命令构建镜像。

    [root@VM_88_88_centos hellonode]# vim Dockerfile   
    [root@VM_88_88_centos hellonode]# ls  
    Dockerfile  server.js  
    [root@VM_88_88_centos hellonode]# docker build -t hello-node:v1 .  
    Sending build context to Docker daemon 3.072 kB  
    Step 1 : FROM node:4.4  
    Trying to pull repository docker.io/library/node ...  
    4.4: Pulling from docker.io/library/node  
    ......  
    ......  
    Removing intermediate container 1e8d01dc319f  
    Successfully built 027232e62e3f  
    [root@VM_88_88_centos hellonode]# docker images  
    REPOSITORY                                     TAG                 IMAGE ID            CREATED             SIZE
    hello-node                                     v1                  027232e62e3f        54 minutes ago      647.4 MB  

上传该镜像到 qcloud 镜像仓库

更多镜像操作详情见:镜像仓库基本教程。

[root@VM_3_224_centos hellonode]# sudo docker tag 027232e62e3f ccr.fincloud.tencent.cn/test/helloworld:v1
[root@VM_3_224_centos hellonode]# sudo docker push ccr.fincloud.tencent.cn/test/helloworld:v1  
The push refers to a repository [ccr.fincloud.tencent.cn/test/helloworld]  
1b8da8805305: Pushed   
20a6f9d228c0: Pushed  
80c332ac5101: Pushed  
04dc8c446a38: Pushed  
1050aff7cfff: Pushed  
66d8e5ee400c: Pushed  
2f71b45e4e25: Pushed  
v1: digest: sha256:38b194feeee09abf8ee45e7abca82b9fe494b18b953c771ce8ebefa387107be9 size: 1772  

第二步:通过该镜像创建 Hello World 服务

注意: 在创建 helloworld 服务之前,您必须拥有: 一个创建好的集群。有关如何创建集群的详细信息,参见部署容器服务TKE 中步骤1创建集群部分。

创建 helloworld 服务

  1. 登录 TKE 控制台。

  2. 单击左侧导航栏中的【集群】,单击集群列表页的一个集群【ID/名称】。

  3. 在集群页选择【工作负载】 > 【Deployment】,在“Deployment”列表里面单击【新建】。

  4. 在【新建Workload】页面输入以下参数。

    • 工作负载名:输入自定义名称,这里以helloworld为例

    • 实例内容器

    • 名称:自定义,这里以my-container为例

    • 镜像:根据实际需求进行选择,这里以helloworld为例

    • 访问设置(Service):勾选【启用】Service按钮,会新建一个与负载同名的Service

    • 服务访问方式:为方便测试,这里选择 提供公网访问

    • 端口映射:容器端口和服务端口都填80

  5. 单击创建Workload

访问 helloworld 服务

提供两种方式访问 helloworld 服务。

  • 通过负载均衡 IP 访问 Hello World 服务

    1. 在集群页面选择【服务】 > 【Service】,在“Service”列表里单击刚刚新建的名为 helloworld 的服务,进入helloworld服务的详情页。

    2. 在浏览器输入上图中的负载均衡IP:服务端口来访问 nginx 服务,

  • 通过服务名称访问 Hello World 服务

    集群内的其他服务或容器可以直接通过服务名称访问。

    进入 Hello World 服务器的默认欢迎页。