【原文链接】
(1)本文在Docker(14)DockerSwarm集群搭建 的环境基础上
如下,有三个manager节点(192.168.145.128,192.168.145.129,192.168.145.130)和一个worker节点(192.168.145.131)
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
fhpe4p0m4gb47pxh48kf9psp8 localhost.localdomain Ready Active Reachable 20.10.10
p1cfiyfyxgkhupjj03tvq40ph localhost.localdomain Ready Active Reachable 20.10.10
wsqaq4isrzhlgphnnaqxucy6r * localhost.localdomain Ready Active Leader 20.10.10
xlgi99te9cchyz9n3ydkycf6k localhost.localdomain Ready Active 20.10.10
[root@localhost ~]#
(2)查看docker service 帮助文档
[root@localhost ~]# docker service --help
Usage: docker service COMMAND
Manage services
Commands:
create Create a new service
inspect Display detailed information on one or more services
logs Fetch the logs of a service or task
ls List services
ps List the tasks of one or more services
rm Remove one or more services
rollback Revert changes to a service's configuration
scale Scale one or multiple replicated services
update Update a service
Run 'docker service COMMAND --help' for more information on a command.
[root@localhost ~]#
(3)创建集群服务
如下,在一个manager集群节点上(192.168.145.128)创建一个Nginx集群服务,对外暴露端口8080,映射到内部80端口,设置副本数为2
[root@localhost ~]# docker service create --name nginx_service -p 8080:80 --replicas 2 nginx
j2jgvfdfyqgcsf1zcii23djul
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
[root@localhost ~]#
(4)查看已创建的集群服务
[root@localhost ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
j2jgvfdfyqgc nginx_service replicated 2/2 nginx:latest *:8080->80/tcp
[root@localhost ~]#
(5)查看已经创建的容器
如下,发现在192.168.145.128和192.168.145.129上分别启动了一个docker,这是因为设置了副本数为2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c01d7f019abd nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx_service.2.k3zo9jubllvv95muy3l4gydf3
[root@localhost ~]#
(6)在浏览器访问,正常
(7)动态扩容服务
在其中一个manager节点执行扩容操作,设置副本数为10
[root@localhost ~]# docker service update --replicas 10 nginx_service
nginx_service
overall progress: 10 out of 10 tasks
1/10: running [==================================================>]
2/10: running [==================================================>]
3/10: running [==================================================>]
4/10: running [==================================================>]
5/10: running [==================================================>]
6/10: running [==================================================>]
7/10: running [==================================================>]
8/10: running [==================================================>]
9/10: running [==================================================>]
10/10: running [==================================================>]
verify: Service converged
[root@localhost ~]#
(8)查看各个节点docker数量
发现在各个节点上共创建了10个正常的docker,因为设置了副本数为10
[root@localhost ~]# docker service ps nginx_service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
u41u5xs2sx4u nginx_service.1 nginx:latest localhost.localdomain Running Running 47 seconds ago
jnyxv9jftpj5 nginx_service.2 nginx:latest localhost.localdomain Running Running 3 seconds ago
s823t867u872 nginx_service.3 nginx:latest localhost.localdomain Running Running 32 seconds ago
dshe3f5rfkza \_ nginx_service.3 nginx:latest localhost.localdomain Shutdown Failed 3 seconds ago "error creating external conne…"
8r80ylbz4v8c \_ nginx_service.3 nginx:latest localhost.localdomain Shutdown Rejected 4 seconds ago "error creating external conne…"
mwo0vxu9zd4d \_ nginx_service.3 nginx:latest localhost.localdomain Shutdown Failed 5 seconds ago "error creating external conne…"
c8f19q3tge0s nginx_service.4 nginx:latest localhost.localdomain Running Running 4 seconds ago
01smx140ihm6 nginx_service.5 nginx:latest localhost.localdomain Running Running less than a second ago
9onahdwcgfdg \_ nginx_service.5 nginx:latest localhost.localdomain Shutdown Rejected less than a second ago "error creating external conne…"
ulr5ntbeqltz \_ nginx_service.5 nginx:latest localhost.localdomain Shutdown Rejected 13 seconds ago "error creating external conne…"
c7rbpp5hcadx \_ nginx_service.5 nginx:latest localhost.localdomain Shutdown Failed 14 seconds ago "error creating external conne…"
k8u10ncvdpje nginx_service.6 nginx:latest localhost.localdomain Running Running 33 seconds ago
n8rvtsglqrum \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 8 seconds ago "error creating external conne…"
kfm79fl5773k \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Failed 9 seconds ago "error creating external conne…"
ep3mz85cq42q \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 6 seconds ago "error creating external conne…"
t53va7itdkj8 nginx_service.7 nginx:latest localhost.localdomain Running Running 3 seconds ago
0uozqvv5gx9r nginx_service.8 nginx:latest localhost.localdomain Running Running 47 seconds ago
7u58boi8oppy nginx_service.9 nginx:latest localhost.localdomain Running Running less than a second ago
p1pzcqsgfaoo \_ nginx_service.9 nginx:latest localhost.localdomain Shutdown Failed less than a second ago "error creating external conne…"
p34rm7b9309p \_ nginx_service.9 nginx:latest localhost.localdomain Shutdown Failed less than a second ago "error creating external conne…"
1lgbmlxpnf32 \_ nginx_service.9 nginx:latest localhost.localdomain Shutdown Rejected 14 seconds ago "error creating external conne…"
yo0zcq4vej8i nginx_service.10 nginx:latest localhost.localdomain Running Running 47 seconds ago
[root@localhost ~]#
(9)动态缩容
如下,设置副本数为4
[root@localhost ~]# docker service update --replicas 4 nginx_service
nginx_service
overall progress: 4 out of 4 tasks
1/4: running [==================================================>]
2/4: running [==================================================>]
3/4: running [==================================================>]
4/4: running [==================================================>]
verify: Service converged
[root@localhost ~]#
(10)查看各个节点docker数量
然后查看发现各个节点docker总数压缩为4个了
[root@localhost ~]# docker service ps nginx_service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
zm51ipb5u3zk nginx_service.1 nginx:latest localhost.localdomain Running Running 14 minutes ago
k3zo9jubllvv nginx_service.2 nginx:latest localhost.localdomain Running Running 14 minutes ago
lfjg4kovkuu2 \_ nginx_service.2 nginx:latest localhost.localdomain Shutdown Rejected 14 minutes ago "error creating external conne…"
ph1p09dnzzol \_ nginx_service.2 nginx:latest localhost.localdomain Shutdown Rejected 14 minutes ago "error creating external conne…"
twg01hlw9lge \_ nginx_service.2 nginx:latest localhost.localdomain Shutdown Rejected 14 minutes ago "error creating external conne…"
6sccd1tvt8rq nginx_service.3 nginx:latest localhost.localdomain Running Running 4 minutes ago
y3lk71ay7tqn \_ nginx_service.3 nginx:latest localhost.localdomain Shutdown Rejected 4 minutes ago "error creating external conne…"
7oqy4e593wo8 \_ nginx_service.3 nginx:latest localhost.localdomain Shutdown Failed 4 minutes ago "error creating external conne…"
97wxj8kyr4be nginx_service.6 nginx:latest localhost.localdomain Running Running 4 minutes ago
1hvkth5ioau2 \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 4 minutes ago "error creating external conne…"
svwo7uyxqomp \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 4 minutes ago "error creating external conne…"
xv3ao9argjwo \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 4 minutes ago "error creating external conne…"
sdsay83pvtz2 \_ nginx_service.6 nginx:latest localhost.localdomain Shutdown Rejected 4 minutes ago "error creating external conne…"
[root@localhost ~]#