[root@swarm-manager ~]# cat > ./sources.list <<END > deb http://mirrors.aliyun.com/debian stretch main contrib non-free > deb http://mirrors.aliyun.com/debian stretch-proposed-updates main contrib non-free > deb http://mirrors.aliyun.com/debian stretch-updates main contrib non-free > deb http://mirrors.aliyun.com/debian-security/ stretch/updates main non-free contrib > END [root@swarm-manager ~]# cat Dockerfile FROM nginx:latest ADD sources.list /etc/apt/sources.list RUN apt-get update && apt-get install -y dnsutils iproute2 net-tools curl && apt-get clean ADD index.html /usr/share/nginx/html/index.html [root@swarm-manager ~]# echo "nginx:v1" > index.html [root@swarm-manager ~]# docker build -t registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 . [root@swarm-manager ~]# echo "nginx:v2" > index.html [root@swarm-manager ~]# docker build -t registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 . [root@swarm-manager ~]# docker push registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 [root@swarm-manager ~]# docker push registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2
[root@swarm-manager ~]# docker service create --name nginx registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 [root@swarm-manager ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS 63c04khgjl03 nginx replicated 1/1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 [root@swarm-manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS zo87xl8e3in9 nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Running Running 4 minutes ago
[root@swarm-manager ~]# docker service update --network-add my-network --publish-add 80:80 nginx [root@swarm-manager ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS 63c04khgjl03 nginx replicated 1/1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 *:80->80/tcp [root@swarm-manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS lppql3gw8phx nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Running Running 3 minutes ago zo87xl8e3in9 \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 3 minutes ago
[root@swarm-manager ~]# docker service scale nginx=2 [root@swarm-manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS lppql3gw8phx nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Running Running 4 minutes ago zo87xl8e3in9 \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 4 minutes ago qqptr8htux76 nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node2 Running Running 2 minutes ago
[root@swarm-manager ~]# curl 127.0.0.1 nginx:v1
[root@swarm-manager ~]# docker service update --image registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 nginx [root@swarm-manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS iwpnzn3ss6uo nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 swarm-node1 Running Running 4 minutes ago lppql3gw8phx \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 4 minutes ago zo87xl8e3in9 \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 5 minutes ago 1c47y3e14dc0 nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 swarm-node2 Running Running 3 minutes ago qqptr8htux76 \_ nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node2 Shutdown Shutdown 3 minutes ago [root@swarm-manager ~]# curl 127.0.0.1 nginx:v2
[root@swarm-manager ~]# docker service update --rollback nginx nginx [root@swarm-manager ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS xign4i5kxc0m nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Running Running 3 minutes ago iwpnzn3ss6uo \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 swarm-node1 Shutdown Shutdown 3 minutes ago lppql3gw8phx \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 5 minutes ago zo87xl8e3in9 \_ nginx.1 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node1 Shutdown Shutdown 6 minutes ago em9hv301ga5f nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node2 Running Running 3 minutes ago 1c47y3e14dc0 \_ nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v2 swarm-node2 Shutdown Shutdown 3 minutes ago qqptr8htux76 \_ nginx.2 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 swarm-node2 Shutdown Shutdown 4 minutes ago [root@swarm-manager ~]# curl 127.0.0.1 nginx:v1
[root@swarm-manager ~]# docker service update --health-cmd "curl -f http://localhost/ || exit 1" nginx [root@swarm-node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7e1935f1c22 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 "nginx -g 'daemon ..." 17 seconds ago Up 12 seconds (health: starting) 80/tcp nginx.1.9dz6g3bizl4kwh8lctr552c6a [root@swarm-node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7e1935f1c22 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 "nginx -g 'daemon ..." 44 seconds ago Up 38 seconds (healthy) 80/tcp nginx.1.9dz6g3bizl4kwh8lctr552c6a
可見當健康狀態異常時,將會中止並移除掉異常的容器並從新啓動一個新的容器。html
[root@swarm-node1 ~]# docker exec -it f7e1935f1c22 rm -f /usr/share/nginx/html/index.html [root@swarm-node1 ~]# date Wed Aug 16 16:48:41 CST 2017 [root@swarm-node1 ~]# docker events --since "2017-08-16T16:48:35" 2017-08-16T16:48:40.063995281+08:00 container exec_create: rm -f /usr/share/nginx/html/index.html f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=lkb6ags10aprqvzii5aht3pjf, com.docker.swarm.task.name=nginx.1.lkb6ags10aprqvzii5aht3pjf, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.lkb6ags10aprqvzii5aht3pjf) ... 2017-08-16T16:49:57.889494869+08:00 container health_status: unhealthy f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=lkb6ags10aprqvzii5aht3pjf, com.docker.swarm.task.name=nginx.1.lkb6ags10aprqvzii5aht3pjf, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.lkb6ags10aprqvzii5aht3pjf) 2017-08-16T16:49:59.891503804+08:00 container kill f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=lkb6ags10aprqvzii5aht3pjf, com.docker.swarm.task.name=nginx.1.lkb6ags10aprqvzii5aht3pjf, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.lkb6ags10aprqvzii5aht3pjf, signal=15) 2017-08-16T16:49:59.964124883+08:00 container die f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=lkb6ags10aprqvzii5aht3pjf, com.docker.swarm.task.name=nginx.1.lkb6ags10aprqvzii5aht3pjf, exitCode=0, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.lkb6ags10aprqvzii5aht3pjf) 2017-08-16T16:50:00.289350308+08:00 network disconnect i6xug49nwdsxauqqpli3apvym (container=f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a, name=ingress, type=overlay) 2017-08-16T16:50:00.289489879+08:00 network disconnect vxe1cwk14avlfp2xjgymhkhdl (container=f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a, name=my-network, type=overlay) 2017-08-16T16:50:00.345105732+08:00 container stop f7e1935f1c2263112fe86a6e74c0d23a2ff369ffd54eab2781f2645a7dc7810a (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=lkb6ags10aprqvzii5aht3pjf, com.docker.swarm.task.name=nginx.1.lkb6ags10aprqvzii5aht3pjf, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.lkb6ags10aprqvzii5aht3pjf) 2017-08-16T16:50:00.922761678+08:00 container create 2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934 (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=y00kqcu26sq75js3fvpqx4kt5, com.docker.swarm.task.name=nginx.1.y00kqcu26sq75js3fvpqx4kt5, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.y00kqcu26sq75js3fvpqx4kt5) 2017-08-16T16:50:03.006063329+08:00 network destroy vxe1cwk14avlfp2xjgymhkhdl (name=my-network, type=overlay) 2017-08-16T16:50:03.376037876+08:00 container destroy 965a0ce53c2c0f2d5a740cb87c39ea51d164f1a6cf5bea0cf0679883dd99cb5c (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=xign4i5kxc0mfp1pji7zzkoo1, com.docker.swarm.task.name=nginx.1.xign4i5kxc0mfp1pji7zzkoo1, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.xign4i5kxc0mfp1pji7zzkoo1) 2017-08-16T16:50:05.720425867+08:00 network connect i6xug49nwdsxauqqpli3apvym (container=2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934, name=ingress, type=overlay) 2017-08-16T16:50:05.808884815+08:00 network disconnect i6xug49nwdsxauqqpli3apvym (container=2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934, name=ingress, type=overlay) 2017-08-16T16:50:05.919636688+08:00 network create vxe1cwk14avlfp2xjgymhkhdl (name=my-network, type=overlay) 2017-08-16T16:50:06.102359133+08:00 network connect i6xug49nwdsxauqqpli3apvym (container=2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934, name=ingress, type=overlay) 2017-08-16T16:50:06.453151382+08:00 network connect vxe1cwk14avlfp2xjgymhkhdl (container=2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934, name=my-network, type=overlay) 2017-08-16T16:50:07.062684099+08:00 container start 2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934 (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=y00kqcu26sq75js3fvpqx4kt5, com.docker.swarm.task.name=nginx.1.y00kqcu26sq75js3fvpqx4kt5, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.y00kqcu26sq75js3fvpqx4kt5) 2017-08-16T16:51:07.221879814+08:00 container exec_create: /bin/sh -c curl -f http://localhost/ || exit 1 2132a1bd9287bc377f0800d9a06e6876aaddb9a71b0afd567c3838fbb6aa3934 (com.docker.swarm.node.id=y83k6khc3vxmch1qd3j8kl4ak, com.docker.swarm.service.id=63c04khgjl033syhc5ef0e9g9, com.docker.swarm.service.name=nginx, com.docker.swarm.task=, com.docker.swarm.task.id=y00kqcu26sq75js3fvpqx4kt5, com.docker.swarm.task.name=nginx.1.y00kqcu26sq75js3fvpqx4kt5, image=registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1@sha256:b223de06038b1187d1d1b955df759839712a10cde33d7f2784ef3f9f5573ad71, name=nginx.1.y00kqcu26sq75js3fvpqx4kt5) [root@swarm-node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2132a1bd9287 registry.cn-hangzhou.aliyuncs.com/vnimos/nginx:v1 "nginx -g 'daemon ..." 31 minutes ago Up 30 minutes (healthy) 80/tcp nginx.1.y00kqcu26sq75js3fvpqx4kt5
[root@swarm-manager ~]# docker node update --label-add project=nginx swarm-node2 [root@swarm-manager ~]# docker node update --label-add "datacenter=xiamen" swarm-node1 [root@swarm-manager ~]# docker node update --label-add "datacenter=fuzhou" swarm-node2 [root@swarm-manager ~]# docker node inspect -f {{.Spec.Labels}} swarm-node1 map[datacenter:xiamen] [root@swarm-manager ~]# docker node inspect -f {{.Spec.Labels}} swarm-node2 map[datacenter:fuzhou project:nginx]
[root@swarm-manager ~]# docker service create --replicas=4 --constraint 'node.hostname == swarm-node1' --name nginx-c1 nginx [root@swarm-manager ~]# docker service create --replicas=4 --constraint 'node.labels.project == nginx' --name nginx-c2 nginx [root@swarm-manager ~]# docker service ps nginx-c1 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS sqcyg8wm8gdt nginx-c1.1 nginx:latest swarm-node1 Running Running 5 minutes ago ttst5umkpt6g nginx-c1.2 nginx:latest swarm-node1 Running Running 5 minutes ago lpiz1vsaj6p3 nginx-c1.3 nginx:latest swarm-node1 Running Running 5 minutes ago ykvrdyty4qie nginx-c1.4 nginx:latest swarm-node1 Running Running 5 minutes ago [root@swarm-manager ~]# docker service ps nginx-c2 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS x322u16dfnyt nginx-c2.1 nginx:latest swarm-node2 Running Running 5 minutes ago zjp93whpf4ah nginx-c2.2 nginx:latest swarm-node2 Running Running 5 minutes ago ff3usxkpo5ae nginx-c2.3 nginx:latest swarm-node2 Running Running 5 minutes ago p3g0haaqg6yu nginx-c2.4 nginx:latest swarm-node2 Running Running 5 minutes ago
[root@swarm-manager ~]# docker service create --replicas=6 --placement-pref 'spread=node.labels.datacenter' --name nginx-c3 nginx [root@swarm-manager ~]# docker service ps nginx-c3 ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS p3y7774kpu0w nginx-c3.1 nginx:latest swarm-node2 Running Running 3 minutes ago 081l22wh87ok nginx-c3.2 nginx:latest swarm-node1 Running Running 3 minutes ago lj1t30mok3te nginx-c3.3 nginx:latest swarm-node1 Running Running 3 minutes ago fhb90j6brwuv nginx-c3.4 nginx:latest swarm-node2 Running Running 3 minutes ago ein699law198 nginx-c3.5 nginx:latest swarm-node2 Running Running 3 minutes ago vo91976o481m nginx-c3.6 nginx:latest swarm-node1 Running Running 3 minutes ago
Docker1.13推出了一個新版本的Docker Compose(V3)。該版本的主要特徵是,容許Swarm Mode使用Docker Compose文件定義直接部署服務。node
[root@swarm-manager ~]# cat docker-compose.yml version: '3' services: mysql: image: mysql environment: - MYSQL_ROOT_PASSWORD=password wordpress: image: wordpress ports: - 80:80 links: - mysql:mysql environment: - WORDPRESS_DB_PASSWORD=password
在Swarm Mode下,須要使用docker stack deploy
命令進行部署,若是直接使用docker-compose up則將會出現以下警告:mysql
[root@swarm-manager ~]# docker-compose up WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`.
[root@swarm-manager ~]# docker stack deploy --compose-file=docker-compose.yml demo Creating network demo_my-network Creating service demo_mysql Creating service demo_wordpress [root@swarm-manager ~]# docker stack ps demo ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 5n5x8z9oso6d demo_wordpress.1 wordpress:latest swarm-node2 Running Running 3 minutes ago 66wmzw3u50i0 demo_mysql.1 mysql:latest swarm-node1 Running Running 4 minutes ago [root@swarm-manager ~]# docker stack services demo ID NAME MODE REPLICAS IMAGE PORTS k791axfnh6le demo_wordpress replicated 1/1 wordpress:latest *:80->80/tcp wp0rz1kls1m1 demo_mysql replicated 1/1 mysql:latest