当前位置:首页 > 问答 > 正文

Kubernetes GlusterFS 实践经验:基于 GlusterFS 构建 Kubernetes 存储卷集群

Kubernetes + GlusterFS实战:打造稳定高效的存储卷集群

(2025年7月最新动态:随着Kubernetes 1.30版本的发布,对CSI驱动支持更加完善,GlusterFS社区也同步更新了其Kubernetes插件,显著提升了动态卷配置的性能和稳定性)

为什么选择GlusterFS作为Kubernetes存储方案?

兄弟们,搞K8s存储选型的时候,GlusterFS绝对是个值得认真考虑的选择,这玩意儿在分布式存储领域摸爬滚打这么多年,有几个特别吸引人的特点:

  1. 完全开源:不像某些商业方案藏着掖着,出了问题连日志都看不懂
  2. 无单点故障:数据自动分布在多个节点上,挂掉一两个节点根本不慌
  3. 横向扩展简单:加机器就能扩容,特别适合业务快速增长的环境
  4. 协议兼容性好:支持NFS/SMB等多种协议,迁移成本低

我们团队去年把生产环境的存储从本地卷迁移到GlusterFS后,最直观的感受就是运维压力小了很多,以前半夜经常被存储报警叫醒的日子终于结束了!

环境准备:硬件配置建议

先说说硬件这块,别到时候性能不行怪方案不好,根据我们的踩坑经验:

测试环境

  • 至少3个节点(物理机或VM都行)
  • 每个节点4核CPU/8GB内存起步
  • 网络建议10Gbps,实在不行千兆也能凑合
  • 每节点至少100GB存储(SSD更好)

生产环境

  • 5节点起步(奇数个利于仲裁)
  • 8核CPU/32GB内存是底线
  • 必须万兆网络
  • 企业级SSD强烈推荐(特别是元数据服务)

特别提醒:千万别用云厂商的超融合存储!我们吃过亏,性能差到怀疑人生。

手把手安装配置GlusterFS集群

1 基础安装

假设我们有3个节点:k8s-node1、k8s-node2、k8s-node3

在每个节点上执行:

# Ubuntu/Debian系
sudo apt update && sudo apt install -y glusterfs-server
sudo systemctl enable --now glusterd
# RHEL/CentOS系
sudo yum install -y centos-release-gluster
sudo yum install -y glusterfs-server
sudo systemctl enable --now glusterd

2 组建存储池

在任意一个节点上执行:

gluster peer probe k8s-node2
gluster peer probe k8s-node3

检查状态:

Kubernetes GlusterFS 实践经验:基于 GlusterFS 构建 Kubernetes 存储卷集群

gluster peer status

应该能看到其他两个节点都是"Connected"状态。

3 创建存储卷

这里我们创建一个复制卷(Replicated Volume),确保数据有冗余:

gluster volume create k8s-vol replica 3 \
  k8s-node1:/data/glusterfs/k8s-vol \
  k8s-node2:/data/glusterfs/k8s-vol \
  k8s-node3:/data/glusterfs/k8s-vol \
  force
gluster volume start k8s-vol

验证一下:

gluster volume info

Kubernetes侧集成

1 安装必要组件

在所有K8s节点安装客户端:

apt install -y glusterfs-client  # 或yum install

2 部署GlusterFS Kubernetes插件

创建glusterfs-endpoints.yaml:

apiVersion: v1
kind: Endpoints
metadata:
  name: glusterfs-cluster
  namespace: default
subsets:
- addresses:
  - ip: 10.0.0.101  # node1 IP
  - ip: 10.0.0.102  # node2 IP
  - ip: 10.0.0.103  # node3 IP
  ports:
  - port: 49152  # GlusterFS默认端口
    protocol: TCP

然后创建service(虽然不真正使用,但K8s要求):

apiVersion: v1
kind: Service
metadata:
  name: glusterfs-cluster
  namespace: default
spec:
  ports:
  - port: 49152
    protocol: TCP
    targetPort: 49152

应用配置:

kubectl apply -f glusterfs-endpoints.yaml
kubectl apply -f glusterfs-service.yaml

3 创建StorageClass

glusterfs-storageclass.yaml:

Kubernetes GlusterFS 实践经验:基于 GlusterFS 构建 Kubernetes 存储卷集群

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: glusterfs-sc
provisioner: kubernetes.io/glusterfs
parameters:
  resturl: "http://glusterfs-cluster:49152"
  restauthenabled: "false"
  volumetype: "replicate:3"

应用它:

kubectl apply -f glusterfs-storageclass.yaml

实战应用示例

1 动态创建PVC

创建pvc.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: glusterfs-pvc
spec:
  storageClassName: glusterfs-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

应用后检查:

kubectl get pvc

2 在Pod中使用

创建pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: glusterfs-test
spec:
  containers:
  - name: test-container
    image: nginx
    volumeMounts:
    - name: glusterfs-vol
      mountPath: /usr/share/nginx/html
  volumes:
  - name: glusterfs-vol
    persistentVolumeClaim:
      claimName: glusterfs-pvc

性能调优经验分享

经过一年多的生产环境运行,我们总结出几个关键优化点:

  1. 客户端缓存:在Pod所在节点调整内核参数

    echo 15 > /proc/sys/vm/vfs_cache_pressure
  2. 卷参数优化

    gluster volume set k8s-vol performance.cache-size 2GB
    gluster volume set k8s-vol performance.io-thread-count 16
  3. 网络优化:确保启用了巨帧(MTU 9000)

    Kubernetes GlusterFS 实践经验:基于 GlusterFS 构建 Kubernetes 存储卷集群

  4. 监控建议

    • 定期检查heal状态:gluster volume heal k8s-vol info
    • 监控inode使用情况(我们曾经因为inode耗尽导致故障)

常见故障处理

1 脑裂问题处理

当网络分区时可能会遇到脑裂,处理步骤:

gluster volume heal k8s-vol full
gluster volume set k8s-vol cluster.quorum-type auto

2 节点故障恢复

假设node3挂了:

  1. 先标记节点:gluster peer detach k8s-node3
  2. 修复后重新加入:gluster peer probe k8s-node3
  3. 触发数据同步:gluster volume heal k8s-vol full

3 PVC无法删除

有时候会遇到Terminating状态的PVC卡住:

kubectl patch pvc your-pvc -n your-ns -p '{"metadata":{"finalizers":null}}'

写在最后

说实话,刚开始用GlusterFS+K8s组合时,我们团队也是各种踩坑,但坚持下来后发现这确实是性价比超高的方案——既不用花大价钱买商业存储,又能获得不错的性能和可靠性。

特别提醒两点:

  1. 一定要做好监控:GlusterFS自己的metrics加上Prometheus监控
  2. 定期演练故障恢复:模拟节点宕机、网络分区等情况

存储系统就像房子的地基,前期多花点时间打好基础,后面业务发展才能稳如磐石,希望我们的经验能帮你少走弯路!

发表评论