使用 glusterfs 做持久化存储

我们复用 kubernetes 的三台主机做 glusterfs 存储。

安装 glusterfs

我们直接在物理机上使用 yum 安装,如果你选择在 kubernetes 上安装,请参考:https://github.com/gluster/gluster-kubernetes/blob/master/docs/setup-guide.md

  1. # 先安装 gluster 源
  2. $ yum install centos-release-gluster -y
  3. # 安装 glusterfs 组件
  4. $ yum install -y glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma glusterfs-geo-replication glusterfs-devel
  5. ## 创建 glusterfs 目录
  6. $ mkdir /opt/glusterd
  7. ## 修改 glusterd 目录
  8. $ sed -i 's/var\/lib/opt/g' /etc/glusterfs/glusterd.vol
  9. # 启动 glusterfs
  10. $ systemctl start glusterd.service
  11. # 设置开机启动
  12. $ systemctl enable glusterd.service
  13. #查看状态
  14. $ systemctl status glusterd.service

配置 glusterfs

  1. # 配置 hosts
  2. $ vi /etc/hosts
  3. 172.20.0.113 sz-pg-oam-docker-test-001.tendcloud.com
  4. 172.20.0.114 sz-pg-oam-docker-test-002.tendcloud.com
  5. 172.20.0.115 sz-pg-oam-docker-test-003.tendcloud.com
  1. # 开放端口
  2. $ iptables -I INPUT -p tcp --dport 24007 -j ACCEPT
  3. # 创建存储目录
  4. $ mkdir /opt/gfs_data
  1. # 添加节点到 集群
  2. # 执行操作的本机不需要 probe 本机
  3. [root@sz-pg-oam-docker-test-001 ~]#
  4. gluster peer probe sz-pg-oam-docker-test-002.tendcloud.com
  5. gluster peer probe sz-pg-oam-docker-test-003.tendcloud.com
  6. # 查看集群状态
  7. $ gluster peer status
  8. Number of Peers: 2
  9. Hostname: sz-pg-oam-docker-test-002.tendcloud.com
  10. Uuid: f25546cc-2011-457d-ba24-342554b51317
  11. State: Peer in Cluster (Connected)
  12. Hostname: sz-pg-oam-docker-test-003.tendcloud.com
  13. Uuid: 42b6cad1-aa01-46d0-bbba-f7ec6821d66d
  14. State: Peer in Cluster (Connected)

配置 volume

GlusterFS 中的 volume 的模式有很多种,包括以下几种:

  • 分布卷(默认模式):即 DHT, 也叫 分布卷: 将文件以 hash 算法随机分布到 一台服务器节点中存储。
  • 复制模式 :即 AFR, 创建 volume 时带 replica x 数量: 将文件复制到 replica x 个节点中。
  • 条带模式 :即 Striped, 创建 volume 时带 stripe x 数量: 将文件切割成数据块,分别存储到 stripe x 个节点中 (类似 raid 0)。
  • 分布式条带模式 :最少需要 4 台服务器才能创建。 创建 volume 时 stripe 2 server = 4 个节点: 是 DHT 与 Striped 的组合型。
  • 分布式复制模式 :最少需要 4 台服务器才能创建。 创建 volume 时 replica 2 server = 4 个节点:是 DHT 与 AFR 的组合型。
  • 条带复制卷模式 :最少需要 4 台服务器才能创建。 创建 volume 时 stripe 2 replica 2 server = 4 个节点: 是 Striped 与 AFR 的组合型。
  • 三种模式混合 : 至少需要 8 台 服务器才能创建。 stripe 2 replica 2 , 每 4 个节点 组成一个 组。

这几种模式的示例图参考:CentOS7 安装 GlusterFS

