将ceph文件存储挂载给k8s使用
·
要将 Ceph 文件存储(CephFS)挂载到 Kubernetes 集群中使用,需通过CephFS-CSI 插件实现动态存储管理,支持 Pod 以ReadWriteMany模式共享存储。以下是详细步骤:
一、前提条件
- 已部署 CephFS 集群(参考之前的部署步骤),且存在文件系统(如
mycephfs) - K8s 集群(1.24 + 版本)已运行,
kubectl工具配置正常 - 获取 Ceph 集群关键信息:
- 集群 ID(
fsid):在 Ceph 节点执行ceph mon dump | grep fsid | awk '{print $2}' - Monitor 地址:如
192.168.1.101:6789,192.168.1.102:6789,192.168.1.301:6789 - CephFS 名称:如
mycephfs - 数据池名称:如
cephfs-data
- 集群 ID(
二、部署 CephFS-CSI 插件(K8s 主节点执行)
1. 创建 Ceph 密钥 Secret
在 K8s 中存储 Ceph 管理员密钥,用于 CSI 插件访问 Ceph 集群:
yaml
# 创建cephfs-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: cephfs-admin-secret
namespace: kube-system
type: kubernetes.io/rook-cephfs
data:
# 替换为Ceph admin用户的密钥(base64编码)
key: $(ceph auth get-key client.admin | base64)
bash
# 执行创建(会自动替换密钥)
envsubst < cephfs-secret.yaml | kubectl apply -f -
2. 部署 CephFS-CSI 插件
使用官方 CSI 插件部署文件存储驱动:
bash
# 创建工作目录
mkdir -p ~/cephfs-csi && cd ~/cephfs-csi
# 下载CSI配置文件(v3.9.0稳定版)
wget https://raw.githubusercontent.com/ceph/ceph-csi/v3.9.0/deploy/cephfs/kubernetes/csi-cephfsplugin.yaml
wget https://raw.githubusercontent.com/ceph/ceph-csi/v3.9.0/deploy/cephfs/kubernetes/csi-cephfsplugin-provisioner.yaml
wget https://raw.githubusercontent.com/ceph/ceph-csi/v3.9.0/deploy/cephfs/kubernetes/csi-cephfsplugin-node.yaml
3. 修改 CSI 配置(关键步骤)
编辑csi-cephfsplugin-provisioner.yaml,替换以下参数(搜索并替换):
yaml
# 在provisioner容器的env中添加
- name: CLUSTER_ID
value: "替换为Ceph集群ID(fsid)"
- name: MONITORS
value: "192.168.1.101:6789,192.168.1.102:6789,192.168.1.301:6789" # 替换为实际Monitor地址
# 在volumeMounts中确保挂载密钥
- name: cephfs-csi-config
mountPath: /etc/ceph-csi-config
- name: cephfs-admin-secret
mountPath: /etc/ceph/admin-secret
# 在volumes中添加密钥卷
- name: cephfs-admin-secret
secret:
secretName: cephfs-admin-secret
4. 部署 CSI 组件
bash
# 部署CSI插件
kubectl apply -f csi-cephfsplugin.yaml
kubectl apply -f csi-cephfsplugin-provisioner.yaml
kubectl apply -f csi-cephfsplugin-node.yaml
# 验证部署(所有Pod状态应为Running)
kubectl get pods -n kube-system | grep cephfs
三、创建 CephFS 存储类(StorageClass)
定义存储类用于动态创建 CephFS 卷:
yaml
# 创建storageclass-cephfs.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: cephfs-sc
provisioner: cephfs.csi.ceph.com
parameters:
clusterID: "替换为Ceph集群ID"
fsName: "mycephfs" # CephFS文件系统名称
pool: "cephfs-data" # 数据池名称
# 密钥配置
csi.storage.k8s.io/provisioner-secret-name: cephfs-admin-secret
csi.storage.k8s.io/provisioner-secret-namespace: kube-system
csi.storage.k8s.io/node-stage-secret-name: cephfs-admin-secret
csi.storage.k8s.io/node-stage-secret-namespace: kube-system
reclaimPolicy: Delete # 删除PVC时自动删除卷
allowVolumeExpansion: true # 支持卷扩容
mountOptions:
- discard # 启用TRIM
- noatime # 禁用访问时间记录(优化性能)
bash
kubectl apply -f storageclass-cephfs.yaml
# 验证存储类
kubectl get sc cephfs-sc
四、创建 PVC 并挂载到 Pod
通过 PVC 申请 CephFS 存储,并在 Pod 中使用。
1. 创建 PVC(持久卷声明)
yaml
# 创建cephfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cephfs-pvc
spec:
accessModes:
- ReadWriteMany # 多Pod共享读写(CephFS核心优势)
resources:
requests:
storage: 10Gi # 申请10GB存储(根据需求调整)
storageClassName: cephfs-sc # 关联存储类
bash
kubectl apply -f cephfs-pvc.yaml
# 验证PVC状态(应为Bound)
kubectl get pvc cephfs-pvc
2. 部署 Pod 使用 PVC
yaml
# 创建cephfs-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: cephfs-demo-pod-1
spec:
containers:
- name: demo-container
image: nginx:alpine
volumeMounts:
- name: cephfs-volume
mountPath: /data # 容器内挂载路径
volumes:
- name: cephfs-volume
persistentVolumeClaim:
claimName: cephfs-pvc # 引用PVC
---
# 再创建一个Pod共享同一存储
apiVersion: v1
kind: Pod
metadata:
name: cephfs-demo-pod-2
spec:
containers:
- name: demo-container
image: nginx:alpine
volumeMounts:
- name: cephfs-volume
mountPath: /data
volumes:
- name: cephfs-volume
persistentVolumeClaim:
claimName: cephfs-pvc
bash
kubectl apply -f cephfs-pod.yaml
# 验证Pod状态(应为Running)
kubectl get pods | grep cephfs-demo
五、验证 CephFS 共享功能
bash
# 在第一个Pod中创建测试文件
kubectl exec -it cephfs-demo-pod-1 -- sh -c "echo 'shared content' > /data/test.txt"
# 在第二个Pod中验证文件共享
kubectl exec -it cephfs-demo-pod-2 -- cat /data/test.txt
# 输出应为:shared content,说明共享成功
六、关键配置说明
-
访问模式:
ReadWriteMany(RWX)是 CephFS 的核心优势,支持多 Pod 同时读写,适合日志共享、配置文件存储等场景。 -
性能优化:
mountOptions中添加discard启用 TRIM,释放未使用空间(NVMe SSD 推荐)。noatime禁用文件访问时间记录,减少元数据写入。- 如需更高性能,可在 Ceph 集群调整
mds_cache_size(元数据缓存)。
-
权限控制:
- 生产环境建议创建专用 Ceph 用户(非
admin),限制仅访问cephfs-data和cephfs-metadata池:bash
# 创建专用用户 ceph auth get-or-create client.k8s-cephfs mon 'allow r' mds 'allow rw' osd 'allow rw pool=cephfs-data, allow rw pool=cephfs-metadata' # 导出密钥并替换Secret中的key字段
- 生产环境建议创建专用 Ceph 用户(非
更多推荐


所有评论(0)