Docker Swarm

一. 架构

Dokcer swarm主要有两部分组成:一个奇数的manger 节点 的a raft consensus group,和一组工作结点,工作结点之间使用的是流言网络协议 ,也叫做控制层面。下图对架构的阐述:

每个管理节点在它本地的raft存储都有swarm状态的所有信息,它们的通信方式使用的是同步的一种方式。

工作节点之间由于扩容的原因,使用是异步通信方式



二. 创建 Docker Swarm

1. swarm的初始化

xiodi@c720131:~$ sudo docker swarm init
Swarm initialized: current node (6h6a4hf307jil12pa891siaib) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-08odkkfd4mui46wvxlehv0y5klis7z30q28mrp7pnw89y4a11y-0rvkmr0tikgulqjgl8h2g6xtw 192.168.20.131:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.


2. 列出swarm的节点

xiodi@c720131:~$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
6h6a4hf307jil12pa891siaib *   c720131.xiodi.cn    Ready               Active              Leader              18.05.0-ce


3. 检查节点的详细信息

xiodi@c720131:~$ sudo docker node inspect 6h6a4hf307jil12pa891siaib
[
     {
         "ID": "6h6a4hf307jil12pa891siaib",
         "Version": {
             "Index": 9
         },
         "CreatedAt": "2018-06-06T07:09:41.581326206Z",
         "UpdatedAt": "2018-06-06T07:09:42.09767829Z",
         "Spec": {
             "Labels": {},
             "Role": "manager",
             "Availability": "active"
         },
         "Description": {
             "Hostname": "c720131.xiodi.cn",
             "Platform": {
                 "Architecture": "x86_64",
                 "OS": "linux"
             },
             "Resources": {
                 "NanoCPUs": 2000000000,
                 "MemoryBytes": 4143411200
             },
             "Engine": {
                 "EngineVersion": "18.05.0-ce",
                 "Plugins": [
                     {
                         "Type": "Log",
                         "Name": "awslogs"
                     },
                     {
                         "Type": "Log",
                         "Name": "fluentd"
                     },
                     {
                         "Type": "Log",
                         "Name": "gcplogs"
                     },
                     {
                         "Type": "Log",
                         "Name": "gelf"
                     },
                     {
                         "Type": "Log",
                         "Name": "journald"
                     },
                     {
                         "Type": "Log",
                         "Name": "json-file"
                     },
                     {
                         "Type": "Log",
                         "Name": "logentries"
                     },
                     {
                         "Type": "Log",
                         "Name": "splunk"
                     },
                     {
                         "Type": "Log",
                         "Name": "syslog"
                     },
                     {
                         "Type": "Network",
                         "Name": "bridge"
                     },
                     {
                         "Type": "Network",
                         "Name": "host"
                     },
                     {
                         "Type": "Network",
                         "Name": "macvlan"
                     },
                     {
                         "Type": "Network",
                         "Name": "null"
                     },
                     {
                         "Type": "Network",
                         "Name": "overlay"
                     },
                     {
                         "Type": "Volume",
                         "Name": "local"
                     }
                 ]
             },
             "TLSInfo": {
                 "TrustRoot": "-----BEGIN CERTIFICATE-----\nMIIBajCCARCgAwIBAgIUWWC4eyklvnLS5Jg/voHIkWorxCIwCgYIKoZIzj0EAwIw\nEzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTgwNjA2MDcwNTAwWhcNMzgwNjAxMDcw\nNTAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH\nA0IABO8UDtnIWogbHOVgdV2Gq7dqlrmGxiS9t2S53bciCJEcSZEARRdq2qEfGI3N\nDFksU70gOhMPTzYwyyqWUGlnZzCjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB\nAf8EBTADAQH/MB0GA1UdDgQWBBQqIjWQ8OWpVx6ZKXrEyaV4OuaoQTAKBggqhkjO\nPQQDAgNIADBFAiA2OiJMd2OwuXFH4uQixic7TvCLRaemuLnkR4MXKdhJKAIhAP+Y\nf6FL0wZw/NVDRw3Z6lNDl9Wayv5lHwC6V36C9DpP\n-----END CERTIFICATE-----\n",
                 "CertIssuerSubject": "MBMxETAPBgNVBAMTCHN3YXJtLWNh",
                 "CertIssuerPublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7xQO2chaiBsc5WB1XYart2qWuYbGJL23ZLndtyIIkRxJkQBFF2raoR8Yjc0MWSxTvSA6Ew9PNjDLKpZQaWdnMA=="
             }
         },
         "Status": {
             "State": "ready",
             "Addr": "192.168.20.131"
         },
         "ManagerStatus": {
             "Leader": true,
             "Reachability": "reachable",
             "Addr": "192.168.20.131:2377"
         }
     }
]



