104、验证Swarm数据持久性 (Swarm11)
上一节我们成功将 nfs 的volume挂载到 Service上,本节验证 Failover时,数据会不会丢失。
Scale Up
增加副本,并验证数据是否能够同步到新启动的容器上
root@host03:~# docker service update --replicas 4 my_web
my_web
overall progress: 4 out of 4 tasks
1/4: running [==================================================>]
2/4: running [==================================================>]
3/4: running [==================================================>]
4/4: running [==================================================>]
verify: Service converged
root@host03:~# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a2v73m955o9j my_web.1 httpd:latest host01 Running Running 13 minutes ago
uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 13 minutes ago
f4tg54ze3hgg my_web.3 httpd:latest host02 Running Running 14 seconds ago
znf4m0jpa0r5 my_web.4 httpd:latest host01 Running Running 14 seconds ago
root@host01:~# docker inspect my_web.4.znf4m0jpa0r5p3fklp1hgdwhu | jq .[0].Mounts
[
{
"Type": "volume",
"Name": "volume-nfs",
"Source": "/var/lib/docker/volumes/volume-nfs/_data",
"Destination": "/usr/local/apache2/htdocs",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
]
root@host01:~# docker exec my_web.4.znf4m0jpa0r5p3fklp1hgdwhu cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
root@host02:~# docker inspect my_web.3.f4tg54ze3hggppy96jldhtdxs | jq .[0].Mounts
[
{
"Type": "volume",
"Name": "volume-nfs",
"Source": "/var/lib/docker/volumes/volume-nfs/_data",
"Destination": "/usr/local/apache2/htdocs",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
]
root@host02:~# docker exec my_web.3.f4tg54ze3hggppy96jldhtdxs cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
更新Volume内容,并进行验证
root@host03:~# echo "add test str" >> /var/nfs/index.html
root@host03:~# cat /var/nfs/index.html
docker swarm nfs volume test
add test str
root@host03:~# curl http://10.12.31.211
docker swarm nfs volume test
add test str
root@host03:~# curl http://10.12.31.212
docker swarm nfs volume test
add test str
root@host03:~# curl http://10.12.31.213
docker swarm nfs volume test
add test str
root@host03:~# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a2v73m955o9j my_web.1 httpd:latest host01 Running Running 17 minutes ago
uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 17 minutes ago
f4tg54ze3hgg my_web.3 httpd:latest host02 Running Running 4 minutes ago
znf4m0jpa0r5 my_web.4 httpd:latest host01 Running Running 4 minutes ago
root@host01:~# docker exec my_web.1.a2v73m955o9js3fbihf0dwei6 cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
root@host01:~# docker exec my_web.4.znf4m0jpa0r5p3fklp1hgdwhu cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
root@host02:~# docker exec my_web.2.uzdfouv3s2bz0ohyahjeeyqao cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
root@host02:~# docker exec my_web.3.f4tg54ze3hggppy96jldhtdxs cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
Failover 验证,host01关机
root@host03:~# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a2v73m955o9j my_web.1 httpd:latest host01 Running Running 9 seconds ago
uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 21 minutes ago
f4tg54ze3hgg my_web.3 httpd:latest host02 Running Running 8 minutes ago
znf4m0jpa0r5 my_web.4 httpd:latest host01 Running Running 9 seconds ago
root@host03:~# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
i4j8tplkkq34 my_web.1 httpd:latest host02 Ready Ready less than a second ago
a2v73m955o9j \_ my_web.1 httpd:latest host01 Shutdown Running 10 seconds ago
uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 21 minutes ago
f4tg54ze3hgg my_web.3 httpd:latest host02 Running Running 8 minutes ago
ve0j4of1m1ao my_web.4 httpd:latest host02 Ready Ready less than a second ago
znf4m0jpa0r5 \_ my_web.4 httpd:latest host01 Shutdown Running 10 seconds ago
root@host03:~# docker service ps my_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
i4j8tplkkq34 my_web.1 httpd:latest host02 Running Ready 4 seconds ago
a2v73m955o9j \_ my_web.1 httpd:latest host01 Shutdown Running 15 seconds ago
uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 21 minutes ago
f4tg54ze3hgg my_web.3 httpd:latest host02 Running Running 8 minutes ago
ve0j4of1m1ao my_web.4 httpd:latest host02 Running Ready 4 seconds ago
znf4m0jpa0r5 \_ my_web.4 httpd:latest host01 Shutdown Running 15 seconds ago
root@host02:~# docker ps | grep my_web
e46bd42b6fda httpd:latest "httpd-foreground" About a minute ago Up About a minute 80/tcp my_web.4.ve0j4of1m1aosbmpg9yew8dxr
5338df22fc68 httpd:latest "httpd-foreground" About a minute ago Up About a minute 80/tcp my_web.1.i4j8tplkkq348ae0tcdcrl38r
426a79228471 httpd:latest "httpd-foreground" 10 minutes ago Up 10 minutes 80/tcp my_web.3.f4tg54ze3hggppy96jldhtdxs
5c5fffec6de6 httpd:latest "httpd-foreground" 23 minutes ago Up 23 minutes 80/tcp my_web.2.uzdfouv3s2bz0ohyahjeeyqao
root@host02:~# docker exec my_web.1.i4j8tplkkq348ae0tcdcrl38r cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
root@host02:~# docker exec my_web.4.ve0j4of1m1aosbmpg9yew8dxr cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
add test str
root@host03:~# curl http://10.12.31.211 # 因为host01 关机了,所有host01的ip也访问不了了
curl: (7) Failed to connect to 10.12.31.211 port 80: No route to host
root@host03:~# curl http://10.12.31.212
docker swarm nfs volume test
add test str
root@host03:~# curl http://10.12.31.213
docker swarm nfs volume test
add test str