因为我们只有三台主机,在此我们使用默认的 分布卷模式 请勿在生产环境上使用该模式,容易导致数据丢失。

  1. # 创建分布卷
  2. $ gluster volume create k8s-volume transport tcp sz-pg-oam-docker-test-001.tendcloud.com:/opt/gfs_data sz-pg-oam-docker-test-002.tendcloud.com:/opt/gfs_data sz-pg-oam-docker-test-003.tendcloud.com:/opt/gfs_data force
  3. # 查看 volume 状态
  4. $ gluster volume info
  5. Volume Name: k8s-volume
  6. Type: Distribute
  7. Volume ID: 9a3b0710-4565-4eb7-abae-1d5c8ed625ac
  8. Status: Created
  9. Snapshot Count: 0
  10. Number of Bricks: 3
  11. Transport-type: tcp
  12. Bricks:
  13. Brick1: sz-pg-oam-docker-test-001.tendcloud.com:/opt/gfs_data
  14. Brick2: sz-pg-oam-docker-test-002.tendcloud.com:/opt/gfs_data
  15. Brick3: sz-pg-oam-docker-test-003.tendcloud.com:/opt/gfs_data
  16. Options Reconfigured:
  17. transport.address-family: inet
  18. nfs.disable: on
  19. # 启动 分布卷
  20. $ gluster volume start k8s-volume

Glusterfs 调优

  1. # 开启 指定 volume 的配额
  2. $ gluster volume quota k8s-volume enable
  3. # 限制 指定 volume 的配额
  4. $ gluster volume quota k8s-volume limit-usage / 1TB
  5. # 设置 cache 大小, 默认 32MB
  6. $ gluster volume set k8s-volume performance.cache-size 4GB
  7. # 设置 io 线程, 太大会导致进程崩溃
  8. $ gluster volume set k8s-volume performance.io-thread-count 16
  9. # 设置 网络检测时间, 默认 42s
  10. $ gluster volume set k8s-volume network.ping-timeout 10
  11. # 设置 写缓冲区的大小, 默认 1M
  12. $ gluster volume set k8s-volume performance.write-behind-window-size 1024MB

Kubernetes 中配置 glusterfs

官方的文档见:https://github.com/kubernetes/kubernetes/tree/master/examples/volumes/glusterfs

以下用到的所有 yaml 和 json 配置文件可以在 glusterfs 中找到。注意替换其中私有镜像地址为你自己的镜像地址。

kubernetes 安装客户端

  1. # 在所有 k8s node 中安装 glusterfs 客户端
  2. $ yum install -y glusterfs glusterfs-fuse
  3. # 配置 hosts
  4. $ vi /etc/hosts
  5. 172.20.0.113 sz-pg-oam-docker-test-001.tendcloud.com
  6. 172.20.0.114 sz-pg-oam-docker-test-002.tendcloud.com
  7. 172.20.0.115 sz-pg-oam-docker-test-003.tendcloud.com

因为我们 glusterfs 跟 kubernetes 集群复用主机,因为此这一步可以省去。

配置 endpoints

  1. $ curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-endpoints.json
  2. # 修改 endpoints.json ,配置 glusters 集群节点 ip
  3. # 每一个 addresses 为一个 ip 组
  4. {
  5. "addresses": [
  6. {
  7. "ip": "172.22.0.113"
  8. }
  9. ],
  10. "ports": [
  11. {
  12. "port": 1990
  13. }
  14. ]
  15. },
  16. # 导入 glusterfs-endpoints.json
  17. $ kubectl apply -f glusterfs-endpoints.json
  18. # 查看 endpoints 信息
  19. $ kubectl get ep

配置 service

  1. $ curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-service.json
  2. # service.json 里面查找的是 enpointes 的名称与端口,端口默认配置为 1,我改成了 1990
  3. # 导入 glusterfs-service.json
  4. $ kubectl apply -f glusterfs-service.json
  5. # 查看 service 信息
  6. $ kubectl get svc

创建测试 pod

  1. $ curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-pod.json
  2. # 编辑 glusterfs-pod.json
  3. # 修改 volumes 下的 path 为上面创建的 volume 名称
  4. "path": "k8s-volume"
  5. # 导入 glusterfs-pod.json
  6. $ kubectl apply -f glusterfs-pod.json
  7. # 查看 pods 状态
  8. $ kubectl get pods
  9. NAME READY STATUS RESTARTS AGE
  10. glusterfs 1/1 Running 0 1m
  11. # 查看 pods 所在 node
  12. $ kubectl describe pods/glusterfs
  13. # 登陆 node 物理机,使用 df 可查看挂载目录
  14. $ df -h
  15. 172.20.0.113:k8s-volume 1.0T 0 1.0T 0% /var/lib/kubelet/pods/3de9fc69-30b7-11e7-bfbd-8af1e3a7c5bd/volumes/kubernetes.io~glusterfs/glusterfsvol