三. 使用swarm创建单个服务

(1)创建一个stack文件

version: "3.5"
services:
   whoami:
     image: training/whoami:latest
     networks:
       - test-net
     ports:
       - 81:8000
     deploy:
       replicas: 6
       update_config:
         parallelism: 2
         delay: 10s
       labels:
         app: sample-app
         environment: prod-south

networks:
   test-net:
     driver: overlay


2. 进行部署stack

xiodi@c720131:~/docker$ sudo docker stack deploy -c stack.yaml sample-stack
[sudo] password for xiodi:
Creating network sample-stack_test-net
Creating service sample-stack_whoami


3. 查看部署的stack

xiodi@c720131:~/docker$ sudo docker stack ls
NAME                SERVICES
sample-stack        1


4. 查看部署的服务

xiodi@c720131:~/docker$ sudo docker service ls
ID                  NAME                  MODE                REPLICAS            IMAGE                    PORTS
t98tjpn7xpby        sample-stack_whoami   replicated          6/6                 training/whoami:latest   *:81->8000/tcp


5. 检查服务和它的任务

xiodi@c720131:~/docker$ sudo docker service ps sample-stack_whoami
ID                                     NAME                    IMAGE                           NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
ib67sdpbu7xc          sample-stack_whoami.1   training/whoami:latest   ubuntu                  Running                  Running 3 minutes ago                      
3qhfu2lj4qis            sample-stack_whoami.2   training/whoami:latest   c720131.xiodi.cn    Running                  Running 3 minutes ago                      
xdrxww29h2wi        sample-stack_whoami.3   training/whoami:latest   c720132.xiodi.cn    Running                  Running 3 minutes ago                      
mtyc6cw65qu0        sample-stack_whoami.4   training/whoami:latest   ubuntu                  Running                  Running 3 minutes ago                      
x8gtlzrqwm3j          sample-stack_whoami.5   training/whoami:latest   c720131.xiodi.cn    Running                  Running 3 minutes ago                      
vfq7gksylhom          sample-stack_whoami.6   training/whoami:latest   c720132.xiodi.cn    Running                  Running 3 minutes ago 


6. 列出在该节点运行的容器

xiodi@c720131:~/docker$ sudo docker container ls
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS               NAMES
0cafe15b204c        training/whoami:latest   "/app/http"         5 minutes ago       Up 5 minutes        8000/tcp            sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop
42b4d5626c58        training/whoami:latest   "/app/http"         5 minutes ago       Up 5 minutes        8000/tcp            sample-stack_whoami.5.x8gtlzrqwm3juduzonczhima1


stack---service---任务--和容器对应关系如下图:


7. 获取服务的详细信息

xiodi@c720131:~/docker$ sudo docker service inspect sample-stack_whoami
[
     {
         "ID": "t98tjpn7xpbyryk7ikbtucwut",
         "Version": {
             "Index": 25
         },
         "CreatedAt": "2018-06-06T07:29:03.797319458Z",
         "UpdatedAt": "2018-06-06T07:29:03.802084194Z",
         "Spec": {
             "Name": "sample-stack_whoami",
             "Labels": {
                 "app": "sample-app",
                 "com.docker.stack.image": "training/whoami:latest",
                 "com.docker.stack.namespace": "sample-stack",
                 "environment": "prod-south"
             },
             "TaskTemplate": {
                 "ContainerSpec": {
                     "Image": "training/whoami:latest@sha256:4d54f5ea6d2c562ae96add2792e8d71a56b7613cbc4e7b8e20a4e4d3085bbaff",
                     "Labels": {
                         "com.docker.stack.namespace": "sample-stack"
                     },
                     "Privileges": {
                         "CredentialSpec": null,
                         "SELinuxContext": null
                     },
                     "StopGracePeriod": 10000000000,
                     "DNSConfig": {},
                     "Isolation": "default"
                 },
                 "Resources": {},
                 "RestartPolicy": {
                     "Condition": "any",
                     "Delay": 5000000000,
                     "MaxAttempts": 0
                 },
                 "Placement": {
                     "Platforms": [
                         {
                             "Architecture": "amd64",
                             "OS": "linux"
                         }
                     ]
                 },
                 "Networks": [
                     {
                         "Target": "ojgp9mdhz2xaorsxca0g64zv2",
                         "Aliases": [
                             "whoami"
                         ]
                     }
                 ],
                 "ForceUpdate": 0,
                 "Runtime": "container"
             },
             "Mode": {
                 "Replicated": {
                     "Replicas": 6
                 }
             },
             "UpdateConfig": {
                 "Parallelism": 2,
                 "Delay": 10000000000,
                 "FailureAction": "pause",
                 "Monitor": 5000000000,
                 "MaxFailureRatio": 0,
                 "Order": "stop-first"
             },
             "RollbackConfig": {
                 "Parallelism": 1,
                 "FailureAction": "pause",
                 "Monitor": 5000000000,
                 "MaxFailureRatio": 0,
                 "Order": "stop-first"
             },
             "EndpointSpec": {
                 "Mode": "vip",
                 "Ports": [
                     {
                         "Protocol": "tcp",
                         "TargetPort": 8000,
                         "PublishedPort": 81,
                         "PublishMode": "ingress"
                     }
                 ]
             }
         },
         "Endpoint": {
             "Spec": {
                 "Mode": "vip",
                 "Ports": [
                     {
                         "Protocol": "tcp",
                         "TargetPort": 8000,
                         "PublishedPort": 81,
                         "PublishMode": "ingress"
                     }
                 ]
             },
             "Ports": [
                 {
                     "Protocol": "tcp",
                     "TargetPort": 8000,
                     "PublishedPort": 81,
                     "PublishMode": "ingress"
                 }
             ],
             "VirtualIPs": [
                 {
                     "NetworkID": "1ov2xqk3ws0017wq9x1eo9sv3",
                     "Addr": "10.255.0.5/16"
                 },
                 {
                     "NetworkID": "ojgp9mdhz2xaorsxca0g64zv2",
                     "Addr": "10.0.0.5/24"
                 }
             ]
         }
     }
]


