103、Swarm如何管理存储数据?(Swarm10)

 
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
 
root@host03:~# apt install -y nfs-kernel-server
root@host03:~# mkdir /var/nfs
root@host03:~# cat /etc/exports
/var/nfs * (rw,sync,no_root_squash)
root@host03:~# systemctl restart nfs-kernel-server.service
 
安装和配置 nfs-client (需要在每台host都创建一遍 volume)
 
root@host01:~# apt install -y nfs-common
root@host01:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs
root@host01:~# docker volume ls
DRIVER              VOLUME NAME
local               volume-nfs
 
root@host02:~# apt install -y nfs-common
root@host02:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs
root@host01:~# docker volume ls
DRIVER              VOLUME NAME
local               volume-nfs
 
root@host03:~# apt install -y nfs-common
root@host03:~# docker volume create --driver local --opt type=nfs --opt o=addr=10.12.31.213,rw --opt device=:/var/nfs volume-nfs
root@host01:~# docker volume ls
DRIVER              VOLUME NAME
local               volume-nfs
 
 
创建Service 并挂载nfs 的volume,并验证
 
root@host03:~# 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
 
root@host03:~# cat /var/nfs/index.html
docker swarm nfs volume test
root@host03:~# curl http://10.12.31.211
docker swarm nfs volume test
root@host03:~# curl http://10.12.31.212
docker swarm nfs volume test
root@host03:~# curl http://10.12.31.213
docker swarm nfs volume test
 
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 4 minutes ago                       
uzdfouv3s2bz        my_web.2            httpd:latest        host02              Running             Running 4 minutes ago
 
root@host01:~# docker exec -it my_web.1.a2v73m955o9js3fbihf0dwei6 cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
root@host01:~# 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"
    }
]
root@host01:~# 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": ""
  }
]
 
 
 
root@host02:~# docker exec -it my_web.2.uzdfouv3s2bz0ohyahjeeyqao cat /usr/local/apache2/htdocs/index.html
docker swarm nfs volume test
root@host02:~# 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"
    }
]
root@host02:~# 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": ""
  }
]
 
 
posted @ 2019-05-15 22:55  三角形  阅读(290)  评论(0编辑  收藏  举报