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