K8s进阶之PersistentVolumeClaim 动态供给
先来简单看一下这张图实现的过程,而后咱们再来研究一下html
说在前面的话,静态供给的话,会须要咱们手动去建立pv,若是没有足够的资源,找不到合适的pv,那么pod就会处于pending等待的状态,就是说找不到合适的伴侣了, 因此解决这两种问题,就给出了这种动态供给,主要是可以自动帮你建立pv ,就是你须要多大的容量,就自动给你建立多大的容量,也就是pv,k8s帮你建立了,建立pvc的时候就须要找pv了,这个时候就交给这个存储类了,而存储类呢,去帮你建立这些pv,存储类呢,就是实现了对指定存储的一个支持,直接帮你去调用api去建立存储类,因此就不须要人工的去帮你建立pv了。 而你去想一想,当节点比较多,业务比较多的时候,再去人工手动建立pv,量仍是很大的,并且也不是很好去维护。 而动态供给主要的一个实现就是StorageClass存储对象,其实它就是声明你使用哪一个存储,而后呢帮你去链接,再帮你去自动建立pv。
举个例子更好去理解
话很少说上图
其实它是一个基于NFS实现的一个pv供给,它大概流程是这样的,咱们可能会建立一个statefulset有状态的应用存储,而后有一个管理的nfs-storageClass,由于nfs目前是不支持这个自动的建立pv的,咱们能够利用社区实现的插件来完成这个pv的自动建立,也就是StorageClass这一块,建立完以后,而后pod再去引用。nginx
这个是kubernetes支持的动态供给的存储插件
https://kubernetes.io/docs/concepts/storage/storage-classes/
这里面呢会告诉你哪些存储支持哪些不支持,支持的话就不用使用社区的存储类了,若是不支持就要去找社区的存储类了,打钩的都是支持的,没打钩的就是不支持的。
这个是那个社区给咱们提供的插件,来看一下
https://github.com/kubernetes-incubator/external-storage
K8s默认是不支持的,咱们可使用这个nfs-client这里面提供的yaml,社区开发的一个组件,这个组件能帮你自动建立PV,在deploy里面,咱们会用到class,这个就会声明了你使用
哪一个存储,哪一个提供的,它会以一个应用的方式部署起来,另一个会用到的就是rbac,
这个存储类会访问API,因此要为他定义RBAC受权策略,还有deployment,它是定义了以组件的形式部署起来,这个组件里面会有一个镜像,直接下载这个镜像就能够了,它能够帮咱们自动的去建立pv,其实就是它来作的,若是k8s支持的话,就直接去调了,但k8s对NFS没有支持,全部须要使用这么个社区的组件来实现了,这里会定义NFS服务器的地址还有数据卷的来源
如今咱们演示一下:
声明这里的yaml文件能够去刚才我发的社区地址当下来git
[root@k8s-master demo]# mkdir nfs-client [root@k8s-master demo]# cd nfs-client/ [root@k8s-master nfs-client]# rz -E rz waiting to receive. [root@k8s-master nfs-client]# ls class.yaml deployment.yaml rbac.yaml
先建立一下rbac,这里也不用定义,直接建立就能够了[root@k8s-master nfs-client]# kubectl create -f rbac.yaml
这里须要修改一个咱们NFS服务器的地址以及NFS服务器的挂载目录github
[root@k8s-master nfs-client]# vim deployment.yaml apiVersion: v1 kind: ServiceAccount metadata: name: nfs-client-provisioner --- kind: Deployment apiVersion: extensions/v1beta1 metadata: name: nfs-client-provisioner spec: replicas: 1 strategy: type: Recreate template: metadata: labels: app: nfs-client-provisioner spec: serviceAccountName: nfs-client-provisioner containers: - name: nfs-client-provisioner image: zhaocheng172/nfs-client-provisioner:latest volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: fuseim.pri/ifs - name: NFS_SERVER value: 192.168.30.27 - name: NFS_PATH value: /opt/k8s volumes: - name: nfs-client-root nfs: server: 192.168.30.27 path: /opt/k8s [root@k8s-master nfs-client]# kubectl create -f class.yaml [root@k8s-master nfs-client]# kubectl create -f deployment.yaml
这里提供者就是fuseim.pri/ifs,在咱们的class里面能够看到,storage须要告知使用者,使用者部署应用到k8s中,想使用这个自动供给,你必须告知它,它须要你在yaml文件里声明这个提供者,那就意味着提供者storageclass能够配置多个,能够是NFS,另外一个是Ceph,另外一个是云存储,均可以能够多个,只要它在建立应用时指定使用哪一个storageclass,它就会在指定在哪一个存储上建立自动pvvim
[root@k8s-master nfs-client]# kubectl get storageclass managed-nfs-storage fuseim.pri/ifs 51s [root@k8s-master nfs-client]# kubectl get pod NAME READY STATUS RESTARTS AGE my-pod 1/1 Running 0 18h nfs-744d977b46-dh9xj 1/1 Running 0 18h nfs-744d977b46-kcx6h 1/1 Running 0 18h nfs-744d977b46-wqhc6 1/1 Running 0 18h nfs-client-provisioner-fbc77b9d4-kkkll 1/1 Running 0 27s
如今咱们就可使用pv的自动供给了
如今咱们来测试一下,我在我原有的静态pod之上,在pvc上加上storageclass的名字
指定咱们的存储类
为了测试咱们实现自动供给,咱们把原来的静态供给删除掉api
[root@k8s-master nfs-client]# kubectl get pv,pvc persistentvolume/zhaocheng 5Gi RWX Retain Released default/my-pvc 18h persistentvolume/zhaochengcheng 10Gi RWX Retain Available 18h [root@k8s-master nfs-client]# kubectl delete persistentvolume/zhaocheng persistentvolume "zhaocheng" deleted [root@k8s-master nfs-client]# kubectl delete persistentvolume/zhaochengcheng persistentvolume "zhaochengcheng" deleted [root@k8s-master nfs-client]# kubectl get pv,pvc No resources found.
这是咱们动态供给的yaml格式,在pvc去指定咱们的存储类storageClassName:
"managed-nfs-storage"bash
[root@k8s-master nfs-client]# vim pod.yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: my-pvc --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: storageClassName: "managed-nfs-storage" accessModes: - ReadWriteMany resources: requests: storage: 5Gi [root@k8s-master nfs-client]# kubectl create -f pod.yaml
查看咱们的pv,pvc已经自动帮咱们去建立,就不用咱们手动再去建立pv了服务器
[root@k8s-master nfs-client]# kubectl get pod my-pod 1/1 Running 0 110s nfs-744d977b46-dh9xj 1/1 Running 0 18h nfs-744d977b46-kcx6h 1/1 Running 0 18h nfs-744d977b46-wqhc6 1/1 Running 0 18h nfs-client-provisioner-fbc77b9d4-kkkll 1/1 Running 0 20m [root@k8s-master nfs-client]# kubectl get pv,pvc persistentvolume/pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a 5Gi RWX Delete Bound default/my-pvc managed-nfs-storage 67s NAME STATUS VOLUME persistentvolumeclaim/my-pvc Bound pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a 5Gi RWX managed-nfs-storage 67s
而在咱们nfs服务器上也能看到pvc的目录,如今咱们就能够去用了网络
[root@localhost k8s]# ls default-my-pvc-pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a wwwroot zhaocheng zhaochengcheng [root@localhost k8s]# cd default-my-pvc-pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a/ [root@localhost default-my-pvc-pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a]# ls [root@localhost default-my-pvc-pvc-a24d4a5e-8f9d-4478-bfe5-b86e2360ae5a]# echo "hello persistentvolumeclaim" > index.html
查看咱们的容器podapp
[root@k8s-master nfs-client]# kubectl exec -it my-pod bash root@my-pod:/# cat /usr/share/nginx/html/index.html hello persistentvolumeclaim