1. 程式人生 > >103、Swarm如何管理存儲數據?(Swarm10)

103、Swarm如何管理存儲數據?(Swarm10)

好的 label adding exe ins 副本 systemctl port love

參考https://www.cnblogs.com/CloudMan6/p/8000906.html Service 的容器副本會 scale up/down ,會 failover,會在不同的主機上創建和銷毀,這就引出一個問題,如果Service有要管理的數據,那麽這些數據應該如何存放呢? 選項一:打包在容器裏 顯然不行,除非數據不會發生變化,否則,如何在多個副本間保持同步呢 選項二:數據放在Docker主機的本地目錄中,通過 volume 映射到容器中 位於同一個主機的副本倒是可以共享這個volume,但是不同主機中的副本該如何同步呢? 選項三:利用 Docker 的 volume driver ,由外部的storage provider 管理和提供 volume,所有Docker 主機的volume將掛載到各個副本。 這是目前最好的方案了,volume 不依賴docker主機和容器,生命周期由storage provider 管理,volume 的高可用和數據有效性也全權由provider負責,Docker只管使用。 下面我們以 nfs 來實踐第三種方案 host01 10.12.31.211 nfs-client host02 10.12.31.212 nfs-client host03 10.12.31.213 nfs-client + nfs-server(/var/nfs) 安裝和配置 nfs-server
[email protected]:~# apt install -y nfs-kernel-server [email protected]:~# mkdir /var/nfs [email protected]:~# cat /etc/exports /var/nfs * (rw,sync,no_root_squash) [email protected]:~# systemctl restart nfs-kernel-server.service 安裝和配置 nfs-client (需要在每臺host都創建一遍 volume) [email protected]:~# apt install -y nfs-common
[email protected]:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs [email protected]:~# docker volume ls DRIVER VOLUME NAME local volume-nfs [email protected]:~# apt install -y nfs-common
[email protected]:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs [email protected]:~# docker volume ls DRIVER VOLUME NAME local volume-nfs [email protected]:~# apt install -y nfs-common [email protected]:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs [email protected]:~# docker volume ls DRIVER VOLUME NAME local volume-nfs 創建Service 並掛載nfs 的volume,並驗證 [email protected]:~# docker service create --name my_web --publish 80:80 --mount type=volume,source=volume-nfs,destination=/usr/local/apache2/htdocs --replicas 2 httpd z3ojyj6n5ibpyh4ab49664pvn overall progress: 2 out of 2 tasks 1/2: running 2/2: running verify: Service converged [email protected]:~# cat /var/nfs/index.html docker swarm nfs volume test [email protected]:~# curl http://10.12.31.211 docker swarm nfs volume test [email protected]:~# curl http://10.12.31.212 docker swarm nfs volume test [email protected]:~# curl http://10.12.31.213 docker swarm nfs volume test [email protected]:~# docker service ps my_web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS a2v73m955o9j my_web.1 httpd:latest host01 Running Running 4 minutes ago uzdfouv3s2bz my_web.2 httpd:latest host02 Running Running 4 minutes ago [email protected]:~# docker exec -it my_web.1.a2v73m955o9js3fbihf0dwei6 cat /usr/local/apache2/htdocs/index.html docker swarm nfs volume test [email protected]:~# docker volume inspect volume-nfs [ { "CreatedAt": "2019-05-15T21:11:42+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/volume-nfs/_data", "Name": "volume-nfs", "Options": { "device": ":/var/nfs", "o": "addr=10.12.31.213,rw", "type": "nfs" }, "Scope": "local" } ] [email protected]:~# docker inspect my_web.1.a2v73m955o9js3fbihf0dwei6 | 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": "" } ] [email protected]:~# docker exec -it my_web.2.uzdfouv3s2bz0ohyahjeeyqao cat /usr/local/apache2/htdocs/index.html docker swarm nfs volume test [email protected]:~# docker volume inspect volume-nfs [ { "CreatedAt": "2019-05-15T21:11:42+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/volume-nfs/_data", "Name": "volume-nfs", "Options": { "device": ":/var/nfs", "o": "addr=10.12.31.213,rw", "type": "nfs" }, "Scope": "local" } ] [email protected]:~# docker inspect my_web.2.uzdfouv3s2bz0ohyahjeeyqao | 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": "" } ]

103、Swarm如何管理存儲數據?(Swarm10)