部署基于 Docker 的服务

部署基于 Docker 的服务

在本教程中,您将创建自定义 Docker 镜像,并将其部署到 DC/OS。

先决条件

创建自定义 Docker 镜像

  1. 创建名为 index.html 的文件。将以下标记粘贴到 index.html 并保存:

    1. <html>
    2. <body>
    3. <h1> Hello brave new world! </h1>
    4. </body>
    5. </html>
  2. 创建名为 Dockerfile 的文件。将以下 Docker 命令粘贴到其中,并保存:

    1. FROM nginx:1.9
    2. COPY index.html /usr/share/nginx/html/index.html
  3. 构建 Docker 镜像,其中 是您的 Docker Hub 用户名:

    1. docker build -t <username>/simple-docker .

    输出应类似于:

    1. Sending build context to Docker daemon 3.072 kB
    2. Step 1 : FROM nginx:1.9
    3. 1.9: Pulling from library/nginx
    4. 51f5c6a04d83: Pull complete
    5. a3ed95caeb02: Pull complete
    6. 640c8f3d0eb2: Pull complete
    7. a4335300aa89: Pull complete
    8. Digest: sha256:54313b5c376892d55205f13d620bc3dcccc8e70e596d083953f95e94f071f6db
    9. Status: Downloaded newer image for nginx:1.9
    10. ---> c8c29d842c09
    11. Step 2 : COPY index.html /usr/share/nginx/html/index.html
    12. ---> 61373621782c
    13. Removing intermediate container 225910aa385d
    14. Successfully built 61373621782c
  4. 登录到 Docker Hub:

    1. docker login
  5. 将图像推送到 Docker Hub,其中 <username> 是您的 Docker Hub 用户名:

    1. docker push <username>/simple-docker

    输出应类似于:

    1. The push refers to a repository [docker.io/<username>/simple-docker]
    2. 6e2a0db36f4c: Pushed
    3. 5f70bf18a086: Mounted from library/nginx
    4. 49027b789c92: Mounted from library/nginx
    5. 20f8e7504ae5: Mounted from library/nginx
    6. 4dcab49015d4: Mounted from library/nginx
    7. latest: digest: sha256:f733e23e1f5e83a29a223d0a7d30244b30c0d57d17aa0421d962019545d69c17 size: 2185

创建 Docker 应用程序并部署到 DC/OS

  1. 使用以下内容创建 Marathon 应用定义,并另存为 hello-nginx.json:在 image 字段,替换 <username>在 使用Docker Hub用户名。 在里面 type 领域, 指定 字段中,按您需要的 containerizer runtime 指定 MESOS 要么 或 DOCKER。

    1. {
    2. "id": "hello-nginx",
    3. "container": {
    4. "type": "[MESOS | DOCKER]",
    5. "docker": {
    6. "image": "<username>/simple-docker",
    7. "parameters": [
    8. {
    9. "key": "log-driver",
    10. "value": "none"
    11. }
    12. ]
    13. },
    14. "portMappings": [
    15. { "hostPort": 80, "containerPort": 80, "protocol": "tcp" }
    16. ]
    17. },
    18. "networks": [
    19. {
    20. "mode": "container/bridge"
    21. }
    22. ],
    23. "acceptedResourceRoles": ["slave_public"],
    24. "instances": 1,
    25. "cpus": 0.1,
    26. "mem": 64
    27. }

    此文件指定了一个名为 hello-nginx 的简单 Marathon 应用程序,该应用程序在公共节点上运行自身的一个实例。

  2. 使用 DC/OS 命令将 hello-nginx 应用程序添加到 Marathon:

    1. dcos marathon app add hello-nginx.json

    如果添加成功,则没有输出。

  3. 如果您选择了 MESOS 运行时间,在您确认添加了该应用程序时,您将看到以下内容:

    1. dcos marathon app list
    2. ID MEM CPUS TASKS HEALTH DEPLOYMENT WAITING CONTAINER CMD
    3. /hello-nginx 64 0.1 1/1 N/A --- False MESOS N/A
  4. 如果您使用 AWS CloudFormation 模板 将应用程序公开到应用定义中指定的端口(例如,端口 80),则必须在公共 ELB 上重新设置运行状况检查。

    1. 在 CloudFormation 中,勾选堆栈旁边的复选框。
    2. 单击 Resources 选项卡。
    3. 搜索 PublicSlaveLoadBalancer
    4. 单击 Physical ID(物理 ID)列中的链接。
    5. 遵循更新运行状况检查配置中的说明。
  5. 转到公共代理节点,查看网站是否正在运行。若要查找公共代理 IP 地址,请参阅查找公共代理 IP

    您应在浏览器中看到以下消息:

    Hello Brave World

    图 1. Hello World 消息

后续步骤

了解如何使用 Marathon-LB 在公共节点上对应用程序进行负载均衡。