nfs-server-provisioner部署一个nfs server,然后创建pv与nfs进行绑定,所有其他使用nfs的storageclass的pvc所动态创建的pv都会在这个pv下进行挂载。
github地址:https://github.com/helm/charts/tree/master/stable/nfs-server-provisioner
安装nfs-server-provisioner
helm repo add apphub https://apphub.aliyuncs.com
helm install nfs-server-provisioner \
--set persistence.enabled=true \
--set persistence.size=30Gi \
--set persistence.storageClass=standard \
--set storageClass.defaultClass=true \
--set nodeSelector.kubernetes\\.io/hostname=node01 \
apphub/nfs-server-provisioner
查看创建的pods
[root@master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-server-provisioner-0 0/1 Pending 0 3s
查看创建的pvc
[root@master01 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-nfs-server-provisioner-0 Pending standard 18m
查看创建的storageClass
[root@master01 ~]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs (default) cluster.local/nfs-server-provisioner Delete Immediate true 6m45s
部署后pod和pvc都处于pending状态,需要提供一个pv来做存储卷,并且这个pv要与安装之后自动生成的pvc绑定。
手动创建hostPath类型的pv:
cat > nfs-server-pv.yaml <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: data-nfs-server-provisioner-0
spec:
capacity:
storage: 30Gi
accessModes:
- ReadWriteOnce
hostPath:
## 绑定在node上的位置
path: /data/k8s/volumes/data-nfs-server-provisioner-0
claimRef:
namespace: default
## 自动生成的pvc名字
name: data-nfs-server-provisioner-0
EOF
执行yaml
kubectl apply -f nfs-server-pv.yml
再次查看pod和pvc
[root@master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-server-provisioner-0 1/1 Running 0 6m38s
[root@master01 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-nfs-server-provisioner-0 Bound data-nfs-server-provisioner-0 30Gi RWO standard 6m39s
创建测试pod
$ cat test-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox
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: nfs-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
storageClassName: "nfs"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
执行yaml文件
kubectl apply -f test-pod.yaml
查看自动创建的pvc和pv
[root@master01 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-nfs-server-provisioner-0 Bound data-nfs-server-provisioner-0 30Gi RWO standard 36m
nfs-pvc Bound pvc-2d1eb647-5efb-44ab-a761-73ae9763b2f9 100Mi RWO nfs 4m21s
[root@master01 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
data-nfs-server-provisioner-0 30Gi RWO Retain Bound default/data-nfs-server-provisioner-0 14m
pvc-2d1eb647-5efb-44ab-a761-73ae9763b2f9 100Mi RWO Delete Bound default/nfs-pvc nfs 4m23s
在node01验证写入数据成功
[root@node01 ~]# cd /data/k8s/volumes/data-nfs-server-provisioner-0
[root@node01 data-nfs-server-provisioner-0]# ls pvc-2d1eb647-5efb-44ab-a761-73ae9763b2f9/
SUCCESS