zoukankan      html  css  js  c++  java
  • 容器基础(八): 使用docker swarm部署程序

    环境

    基于上一节的env/server:v0.1, env/worker:v0.1镜像, 在基于debian8.2的两台机器上测试部署docker swarm.

    docker service部署

    ➜  ~  docker service create --replicas 1 --name server -e APP_PORT=5000 --network docker-net --publish 5000:5000 env/server:v0.1
    image env/server:v0.1 could not be accessed on a registry to record
    its digest. Each node will access env/server:v0.1 independently,
    possibly leading to different nodes running different
    versions of the image.
    
    pzyx887jb112isgkyorarvt7t
    overall progress: 1 out of 1 tasks 
    1/1: running   [==================================================>] 
    verify: Service converged 
    ➜  ~~  docker service create --replicas 2 --name worker -e APP_DOMAIN=server -e APP_PORT=5000 --network docker-net env/worker:v0.1
    image env/worker:v0.1 could not be accessed on a registry to record
    its digest. Each node will access env/worker:v0.1 independently,
    possibly leading to different nodes running different
    versions of the image.
    
    wavv2ie7ad9je6s33q82f3skv
    overall progress: 2 out of 2 tasks 
    1/2: running   [==================================================>] 
    2/2: running   [==================================================>] 
    verify: Service converged 
    ➜  ~  docker service ls
    ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
    pzyx887jb112        server              replicated          1/1                 env/server:v0.1     *:5000->5000/tcp
    wavv2ie7ad9j        worker              replicated          2/2                 env/worker:v0.1~  docker service ps worker
    ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
    hyjcg25ymfvx        worker.1            env/worker:v0.1     debian-vm           Running             Running 42 seconds ago                       
    zpx2oq4q0dhu        worker.2            env/worker:v0.1     debian              Running             Running 36 seconds ago                        
    
     1 ➜  ~  docker ps 
     2 CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
     3 a9b1931acf12        env/worker:v0.1     "python worker.py"   2 minutes ago       Up 2 minutes                            worker.2.zpx2oq4q0dhuqgvtkt0mm8dyt
     4 994e4779c252        env/server:v0.1     "python server.py"   3 minutes ago       Up 3 minutes                            server.1.v5mu7wa8tfswayzwjh3u1qtpk
     5 ➜  ~  docker exec 994e4779c252 cat /env/server/log/server.log
     6 2018-11-21 23:02:33 [INFO]  Waiting for connection...
     7 2018-11-21 23:03:26 [INFO]  ('10.0.0.5', 37802) connected
     8 2018-11-21 23:03:27 [INFO]  ('10.0.0.4', 54099) connected
     9 ➜  ~  docker exec a9b1931acf12 cat /env/worker/log/worker.log
    10 2018-11-21 23:03:27 [INFO]  [+] ('10.0.0.2', 5000) connected
    11 2018-11-21 23:03:27 [INFO]  [recv] hello, docker!
    12 ➜  ~  docker exec a9b1931acf12 ping -c 2 server              
    13 PING server (10.0.0.2) 56(84) bytes of data.
    14 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.117 ms
    15 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.205 ms
    16 
    17 --- server ping statistics ---
    18 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
    19 rtt min/avg/max/mdev = 0.117/0.161/0.205/0.044 ms
    manager node上测试服务可用和可发现
     1 root@debian-vm:~# docker ps
     2 CONTAINER ID        IMAGE               COMMAND              CREATED              STATUS              PORTS               NAMES
     3 94b6e94a4f4c        env/worker:v0.1     "python worker.py"   About a minute ago   Up 58 seconds                           worker.1.hyjcg25ymfvx2u6ghy3ng7g1y
     4 root@debian-vm:~# docker exec 94b6e94a4f4c cat /env/worker/log/worker.log
     5 2018-11-21 23:03:21 [INFO]  [+] ('10.0.0.2', 5000) connected
     6 2018-11-21 23:03:21 [INFO]  [recv] hello, docker!
     7 root@debian-vm:~# docker exec 94b6e94a4f4c ping -c 2 server
     8 PING server (10.0.0.2) 56(84) bytes of data.
     9 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.058 ms
    10 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.057 ms
    11 
    12 --- server ping statistics ---
    13 2 packets transmitted, 2 received, 0% packet loss, time 1002ms
    14 rtt min/avg/max/mdev = 0.057/0.057/0.058/0.007 ms
    15 root@debian-vm:~# 
    worker node上测试服务可用和可发现
    # 从worker node上验证publish端口
    root@debian-vm:/env/worker# export APP_PORT=5000
    root@debian-vm:/env/worker# export APP_DOMAIN=192.168.2.118
    root@debian-vm:/env/worker# python worker.py 
    [+] ('192.168.2.118', 5000) connected
    [+][recv] hello, docker!
    ^C
    [-] signal(2) received, exit!
    root@debian-vm:/env/worker#
    
    # 从manager node上验证publish端口
    ➜  worker  export APP_PORT=5000
    ➜  worker  export APP_DOMAIN=192.168.2.118
    ➜  worker  python worker.py 
    [+] ('192.168.2.118', 5000) connected
    [+][recv] hello, docker!
    ^C
    [-] signal(2) received, exit!
    ➜  worker  docker exec 994e4779c252 cat /env/server/log/server.log
    2018-11-21 23:02:33 [INFO]  Waiting for connection...
    2018-11-21 23:03:26 [INFO]  ('10.0.0.5', 37802) connected
    2018-11-21 23:03:27 [INFO]  ('10.0.0.4', 54099) connected
    2018-11-21 23:11:42 [INFO]  ('10.255.0.2', 41683) connected
    2018-11-21 23:11:43 [INFO]  ('10.255.0.2', 41683) closed
    2018-11-21 23:15:27 [INFO]  ('10.255.0.2', 56739) connected
    2018-11-21 23:15:28 [INFO]  ('10.255.0.2', 56739) closed
    ➜  worker
    验证服务的publish端口

    docker stack部署

    #docker-compose.yml
    
    version: "3"
    services:
      server:
        image: env/server:v0.1
        build:
          context: /app/env/server
          dockerfile: Dockerfile
        labels:
          description: "tcp server test script"
        ports:
          - 3000:${APP_PORT-3000}
          - ${APP_PORT-3000}:${APP_PORT-3000}
        restart: always
        environment:
          - APP_PORT=${APP_PORT-3000}
        networks:
          - overlay
        deploy:
          replicas: 1
    
      worker:
        image: env/worker:v0.1
        build: /app/env/worker
        labels:
          description: "tcp client test script"
        links:
          - server
        environment:
          - APP_PORT=${APP_PORT-3000}
          - APP_DOMAIN=server
        networks:
          - overlay
        depends_on:
          - server
        deploy:
          replicas: 3
          restart_policy:
            condition: on-failure
    
      worker_test:
        image: env/worker_test:v0.1
        networks:
          - overlay
        deploy:
          replicas: 3
    
    networks:
      overlay:
    ➜  /env  echo $APP_PORT
    5000/env  docker stack deploy -c docker-compose.yml  env
    Ignoring unsupported options: build, links, restart
    
    Creating network env_overlay
    Creating service env_worker_test
    Creating service env_server
    Creating service env_worker
    ➜  /env  docker service ls
    ID                  NAME                MODE                REPLICAS            IMAGE                  PORTS
    lwl5gpaseyz5        env_server          replicated          1/1                 env/server:v0.1        *:3000->5000/tcp, *:5000->5000/tcp
    0ywtuy79e5af        env_worker          replicated          3/3                 env/worker:v0.1        
    a2malzyjs8a8        env_worker_test     replicated          3/3                 env/worker_test:v0.1/env  docker ps
    CONTAINER ID        IMAGE                  COMMAND              CREATED              STATUS              PORTS               NAMES
    6534e1ab7a0f        env/worker:v0.1        "python worker.py"   About a minute ago   Up About a minute                       env_worker.2.o8oo0c2do3xi17bb2y1jyxyvt
    a6f05a1180fa        env/worker_test:v0.1   "python worker.py"   About a minute ago   Up About a minute                       env_worker_test.2.xyriek6uv0jt5u8grfh2r2nip
    dd178c531cab        env/worker_test:v0.1   "python worker.py"   About a minute ago   Up About a minute                       env_worker_test.3.e5akyik75zru3a3vm7t0uow42
    ➜  /env  docker exec 6534e1ab7a0f cat /env/worker/log/worker.log
    2018-11-21 23:41:08 [INFO]  [+] ('10.0.3.8', 5000) connected
    2018-11-21 23:41:08 [INFO]  [recv] hello, docker!
     1 root@debian-vm:/# docker ps
     2 CONTAINER ID        IMAGE                  COMMAND              CREATED             STATUS              PORTS               NAMES
     3 54816f125b5d        env/worker:v0.1        "python worker.py"   2 minutes ago       Up 2 minutes                            env_worker.3.583qka4f0we1tr8gzfbfwd8xq
     4 b148b68c60ac        env/worker:v0.1        "python worker.py"   2 minutes ago       Up 2 minutes                            env_worker.1.pajhjlgfcfpkcvtgbl4pd9iec
     5 4501dddabc12        env/server:v0.1        "python server.py"   2 minutes ago       Up 2 minutes                            env_server.1.r8rffhrzds0p5qreiz3zskglt
     6 423f9f7fe5bf        env/worker_test:v0.1   "python worker.py"   2 minutes ago       Up 2 minutes                            env_worker_test.1.yafiu0eg5bmmv47do4dwtbcdm
     7 root@debian-vm:/# docker exec 4501dddabc12 cat /env/server/log/server.log
     8 2018-11-21 23:40:58 [INFO]  Waiting for connection...
     9 2018-11-21 23:41:02 [INFO]  ('10.0.3.3', 34822) connected
    10 2018-11-21 23:41:02 [INFO]  ('10.0.3.3', 46612) connected
    11 2018-11-21 23:41:03 [INFO]  ('10.0.3.2', 33741) connected
    12 root@debian-vm:/# 
    manager node验证连接建立成功
    # worker node启动客户端
    ➜  worker  export APP_PORT=3000
    ➜  worker  export APP_DOMAIN=192.168.2.118
    ➜  worker  python worker.py 
    [+] ('192.168.2.118', 3000) connected
    [+][recv] hello, docker!
    ^C
    [-] signal(2) received, exit!
    ➜  worker 
    
    
    # manager node查看日志, 验证连接成功
    root@debian-vm:/# docker exec 4501dddabc12 cat /env/server/log/server.log
    2018-11-21 23:40:58 [INFO]  Waiting for connection...
    2018-11-21 23:41:02 [INFO]  ('10.0.3.3', 34822) connected
    2018-11-21 23:41:02 [INFO]  ('10.0.3.3', 46612) connected
    2018-11-21 23:41:03 [INFO]  ('10.0.3.2', 33741) connected
    2018-11-21 23:46:29 [INFO]  ('10.255.0.2', 37693) connected
    2018-11-21 23:46:30 [INFO]  ('10.255.0.2', 37693) closed
    root@debian-vm:/# 
    验证服务的publish端口

    Excellence, is not an act, but a habit.
    作者:子厚.
    出处:http://www.cnblogs.com/aios/
    本文版权归作者和博客园共有,欢迎转载、交流、点赞、评论,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

  • 相关阅读:
    python基础练习题(输入三个整数x,y,z,请把这三个数由小到大输出)
    python基础练习题(一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?)
    python基础练习题(九九乘法表)
    四步测试设计法—摘自《测试架构师修炼之道》
    砍价活动风控的跟踪记录
    C#窗口程序CPU占用高的原因和解决方法(转)
    C# 开启一个新线程
    C# 在关闭窗口程序时执行一些操作
    关于在Linux下执行程序时,需要配置路径的/etc/ld.so.conf详解【转】
    QT新建窗体程序,出现错误 unkown type name ‘QApplication’ 和 unkown type name ‘MainWindow’解决方法
  • 原文地址:https://www.cnblogs.com/aios/p/9998913.html
Copyright © 2011-2022 走看看