[K8S] PV动态供给
@ 存储类(Storage Class)根据官网的存储制备器表格, NFS没有对应的内置制备器 ->https://kubernetes.io/zh/docs/concepts/storage/storage-classes/但可以使用外部制备器,代码仓库 ->https://github.com/kubernetes-sigs/nfs-subdir-external-provisio
@ 存储类(Storage Class)
根据官网的存储制备器表格, NFS没有对应的内置制备器 ->
https://kubernetes.io/zh/docs/concepts/storage/storage-classes/
但可以使用外部制备器,代码仓库 ->
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
修改deployment.yaml的NFS服务器地址和路径, 然后apply
[root@k8s-master nfs-external-provisioner]# kubectl apply -f .
storageclass.storage.k8s.io/managed-nfs-storage created
deployment.apps/nfs-client-provisioner created
serviceaccount/nfs-client-provisioner created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
[root@k8s-master nfs-external-provisioner]#
现在可以获取到存储类
[root@k8s-master nfs-external-provisioner]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage k8s-sigs.io/nfs-subdir-external-provisioner Delete Immediate false 27s
[root@k8s-master nfs-external-provisioner]#
test-deploy-sc.yaml内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-sc
spec:
selector:
matchLabels:
app: test-pv
replicas: 3
template:
metadata:
labels:
app: test-pv
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: pv1
mountPath: /usr/share/nginx/html
volumes:
- name: pv1
persistentVolumeClaim:
claimName: test-sc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-sc
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteMany
resources:
requests:
storage: 60Gi
---
apiVersion: v1
kind: Service
metadata:
name: web-sc
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: test-pv
关注其中的PVC资源, 储存需求60G, storageClassName对应nfs-external-provisioner/class.yaml
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteMany
resources:
requests:
storage: 60Gi
class.yaml内容如下
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
说明:
line 4,存储类名称 "managed-nfs-storage"
line 7,archiveOnDelete, VOLUME路径在PVC删除后是否归档 false -> 自动删除 / true -> 归档保留
@ PV动态供给
当前环境没有PV或者PVC
[root@k8s-master ~]# kubectl get pv
No resources found
[root@k8s-master ~]# kubectl get pvc
No resources found in default namespace.
[root@k8s-master ~]#
现在创建Deployment,SVC和PVC, 可以看到同时创建了一个容量60G的PV, 回收策略是Delete
[root@k8s-master ~]# kubectl apply -f test-deploy-sc.yaml
deployment.apps/web-sc created
persistentvolumeclaim/test-sc created
service/web-sc created
[root@k8s-master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae 60Gi RWX Delete Bound default/test-sc managed-nfs-storage 6s
[root@k8s-master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-sc Bound pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae 60Gi RWX managed-nfs-storage 13s
[root@k8s-master ~]#
进入Pod中的容器,在存储路径touch一个文件试试
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-5fd446cd9d-c62kl 1/1 Running 0 54m
web-sc-596569cb9f-cvj4r 1/1 Running 0 42m
web-sc-596569cb9f-plnr5 1/1 Running 0 42m
web-sc-596569cb9f-x8g9v 1/1 Running 0 42m
[root@k8s-master ~]# kubectl exec -it web-sc-596569cb9f-cvj4r -- bash
root@web-sc-596569cb9f-cvj4r:/# cd /usr/share/nginx/html
root@web-sc-596569cb9f-cvj4r:/usr/share/nginx/html# ls
root@web-sc-596569cb9f-cvj4r:/usr/share/nginx/html# touch testSC
root@web-sc-596569cb9f-cvj4r:/usr/share/nginx/html#
再到NFS服务器查看,可以看到NFS路径下多了一个文件夹 default-test-sc-pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae 对应kubectl get pvc的VOLUME列pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae,显然是自动创建的,进入改路径可以看到刚才touch的文件
[root@k8s-node2 ~]# cd /nfstest/
[root@k8s-node2 nfstest]# ls -l
total 0
drwxrwxrwx 2 root root 20 Aug 29 11:55 default-test-sc-pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae
drwxr-xr-x 2 root root 6 Aug 29 09:54 pv0001
drwxr-xr-x 2 root root 6 Aug 29 09:54 pv0002
drwxr-xr-x 2 root root 6 Aug 29 09:54 pv0003
drwxr-xr-x 2 root root 6 Aug 29 09:54 pv0004
drwxr-xr-x 2 root root 6 Aug 29 10:03 pv0005
[root@k8s-node2 nfstest]# cd default-test-sc-pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae/
[root@k8s-node2 default-test-sc-pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae]# ls
testSC
[root@k8s-node2 default-test-sc-pvc-2c936885-8d66-4e60-8a0e-8e5e2fc437ae]#
再来试试删除这个PVC,这里路径和里面的文件会被删除,对应 class.yaml的如下配置
parameters:
archiveOnDelete: "false"
[root@k8s-master ~]# kubectl delete -f test-deploy-sc.yaml
deployment.apps "web-sc" deleted
persistentvolumeclaim "test-sc" deleted
service "web-sc" deleted
[root@k8s-master ~]#
NFS:
[root@k8s-node2 nfstest]# pwd
/nfstest
[root@k8s-node2 nfstest]# ls
pv0001 pv0002 pv0003 pv0004 pv0005
[root@k8s-node2 nfstest]#
将 class.yam 配置archiveOnDelete改为"true", 为PV创建的路径和文件会保留
[root@k8s-master nfs-external-provisioner]# kubectl apply -f class.yaml
The StorageClass "managed-nfs-storage" is invalid: parameters: Forbidden: updates to parameters are forbidden.
[root@k8s-master nfs-external-provisioner]# kubectl delete -f class.yaml
storageclass.storage.k8s.io "managed-nfs-storage" deleted
[root@k8s-master nfs-external-provisioner]# kubectl apply -f class.yaml
storageclass.storage.k8s.io/managed-nfs-storage created
[root@k8s-master nfs-external-provisioner]# kubectl apply -f ../test-deploy-sc.yaml
deployment.apps/web-sc created
persistentvolumeclaim/test-sc created
service/web-sc created
[root@k8s-master nfs-external-provisioner]#
进入创建的Pod,touch文件,然后删除PVC, 可以在NFS服务器看到 目录default-<PVC NAME>-<PVC VOLUME>的名称更新为 archived-default-<PVC NAME>-<PVC VOLUME> ,即删除PVC后给数据做了归档, 原目录下的内容也保留了
k8s-master:
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-5fd446cd9d-c62kl 1/1 Running 0 66m
web-sc-596569cb9f-c5979 1/1 Running 0 55s
web-sc-596569cb9f-cl6pb 1/1 Running 0 55s
web-sc-596569cb9f-cpr88 1/1 Running 0 55s
[root@k8s-master ~]# kubectl exec -it web-sc-596569cb9f-c5979 -- bash
root@web-sc-596569cb9f-c5979:/usr# cd /usr/share/nginx/html/
root@web-sc-596569cb9f-c5979:/usr/share/nginx/html# ls
root@web-sc-596569cb9f-c5979:/usr/share/nginx/html# touch Hahahaha
root@web-sc-596569cb9f-c5979:/usr/share/nginx/html#
NFS:
[root@k8s-node2 nfstest]# ls
default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615 pv0001 pv0002 pv0003 pv0004 pv0005
[root@k8s-node2 nfstest]# cd default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615/
[root@k8s-node2 default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615]# ls
Hahahaha
[root@k8s-node2 default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615]# cd ..
[root@k8s-node2 nfstest]#
k8s-master:
[root@k8s-master ~]# kubectl delete -f test-deploy-sc.yaml
deployment.apps "web-sc" deleted
persistentvolumeclaim "test-sc" deleted
service "web-sc" deleted
[root@k8s-master ~]#
NFS:
[root@k8s-node2 nfstest]# ls
archived-default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615 pv0001 pv0002 pv0003 pv0004 pv0005
[root@k8s-node2 nfstest]# cd archived-default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615/
[root@k8s-node2 archived-default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615]# ls
Hahahaha
[root@k8s-node2 archived-default-test-sc-pvc-7b7c6f87-95e2-4d58-97af-b06723154615]#
更多推荐
所有评论(0)