建立swarm集群,加入节点
[root@server1 ~]# docker swarm init
Swarm initialized: current node (mqckm2p9hxanpf0y40vilrrhm) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1k4cko7qtg3k7m584jlxgnfb37dz1e7vxcti0ewob0gp1cc3q5-1kojtcxemf2u2u6gqdf189u8m 172.25.24.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@server2 ~]# systemctl start docker
[root@server2 ~]# docker swarm join --token SWMTKN-1-1k4cko7qtg3k7m584jlxgnfb37dz1e7vxcti0ewob0gp1cc3q5-1kojtcxemf2u2u6gqdf189u8m 172.25.24.1:2377
This node joined a swarm as a worker.
[root@server3 docker]# systemctl start docker
[root@server3 docker]# docker swarm join --token SWMTKN-1-1k4cko7qtg3k7m584jlxgnfb37dz1e7vxcti0ewob0gp1cc3q5-1kojtcxemf2u2u6gqdf189u8m 172.25.24.1:2377
This node joined a swarm as a worker.
[root@server1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
mqckm2p9hxanpf0y40vilrrhm * server1 Ready Active Leader 18.06.1-ce
elzhtgtfyg9t1r6mejdyc4z02 server2 Ready Active 18.06.1-ce
su4d2q9uvc0j06rgfp0571gap server3 Ready Active 18.06.1-ce
创建集群服务nginx
[root@server2 ~]# docker load -i nginx.tar
[root@server3 ~]# docker load -i nginx.tar
[root@server1 ~]# docker network create -d overlay webnet
yl2yqzdktqqhr763jbkfiiwki
[root@server1 ~]# docker service create --name web --network webnet --publish 80:80 --replicas 3 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.
scwxsomz2naes2bodvdvvagqx
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
scwxsomz2nae web replicated 3/3 nginx:latest *:80->80/tcp
[root@server1 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sg8igqtfinoc web.1 nginx:latest server3 Running Running about a minute ago
l0ru6g12iqyg web.2 nginx:latest server1 Running Running 44 seconds ago
o6w5m21ichio web.3 nginx:latest server2 Running Running about a minute ago
扩大服务规模
[root@server1 ~]# 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@server1 ~]# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
sg8igqtfinoc web.1 nginx:latest server3 Running Running 3 minutes ago
l0ru6g12iqyg web.2 nginx:latest server1 Running Running 3 minutes ago
o6w5m21ichio web.3 nginx:latest server2 Running Running 3 minutes ago
k79qb7uvri1x web.4 nginx:latest server1 Running Running 58 seconds ago
rbtytd6afgu3 web.5 nginx:latest server2 Running Running about a minute ago
tqaz5yx7kvp5 web.6 nginx:latest server3 Running Running about a minute ago
swarm自带负载均衡
[root@server1 ~]# vim index.html
[root@server1 ~]# docker cp index.html web.2.l0ru6g12iqygvvj97bijrnib6:/usr/share/nginx/html
[root@server2 ~]# vim index.html
[root@server2 ~]# docker cp index.html web.3.o6w5m21ichiodz7k741z5ib2x:/usr/share/nginx/html
[root@server3 ~]# docker cp index.html web.1.sg8igqtfinoc7rs2ic25do7qn:/usr/share/nginx/html
[root@foundation24 Desktop]# for i in {1..10}; do curl http://172.25.24.1/index.html; done
server1
server2
server3
server1
server2
server3
server1
server2
server3
server1
监控
[root@server1 ~]# docker load -i visualizer.tar
[root@server2 ~]# docker load -i visualizer.tar
[root@server3 ~]# docker load -i visualizer.tar
[root@server1 ~]# 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
mage 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.
ieykdny57jo96egraywu9bu57
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
在浏览器上访问http://172.25.24.1:8080/
更新服务
[root@server1 ~]# docker load -i httpd.tar
[root@server2 ~]# docker load -i httpd.tar
[root@server3 ~]# docker load -i httpd.tar
[root@server1 ~]# docker service update --image httpd --update-delay 5s --update-parallelism 5 web
#更新间隔5s 每次更新5个
portainer
[root@server1 portainer]# docker load -i portainer-agent.tar
dd4969f97241: Loading layer 278kB/278kB
1f0ac9aec537: Loading layer 2.048kB/2.048kB
0cf0d1d03535: Loading layer 12.15MB/12.15MB
Loaded image ID: sha256:9335796fedf93898c4c8b00b77cfee6bad0f5bf6f1e1b802faeb29a4058bf719
[root@server1 portainer]# docker load -i portainer.tar
e7260fd2a5f2: Loading layer 73.85MB/73.85MB
[root@server1 portainer]# docker stack deploy --compose-file=portainer-agent-stack.yml portainer
[root@server2 portainer]# docker load -i portainer-agent.tar
dd4969f97241: Loading layer 278kB/278kB
1f0ac9aec537: Loading layer 2.048kB/2.048kB
0cf0d1d03535: Loading layer 12.15MB/12.15MB
[root@server3 portainer]# docker load -i portainer-agent.tar
dd4969f97241: Loading layer 278kB/278kB
1f0ac9aec537: Loading layer 2.048kB/2.048kB
0cf0d1d03535: Loading layer 12.15MB/12.15MB
Loaded image ID: sha256:9335796fedf93898c4c8b00b77cfee6bad0f5bf6f1e1b802faeb29a4058bf719