Docker-swarm 将节点分为 manager 和 worker
172.25.61.12 做manager(server12)
172.25.61.13(server13)和172.25.61.14(server14)做worker
- 在 manager(server12) 端创建 swarm 环境,获取 token
Server12:
[root@server12 ~]# yum install container-selinux-2.21-1.el7.noarch.rpm docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm libsemanage-2.5-8.el7.x86_64.rpm libsemanage-python-2.5-8.el7.x86_64.rpm pigz-2.3.4-1.el7.x86_64.rpm policycoreutils-2.5-17.1.el7.x86_64.rpm policycoreutils-python-2.5-17.1.el7.x86_64.rpm -y
[root@server12 ~]# systemctl start docker.service
[root@server12 ~]# docker load -i nginx.tar
cdb3f9544e4c: Loading layer 58.44MB/58.44MB
a8c4aeeaa045: Loading layer 54.24MB/54.24MB
08d25fa0442e: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@server12 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 2 months ago 109MB
[root@server12 ~]# docker swarm init
Swarm initialized: current node (fhn4aa274tejkrk3e2qwc4u7f) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5bnoptdinrcpn1tvxlojcy83ubd5qh5400s16lkkh5ujybrkny-5bwww2fqk0sehpop3a2jrdq45 172.25.61.12:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
其他两个 worker 节点加入到 swarm cluster
Server13:
[root@server13 ~]# yum install container-selinux-2.21-1.el7.noarch.rpm docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm libsemanage-2.5-8.el7.x86_64.rpm libsemanage-python-2.5-8.el7.x86_64.rpm pigz-2.3.4-1.el7.x86_64.rpm policycoreutils-2.5-17.1.el7.x86_64.rpm policycoreutils-python-2.5-17.1.el7.x86_64.rpm -y
[root@server13 ~]# systemctl start docker.service
[root@server13 ~]# docker load -i nginx.tar
cdb3f9544e4c: Loading layer 58.44MB/58.44MB
a8c4aeeaa045: Loading layer 54.24MB/54.24MB
08d25fa0442e: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@server13 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 2 months ago 109MB
[root@server13 ~]# docker swarm join --token SWMTKN-1-5bnoptdinrcpn1tvxlojcy83ubd5qh5400s16lkkh5ujybrkny-5bwww2fqk0sehpop3a2jrdq45 172.25.61.12:2377
This node joined a swarm as a worker.
Server14:
[root@server14 ~]# yum install container-selinux-2.21-1.el7.noarch.rpm docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm libsemanage-2.5-8.el7.x86_64.rpm libsemanage-python-2.5-8.el7.x86_64.rpm pigz-2.3.4-1.el7.x86_64.rpm policycoreutils-2.5-17.1.el7.x86_64.rpm policycoreutils-python-2.5-17.1.el7.x86_64.rpm -y
[root@server14 ~]# systemctl start docker.service
[root@server14 ~]# docker load -i nginx.tar
cdb3f9544e4c: Loading layer 58.44MB/58.44MB
a8c4aeeaa045: Loading layer 54.24MB/54.24MB
08d25fa0442e: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@server14 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 2 months ago 109MB
[root@server14 ~]# docker swarm join --token SWMTKN-1-5bnoptdinrcpn1tvxlojcy83ubd5qh5400s16lkkh5ujybrkny-5bwww2fqk0sehpop3a2jrdq45 172.25.61.12:2377
This node joined a swarm as a worker.
在 manager 端查看 swarm 节点信息
Server12:
[root@server12 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
fhn4aa274tejkrk3e2qwc4u7f * server12 Ready Active Leader 18.03.1-ce
9fs5h5liktjov1of24u0zchah server13 Ready Active 18.03.1-ce
k9xyzw19owsjgvtfo5yzlw0fs server14 Ready Active 18.03.1-ce
[root@server12 ~]# docker service create --name web --replicas 3 -p 80:80 nginx
image nginx:latest could not be accessed on a registry to record
its digest. Each node will access nginx:latest independently,
possibly leading to different nodes running different
versions of the image.
w2ttvsc51jvcgyvaul9cqp7ij
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
[root@server12 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
aq6uyd3vbxhc web.1 nginx:latest server13 Running Running 17 seconds ago
jku1zy8t24rw web.2 nginx:latest server14 Running Running 17 seconds ago
yrjh49fmztl0 web.3 nginx:latest server12 Running Running 17 seconds ago
访问网页
编辑默认发布页,复制到nginx容器下
[root@server12 ~]# vim index.html
server12
[root@server12 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dd4e46a733e nginx:latest "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp web.3.yrjh49fmztl0hky4ia9e2vzl6
[root@server12 ~]# docker cp index.html 8dd4e46a733e:/usr/share/nginx/html
Server13:
[root@server13 ~]# vim index.html
server13
[root@server13 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
152c98f43e1f nginx:latest "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 80/tcp web.1.aq6uyd3vbxhc8xxocvzm2uy55
[root@server13 ~]# docker cp index.html 152c98f43e1f:/usr/share/nginx/html
Server14:
[root@server14 ~]# vim index.html
server14
[root@server14 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c178f4823e3 nginx:latest "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 80/tcp web.2.jku1zy8t24rwopcmxtgnjw7e8
[root@server14 ~]# docker cp index.html 5c178f4823e3:/usr/share/nginx/html
测试其负载均衡
真实主机:
[root@foundation61 Desktop]# curl 172.25.61.12
server14
[root@foundation61 Desktop]# curl 172.25.61.12
server12
[root@foundation61 Desktop]# curl 172.25.61.12
server13
[root@foundation61 Desktop]# curl 172.25.61.12
server14
[root@foundation61 Desktop]# curl 172.25.61.12
server12
[root@foundation61 Desktop]# curl 172.25.61.12
server13
visualizer图形监控
[root@server11 ~]# docker load -i visualizer.tar
5bef08742407: Loading layer 4.221MB/4.221MB
5f70bf18a086: Loading layer 1.024kB/1.024kB
0a19bde117a5: Loading layer 60.01MB/60.01MB
f7e883283ebc: Loading layer 3.942MB/3.942MB
dfd8ee95c7e7: Loading layer 1.536kB/1.536kB
300a6cad969a: Loading layer 8.704kB/8.704kB
d1627040da6d: Loading layer 489kB/489kB
00ed018016c5: Loading layer 2.56kB/2.56kB
d5aa1ab1b431: Loading layer 4.096kB/4.096kB
2d6a463420f7: Loading layer 4.608kB/4.608kB
53888d7f4cca: Loading layer 2.56kB/2.56kB
ea93ed99abca: Loading layer 2.598MB/2.598MB
fa467b43abc0: Loading layer 4.096kB/4.096kB
94cd25765710: Loading layer 96.48MB/96.48MB
Loaded image: dockersamples/visualizer:latest
创建服务
[root@server11 ~]# docker service create \
> --name=viz \
> --publish=8080:8080/tcp \
> --constraint=node.role==manager \
> --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
> dockersamples/visualizer
image dockersamples/visualizer:latest could not be accessed on a registry to record
its digest. Each node will access dockersamples/visualizer:latest independently,
possibly leading to different nodes running different
versions of the image.
q45h9u9ljuv0sqethfpjzemjt
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
在浏览器访问:
scale参数:
其可以调整容器副本的个数
[root@server11 ~]# docker service scale web=6
web scaled to 6
overall progress: 6 out of 6 tasks
1/6: running
2/6: running
3/6: running
4/6: running
5/6: running
6/6: running
verify: Service converged
[root@server11 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
s1554khdx725 web.1 nginx:latest server13 Running Running about an hour ago
wvhcay4td1iz web.2 nginx:latest server11 Running Running about an hour ago
uk58fbxvhx3c web.3 nginx:latest server12 Running Running about an hour ago
rsberv76mc06 web.4 nginx:latest server12 Running Running 23 seconds ago
xvl4v47pg679 web.5 nginx:latest server13 Running Running 23 seconds ago
z93c8om5fd8p web.6 nginx:latest server11 Running Running 23 seconds ago
[root@server11 ~]# docker service scale web=50
web scaled to 50
overall progress: 50 out of 50 tasks
verify: Service converged
[root@foundation20 docker]# scp -r certs.d/ server11:/etc/docker/
root@server11's password:
ca.crt 100% 2098 2.1KB/s 00:00
[root@foundation20 docker]# scp -r certs.d/ server12:/etc/docker/
root@server12's password:
ca.crt 100% 2098 2.1KB/s 00:00
[root@foundation20 docker]# scp -r certs.d/ server13:/etc/docker/
root@server13's password:
ca.crt 100% 2098 2.1KB/s 00:00
Server11,server12,server13:
[root@server11 ~]# vim /etc/hosts
172.25.20.250 westos.org
[root@server11 ~]# docker pull westos.org/rhel7:v1
v1: Pulling from rhel7
48f5bbc9baf5: Pull complete
50e6de2e7131: Pull complete
6cc5139c9240: Pull complete
Digest: sha256:4a1f7e556c0975934d2c4263829252d171fa67b3618727250b0a6b6337d685be
Status: Downloaded newer image for westos.org/rhel7:v1
将nginx更新为westos.org/rhel7:v1
Server11:
[root@server11 ~]# docker service update --update-delay 5s --update-parallelism 5 --image westos.org/rhel7:v1 web
web
overall progress: 50 out of 50 tasks