K8s 安装 NFS Subdir External Provisioner

  • NFS subdir external provisioner is an automatic provisioner that use your existing and already configured NFS server to support dynamic provisioning of Kubernetes Persistent Volumes via Persistent Volume Claims. Persistent volumes are provisioned as ${namespace}-${pvcName}-${pvName}

1. 环境准备

2. 安装 Provisioner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
$ helm show values nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
$ helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
--set nfs.server=10.4.7.201 \
--set nfs.path=/data/nfs \
--set image.repository=registry.cn-hangzhou.aliyuncs.com/iuxt/nfs-subdir-external-provisioner

# 查看
$ kubectl get pod nfs-subdir-external-provisioner-68fff55cc9-s7msb -o yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: nfs-subdir-external-provisioner
name: nfs-subdir-external-provisioner-68fff55cc9-s7msb
spec:
containers:
- env:
# 必须与class.yaml中的provisioner的名称一致
- name: PROVISIONER_NAME
value: cluster.local/nfs-subdir-external-provisioner
# NFS服务器的ip地址
- name: NFS_SERVER
value: 10.4.7.201
# 共享挂载目录
- name: NFS_PATH
value: /data/nfs
image: registry.cn-hangzhou.aliyuncs.com/iuxt/nfs-subdir-external-provisioner:v4.0.2
name: nfs-subdir-external-provisioner
volumes:
- name: nfs-subdir-external-provisioner-root
nfs:
path: /data/nfs
server: 10.4.7.201
......
$ kubectl get sc nfs-client -o yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
annotations:
meta.helm.sh/release-name: nfs-subdir-external-provisioner
meta.helm.sh/release-namespace: default
name: nfs-client
provisioner: cluster.local/nfs-subdir-external-provisioner
allowVolumeExpansion: true
parameters:
archiveOnDelete: "true"
reclaimPolicy: Delete
volumeBindingMode: Immediate
......

3. 验证

  • test-claim.yaml
1
2
3
4
5
6
7
8
9
10
11
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
spec:
storageClassName: nfs-client
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi
  • test-pod.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:stable
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim
  • 验证
1
2
3
4
5
6
7
8
9
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-claim Bound pvc-1787429e-542b-451f-8a48-7b158bc4ccab 1Mi RWX nfs-client 2h
$ kubectl get pod test-pod
NAME READY STATUS RESTARTS AGE
test-pod 0/1 Completed 0 2h
$ ll /data/nfs/default-test-claim-pvc-1787429e-542b-451f-8a48-7b158bc4ccab/
total 0
-rw-r--r-- 1 nobody nogroup 0 Oct 7 15:21 SUCCESS

参考