存储类
storageclass连接存储插件,根据PVC的消费动态生成PV
一、开启NFS
[root@master ~]# yum -nd [root@master ~]# cat /etc/exports /nfsdata *(rw,sync,no_root_squash) [root@master ~]# systemctl start rpcbind [root@master ~]# systemctl start nfs-server [root@master ~]# systemctl enable rpcbind [root@master ~]# systemctl enable nfs-server [root@master ~]# showmount -e
PS: 不要忘了,node节点也要下载 nfs-utils,否则将不能挂载。
二、开启rbac权限
RBAC基于角色的访问控制--全拼Role-Based Access Control
[root@master ~]# vim rbac-rolebind.yaml kind: Namespace apiVersion: v1 metadata: name: bdqn --- apiVersion: v1 kind: ServiceAccount metadata: name: nfs-provisioner namespace: bdqn --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: nfs-provisioner-runner namespace: bdqn rules: - apiGroups: [""] resources: ["persistentvolumes"] verbs: ["get", "list", "watch", "create", "delete"] - apiGroups: [""] resources: ["persistentvolumeclaims"] verbs: ["get", "list", "watch", "update"] - apiGroups: ["storage.k8s.io"] resources: ["storageclasses"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["events"] verbs: ["watch", "create", "update", "patch"] - apiGroups: [""] resources: ["services", "endpoints"] verbs: ["get","create","list", "watch","update"] - apiGroups: ["extensions"] resources: ["podsecuritypolicies"] resourceNames: ["nfs-provisioner"] verbs: ["use"] --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: run-nfs-provisioner subjects: - kind: ServiceAccount name: nfs-provisioner namespace: bdqn roleRef: kind: ClusterRole name: nfs-provisioner-runner apiGroup: rbac.authorization.k8s.io
三、创建nfs-deployment.yaml
[root@master sc]# vim nfs-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nfs-client-provisioner namespace: bdqn spec: selector: matchLabels: app: nfs-client-provisioner replicas: 1 strategy: type: Recreate template: metadata: labels: app: nfs-client-provisioner spec: serviceAccount: nfs-provisioner containers: - name: nfs-client-provisioner image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner imagePullPolicy: IfNotPresent volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: bdqn-test - name: NFS_SERVER value: 192.168.8.10 - name: NFS_PATH value: /nfsdata volumes: - name: nfs-client-root nfs: server: 192.168.8.10 path: /nfsdata
PS: nfs-client-provisioner这个镜像的作用,它通过k8s集群内置的NFS驱动,挂载远端的NFS服务器到本地目录(这里的本地目录指的是容器内的目录),然后将自身作为storage provisioner,关联到storageclass。
并且暴露了一个环境变量
env:
name: PROVISIONER_NAME #提供者的名称 重要! 记住这个名字
value: bdqn-test #真正提供存储的是bdqn-test
name: NFS_SERVER #用的是nfs服务
注:如果nfs-client-provisioner noready,执行iptables -F
四、创建storageclass资源
kubectl explain sc #可以查看一下sc的帮助 [root@master ~]# vim storageclass.yaml #创建一个最简略的sc资源一共7行 apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: storageclass namespace: bdqn #所在名称空间 provisioner: bdqn-test #存储提供者是谁 需要和上段env里指定的name下的 value:bdqn-test一致 reclaimPolicy: Retain [root@master ~]# kubectl get sc #不受名称空间影响,可以直接查到 [root@master ~]# kubectl describe sc
验证:最后创建一个pvc资源对象,看能否达到自动创建pv的效果
五、创建PVC验证
[root@master sc]# vim test-pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: test-pvc #给pod资源或其他资源管理用的名称 namespace: bdqn spec: storageClassName: storageclass accessModes: - ReadWriteOnce resources: requests: storage: 200Mi #设置存储大小 [root@master ~]# kubectl get pvc -n bdqn [root@master ~]# kubectl get pod -n bdqn #查到pod的名称 [root@master ~]# ls /nfsdata/ 1.txt bdqn-test-pvc-pvc-ebca0cbf-6bd9-4b49-9967-09e266de9ea4 [root@master ~]# kubectl exec -it -n bdqn nfs-client-provisioner-f645cc7-rznbs sh / #ls /persistentvolumes/
六、创建一个Pod测试
vim test-pod.yaml apiVersion: v1 kind: Pod metadata: name: test-pod namespace: bdqn spec: containers: - name: test-pod image: busybox args: - /bin/sh - -c - sleep 3000 volumeMounts: - name: nfs-pv mountPath: /test #随便自定义一个目录即可 volumes: - name: nfs-pv persistentVolumeClaim: claimName: test-pvc #关联上我们上面创建的pvc名字 [root@master ~]# kubectl apply -f test-pod.yaml pod/test-pod created [root@master ~]# kubectl get pod -n bdqn [root@master ~]# ls /nfsdata/ 1.txt bdqn-test-pvc-pvc-ebca0cbf-6bd9-4b49-9967-09e266de9ea4 [root@master sc]# echo 123456 > /nfsdata/bdqn-test-pvc-pvc-ebca0cbf-6bd9-4b49- 9967-09e266de9ea4/test.txt [root@master ~]# kubectl exec -n bdqn test-pod cat /test/test.txt 123456