1. 程式人生 > >使用 GlusterFS 做 Docker Swarm 持久化儲存

使用 GlusterFS 做 Docker Swarm 持久化儲存

Swarm Storage Glusterfs

  • 說明
系統:CentOS-7.6 1810 Minimal

基於此環境之上 https://my.oschina.net/yx571304/blog/3113758
  • glusterfs-server 安裝
for node in node01 node02 node03; do
    ssh -T $node <<'EOF'
      # 新增軟體倉庫
      yum install -y centos-release-gluster bash-completion

      # 安裝 glusterfs-server
      yum install -y glusterfs-server

      # 啟動服務跟隨系統啟動
      systemctl start glusterd
      systemctl enable glusterd
      
      # 建立資料目錄
      mkdir -p /gluster/data
EOF
done
  • 配置
# 配置信任池
gluster peer probe node02
gluster peer probe node03

# 檢查信任池狀態
gluster peer status
netstat -antp | grep glusterd

# 建立複製卷
gluster volume create swarm-vols replica 3 node01:/gluster/data node02:/gluster/data node03:/gluster/data force

# 設定許可權 僅允許從本地掛載
gluster volume set swarm-vols auth.allow 127.0.0.1

# 啟動
gluster volume start swarm-vols
gluster volume status swarm-vols
gluster volume info

# 掛載
for node in node01 node02 node03; do
    ssh -T $node <<'EOF'
        mkdir -p /swarm/volumes
        mount.glusterfs localhost:/swarm-vols /swarm/volumes
EOF
done
  • 驗證
# node02 啟動 nginx-alpine
mkdir /swarm/volumes/nginx
docker service create \
  --name nginx \
  --constraint 'node.hostname == node02' \
  --mount type=bind,source=/swarm/volumes/nginx,target=/usr/share/nginx/html \
  --publish 8080:80 \
  nginx:alpine

docker service ps nginx
docker service scale nginx=2
ssh -T node02 'echo $(hostname -f) $(hostname -i) > /swarm/volumes/nginx/index.html'
curl http://127.0.0.1:8080

# 更改容器策略使容器在 node03 節點啟動
docker service update --constraint-rm 'node.hostname == node02' nginx
docker service update --constraint-add 'node.hostname == node03' nginx
curl http://127.0.0.1:8080

# 刪除
docker servi