Doker集群:
- Docker swarm 是docker官方的三大项目之一,提供docker容器集群服务,是docker官方对容器云生态支持的核心方案,Docker Swarm项目开始于2014年,是Docker公司推出的第一个容器集群项目。使用docker swarm可以将多个docker主机封装为一个大型的虚拟docker主机,swarm集群提供给用户管理集群内所有容器的操作接口与使用一台docker主机几乎相同,从而可以快速打造一套容器云服务。
[root@server1 ~]# systemctl start docker
[root@server1 ~]# docker swarm init # 初始化集群
Swarm initialized: current node (rai2qpd7sf6bugqc3ia7pd99t) is now a manager.
To add a worker to this swarm, run the following command:
# 执行如下命令,为集群添加节点
docker swarm join \
--token SWMTKN-1-3uppnqtjkks8ktwys7nfvwbq3m3uqqrz7d3ojlietsgssgu4i7-9qa91w39e44uxt56hpbo0tpgi \
172.25.21.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@server2 ~]# docker swarm join \
> --token SWMTKN-1-3uppnqtjkks8ktwys7nfvwbq3m3uqqrz7d3ojlietsgssgu4i7-9qa91w39e44uxt56hpbo0tpgi \
> 172.25.21.1:2377
This node joined a swarm as a worker.
[root@server3 ~]# docker swarm join \
> --token SWMTKN-1-3uppnqtjkks8ktwys7nfvwbq3m3uqqrz7d3ojlietsgssgu4i7-9qa91w39e44uxt56hpbo0tpgi \
> 172.25.21.1:2377
This node joined a swarm as a worker.
[root@server1 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
ckqm9zm2pnmntnekwv2de2398 server2 Ready Active
rai2qpd7sf6bugqc3ia7pd99t * server1 Ready Active Leader
s4es8qh4qant0zkfh1ciu9hpe server3 Ready Active
[root@server1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 test.com/nginx:v1
# --name 服务名称
# --publish 端口映射
# --replicas 副本个数
v068oxv07bv60skpaorjrwb7x
# 建立成功查看服务状态
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
v068oxv07bv6 nginx replicated 3/3 test.com/nginx:v1
# 查看服务的具体信息(分别在三个节点上运行)
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vq468p2hyk92 nginx.1 test.com/nginx:v1 server3 Running Running 4 seconds ago
q4fqojzhm3ty nginx.2 test.com/nginx:v1 server1 Running Running 4 seconds ago
k3vxbnr5dpqf nginx.3 test.com/nginx:v1 server2 Running Running 5 seconds ago
# 将服务扩容成 6 个
[root@server1 ~]# docker service scale nginx=6
nginx scaled to 6
# 扩容后查看服务状态(此刻为starting)
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vq468p2hyk92 nginx.1 test.com/nginx:v1 server3 Running Running 9 minutes ago
q4fqojzhm3ty nginx.2 test.com/nginx:v1 server1 Running Running 9 minutes ago
k3vxbnr5dpqf nginx.3 test.com/nginx:v1 server2 Running Running 9 minutes ago
sidqa0ps5wnn nginx.4 test.com/nginx:v1 server3 Running Starting less than a second ago
fyula2qmi3j1 nginx.5 test.com/nginx:v1 server1 Running Starting 1 second ago
nivx3r11b0aq nginx.6 test.com/nginx:v1 server2 Running Starting 2 seconds ago
# 再次观察,可看到服务启动成功
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vq468p2hyk92 nginx.1 test.com/nginx:v1 server3 Running Running 9 minutes ago
q4fqojzhm3ty nginx.2 test.com/nginx:v1 server1 Running Running 9 minutes ago
k3vxbnr5dpqf nginx.3 test.com/nginx:v1 server2 Running Running 9 minutes ago
sidqa0ps5wnn nginx.4 test.com/nginx:v1 server3 Running Running 10 seconds ago
fyula2qmi3j1 nginx.5 test.com/nginx:v1 server1 Running Running 11 seconds ago
nivx3r11b0aq nginx.6 test.com/nginx:v1 server2 Running Running 12 seconds ago
# 从私有仓库中获取镜像
[root@server1 ~]# docker pull test.com/visualizer
Using default tag: latest
latest: Pulling from visualizer
88286f41530e: Pull complete
cea94055e43e: Pull complete
Digest: sha256:ea603c00f25b18c9a3aa49703c47bd5ca6c105637fe6f4887b606043c66ab528
Status: Downloaded newer image for test.com/visualizer:latest
# 进行服务建立
[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 \
> test.com/visualizer # 镜像
rgizfcjvt4s1qijp57cmmr80w
# 建立成功,查看容器基本信息
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a801ae7086cf test.com/visualizer@sha256:ea603c00f25b18c9a3aa49703c47bd5ca6c105637fe6f4887b606043c66ab528 "npm start" 6 seconds ago Up 4 seconds (health: starting) 8080/tcp viz.1.p2x0cl1ep910bpp1giu6trxp6
# 将服务的容器数缩减为3个
[root@server1 ~]# docker service scale nginx=3
nginx scaled to 3
# 查看服务信息,副本数变成3
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
rgizfcjvt4s1 viz replicated 1/1 test.com/visualizer:latest
v068oxv07bv6 nginx replicated 3/3 test.com/nginx:v1
# 查看服务具体信息
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
q4fqojzhm3ty nginx.2 test.com/nginx:v1 server1 Running Running 26 minutes ago
k3vxbnr5dpqf nginx.3 test.com/nginx:v1 server2 Running Running 26 minutes ago
sidqa0ps5wnn nginx.4 test.com/nginx:v1 server3 Running Running 16 minutes ago
在监控端进行查看
为各个节点创建默认发布文件
# 编辑默发布页面
[root@server1 ~]# echo 'server1' > index.html
[root@server1 ~]# cat index.html
server1
查看容器信息
[root@server1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a801ae7086cf test.com/visualizer@sha256:ea603c00f25b18c9a3aa49703c47bd5ca6c105637fe6f4887b606043c66ab528 "npm start" 8 minutes ago Up 8 minutes (healthy) 8080/tcp viz.1.p2x0cl1ep910bpp1giu6trxp6
82aa12ca3102 test.com/nginx@sha256:2de9d5fc6585b3f330ff5f2c323d2a4006a49a476729bbc0910b695771526e3f "nginx -g 'daemon ..." 29 minutes ago Up 29 minutes 80/tcp nginx.2.q4fqojzhm3tyi90q3slx6ciy2
# 将默认发布页面传到容器中的指定位置
[root@server1 ~]# docker container cp index.html nginx.2.q4fqojzhm3tyi90q3slx6ciy2:/usr/share/nginx/html
在客户端进行测试:
进行测试:
[root@foundation21 docker]# for i in {1..12}; do curl 172.25.21.1; done
<h1>server2<h1/>
<h1>server3<h1/>
<h1>server1<h1/>
<h1>server2<h1/>
<h1>server3<h1/>
<h1>server1<h1/>
<h1>server2<h1/>
<h1>server3<h1/>
<h1>server1<h1/>
<h1>server2<h1/>
<h1>server3<h1/>
<h1>server1<h1/>
自动进行负载均衡
手动让节点3(server3)宕机,进行测试
[root@server3 ~]# systemctl stop docker.service
在swarm manager 端进行查看:
查看服务信息,副本数没有变化
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
rgizfcjvt4s1 viz replicated 1/1 test.com/visualizer:latest
v068oxv07bv6 nginx replicated 3/3 test.com/nginx:v1
# 通过ps 了看到server3上的容器宕掉了,server2 在多建立一个容器
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
q4fqojzhm3ty nginx.2 test.com/nginx:v1 server1 Running Running 53 minutes ago
k3vxbnr5dpqf nginx.3 test.com/nginx:v1 server2 Running Running 53 minutes ago
m9770t1100q2 nginx.4 test.com/nginx:v1 server2 Running Running 9 seconds ago
sidqa0ps5wnn \_ nginx.4 test.com/nginx:v1 server3 Shutdown Running 26 seconds ago
在监控页面进行查看
恢复server3后,服务是不会自动迁移的
将服务的副本数扩展到30个
[root@server1 ~]# docker service scale nginx=30
nginx scaled to 30
查看服务列表,nginx副本30全alive
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
rgizfcjvt4s1 viz replicated 1/1 test.com/visualizer:latest
v068oxv07bv6 nginx replicated 30/30 test.com/nginx:v1
[root@server1 ~]# docker service update --image test.com/rhel7:v1 --update-parallelism 3 --update-delay 10s nginx
nginx
更新成功后:
更新成功后,镜像换取完成
[root@server1 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
rgizfcjvt4s1 viz replicated 1/1 test.com/visualizer:latest
v068oxv07bv6 nginx replicated 30/30 test.com/rhel7:v1
[root@server1 ~]# docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
xqwutjl221o2 nginx.1 test.com/rhel7:v1 server3 Running Running about a minute ago
v46h5o48oaui \_ nginx.1 test.com/nginx:v1 server3 Shutdown Shutdown about a minute ago
xdtwrrzsx1d5 nginx.2 test.com/rhel7:v1 server1 Running Running 40 seconds ago
q4fqojzhm3ty \_ nginx.2 test.com/nginx:v1 server1 Shutdown Shutdown 43 seconds ago
uaefrls6ejda nginx.3 test.com/rhel7:v1 server1 Running Running 56 seconds ago
k3vxbnr5dpqf \_ nginx.3 test.com/nginx:v1 server2 Shutdown Shutdown 59 seconds ago
......................省略...........................