配置 PersistentVolume

PersistentVolume(PV)和 PersistentVolumeClaim(PVC)是 kubernetes 提供的两种 API 资源,用于抽象存储细节。管理员关注于如何通过 pv 提供存储功能而无需关注用户如何使用,同样的用户只需要挂载 PVC 到容器中而不需要关注存储卷采用何种技术实现。

PVC 和 PV 的关系跟 pod 和 node 关系类似,前者消耗后者的资源。PVC 可以向 PV 申请指定大小的存储资源并设置访问模式。

PV 属性

  • storage 容量
  • 读写属性:分别为 ReadWriteOnce:单个节点读写; ReadOnlyMany:多节点只读 ; ReadWriteMany:多节点读写
  1. $ cat glusterfs-pv.yaml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: gluster-dev-volume
  6. spec:
  7. capacity:
  8. storage: 8Gi
  9. accessModes:
  10. - ReadWriteMany
  11. glusterfs:
  12. endpoints: "glusterfs-cluster"
  13. path: "k8s-volume"
  14. readOnly: false
  15. # 导入 PV
  16. $ kubectl apply -f glusterfs-pv.yaml
  17. # 查看 pv
  18. $ kubectl get pv
  19. NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
  20. gluster-dev-volume 8Gi RWX Retain Available 3s

PVC 属性

  • 访问属性与 PV 相同
  • 容量:向 PV 申请的容量 <= PV 总容量

配置 PVC

  1. $ cat glusterfs-pvc.yaml
  2. kind: PersistentVolumeClaim
  3. apiVersion: v1
  4. metadata:
  5. name: glusterfs-nginx
  6. spec:
  7. accessModes:
  8. - ReadWriteMany
  9. resources:
  10. requests:
  11. storage: 8Gi
  12. # 导入 pvc
  13. $ kubectl apply -f glusterfs-pvc.yaml
  14. # 查看 pvc
  15. $ kubectl get pv
  16. NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE
  17. glusterfs-nginx Bound gluster-dev-volume 8Gi RWX 4s

创建 nginx deployment 挂载 volume

  1. $ vi nginx-deployment.yaml
  2. apiVersion: extensions/v1beta1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-dm
  6. spec:
  7. replicas: 2
  8. template:
  9. metadata:
  10. labels:
  11. name: nginx
  12. spec:
  13. containers:
  14. - name: nginx
  15. image: nginx:alpine
  16. imagePullPolicy: IfNotPresent
  17. ports:
  18. - containerPort: 80
  19. volumeMounts:
  20. - name: gluster-dev-volume
  21. mountPath: "/usr/share/nginx/html"
  22. volumes:
  23. - name: gluster-dev-volume
  24. persistentVolumeClaim:
  25. claimName: glusterfs-nginx
  26. # 导入 deployment
  27. $ kubectl apply -f nginx-deployment.yaml
  28. # 查看 deployment
  29. $ kubectl get pods |grep nginx-dm
  30. nginx-dm-3698525684-g0mvt 1/1 Running 0 6s
  31. nginx-dm-3698525684-hbzq1 1/1 Running 0 6s
  32. # 查看 挂载
  33. $ kubectl exec -it nginx-dm-3698525684-g0mvt -- df -h|grep k8s-volume
  34. 172.20.0.113:k8s-volume 1.0T 0 1.0T 0% /usr/share/nginx/html
  35. # 创建文件 测试
  36. $ kubectl exec -it nginx-dm-3698525684-g0mvt -- touch /usr/share/nginx/html/index.html
  37. $ kubectl exec -it nginx-dm-3698525684-g0mvt -- ls -lt /usr/share/nginx/html/index.html
  38. -rw-r--r-- 1 root root 0 May 4 11:36 /usr/share/nginx/html/index.html
  39. # 验证 glusterfs
  40. # 因为我们使用分布卷,所以可以看到某个节点中有文件
  41. [root@sz-pg-oam-docker-test-001 ~] ls /opt/gfs_data/
  42. [root@sz-pg-oam-docker-test-002 ~] ls /opt/gfs_data/
  43. index.html
  44. [root@sz-pg-oam-docker-test-003 ~] ls /opt/gfs_data/

参考