8. 获取服务的日志信息

xiodi@c720131:~/docker$ sudo docker service logs sample-stack_whoami
sample-stack_whoami.2.3qhfu2lj4qis@c720131.xiodi.cn    | Listening on :8000
sample-stack_whoami.5.x8gtlzrqwm3j@c720131.xiodi.cn    | Listening on :8000
sample-stack_whoami.3.xdrxww29h2wi@c720132.xiodi.cn    | Listening on :8000
sample-stack_whoami.6.vfq7gksylhom@c720132.xiodi.cn    | Listening on :8000
sample-stack_whoami.4.mtyc6cw65qu0@ubuntu    | Listening on :8000
sample-stack_whoami.1.ib67sdpbu7xc@ubuntu    | Listening on :8000


9. 协调至满意状态(在某个容器由于某原因不能提供服务时,swarm会自动修复这种状态)

(1)在此删除其中一个实列。

xiodi@c720131:~$ sudo docker container rm -f sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop
sample-stack_whoami.2.3qhfu2lj4qisgk5akkskzqnop

1528271788(1)


10. 从第9步可以看到,docker-swarm是可以自动恢复的。


11. 接下来演示一下停止掉一个worker节点,看会怎么样。

M945SRM4WG1FT6S5H]H49BW

从上图可以看到,我们停止一个worker节点后,仍然能够自动恢复。


12. 删除服务或者stack.

xiodi@c720131:~$ sudo docker stack rm sample-stack
Removing service sample-stack_whoami
Removing network sample-stack_test-net


四. 使用Docker swarm部署多个服务stack.

1. stack文件内容如下

xiodi@c720131:~/docker$ cat pet-stack.yaml
version: "3.5"
services:
   web:
     image: fundamentalsofdocker/ch08-web:1.0
     networks:
       - pets-net
     ports:
       - 3000:3000
     deploy:
       replicas: 3
   db:
     image: fundamentalsofdocker/ch08-db:1.0
     networks:
       - pets-net
     volumes:
       - pets-data:/var/lib/postgresql/data

volumes:
   pets-data:

networks:
   pets-net:
     driver: overlay


2. 部署服务应用

xiodi@c720131:~/docker$ sudo docker stack deploy -c pet-stack.yaml pets
Creating network pets_pets-net
Creating service pets_db
Creating service pets_web


3. 在pets stack列出所有的任务

xiodi@c720131:~/docker$ sudo docker stack ps pets
ID                  NAME                IMAGE                               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
99uuyw2m5fbc        pets_web.1          fundamentalsofdocker/ch08-web:1.0   c720132.xiodi.cn    Running             Running about a minute ago                      
8key67a3aaai          pets_db.1            fundamentalsofdocker/ch08-db:1.0      c720131.xiodi.cn    Running             Running about a minute ago                      
nkogzxahoxhq         pets_web.2          fundamentalsofdocker/ch08-web:1.0   ubuntu                   Running             Running about a minute ago                      
6230n44cvt04         pets_web.3          fundamentalsofdocker/ch08-web:1.0   c720131.xiodi.cn    Running             Running about a minute ago


4. 请求本机的3000端口,校验部署的服务

[H`1VHW))OEB$L7SC14070Z

posted @ 2018-06-06 16:43  大牛不会太遥远  阅读(827)  评论(0编辑  收藏  举报