Kubernetes 安装和配置 NFS 存储卷
Kubernetes 安装和配置 NFS 存储卷(PersistentVolume) 有两种主要方式:
- 静态 PV(简单,直接挂载现有 NFS 路径,适合测试/固定使用场景)
- 动态 Provisioning(推荐生产环境,使用 StorageClass 自动创建子目录)
# 1. 前置条件:准备 NFS Server
在 NFS 服务器上(独立机器或 NAS)执行:
# Ubuntu/Debian
apt update && apt install -y nfs-kernel-server
# CentOS/RHEL
yum install -y nfs-utils
# 创建共享目录
mkdir -p /nfs/k8s
chown -R nobody:nogroup /nfs/k8s
chmod 777 /nfs/k8s
# 编辑 exports
cat <<EOF >> /etc/exports
/nfs/k8s *(rw,sync,no_subtree_check,no_root_squash)
EOF
exportfs -ra
systemctl restart nfs-kernel-server
showmount -e localhost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
重要权限:
no_root_squash方便 Kubernetes 使用。
所有 Kubernetes 节点都需要安装 NFS 客户端:
# Ubuntu
apt install -y nfs-common
# CentOS
yum install -y nfs-utils
1
2
3
4
5
2
3
4
5
# 2. 静态 NFS PV + PVC(最简单)
nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 100Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany # NFS 典型使用 RWX
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-static
nfs:
server: 192.168.1.100 # 你的 NFS Server IP
path: /nfs/k8s/mydata # NFS 导出的路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
storageClassName: nfs-static
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
kubectl apply -f nfs-pv.yaml
kubectl apply -f nfs-pvc.yaml
1
2
2
在 Pod 中挂载:
volumeMounts:
- mountPath: "/data"
name: nfs-volume
volumes:
- name: nfs-volume
persistentVolumeClaim:
claimName: nfs-pvc
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3. 动态 Provisioning(推荐)
# 推荐方案:NFS CSI Driver(Kubernetes 官方推荐,GA 状态)
# 1. 安装 CSI Driver(最新方式)
helm repo add csi-driver-nfs https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts
helm repo update
helm install csi-driver-nfs csi-driver-nfs/csi-driver-nfs \
--namespace kube-system \
--version v4.13.2
1
2
3
4
5
6
7
2
3
4
5
6
7
创建 StorageClass(nfs-csi-sc.yaml)
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
server: 192.168.1.100
share: /nfs/k8s
# subDir: "${pvc.namespace}-${pvc.name}" # 可自定义子目录格式
reclaimPolicy: Delete
volumeBindingMode: Immediate
mountOptions:
- vers=4.2
- hard
- noatime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kubectl apply -f nfs-csi-sc.yaml
1
然后直接创建 PVC(无需手动 PV):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dynamic-nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
storageClassName: nfs-csi
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 备选方案:nfs-subdir-external-provisioner(较老,但仍常用)
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
--set nfs.server=192.168.1.100 \
--set nfs.path=/nfs/k8s \
--namespace nfs-provisioner \
--create-namespace
1
2
3
4
5
6
2
3
4
5
6
StorageClass 名称默认为 nfs-client。
# 常见问题排查
- Mount failed:检查节点是否能
mount -t nfs 192.168.1.100:/nfs/k8s /mnt测试挂载 - Permission denied:NFS Server 使用
no_root_squash,并检查目录权限 - SELinux(CentOS):
setenforce 0测试,或正确配置 SELinux 上下文 - 多节点:确保所有 Worker 节点都能访问 NFS
- 查看事件:`kubectl describe pvc xxx
上次更新: 2026/05/21, 21:01:23
← etcd的备份和还原 elk安装→
|