天天看點

kubernetes之PV、PVC篇

一、kubernetes之PV、PVC篇

  一)基于aws EBS的pv、pvc實戰

  1、StorageClass(SC)

  持久化卷回收政策

  • 保持(Retain):删除PV後後端存儲上的資料仍然存在,如需徹底删除則需要手動删除後端存儲volume
  • 删除(Delete):删除被PVC釋放的PV和後端存儲volume
  • 回收(Recycle):保留PV,但清空PV上的資料(已廢棄)
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
reclaimPolicy: Retain #持久化卷回收政策
mountOptions:
  - debug      

  2、PersistentVolumeClaim(PVC)

  根據需求配置PVC名稱,權限以及存儲容量

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: task-pv-claim  #pvc名字
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce  #資源權限
  resources:
    requests:
      storage: 1Gi  #存儲容量      

  3、PVC使用

  k8s的pod使用PVC

kind: Pod
apiVersion: v1
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
       claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage      

   4、擴容PV、PVC(kubernetes 1.11版本中開始支援pvc建立後的擴容)

  pvc、pv所使用的StorageClass,配置allowVolumeExpansion: true标簽

  檢視storageclass是否配置了動态擴容,主要看storageclass是否存在allowVolumeExpansion字段

  若沒有,編輯sc添加allowVolumeExpansion字段即可,将value設定為true

## 更改sc
kubectl edit sc openebs-hostpath      
# kubectl get sc openebs-hostpath -o yaml|grep allowVolumeExpansion:
allowVolumeExpansion: true      
  修改方式(修改完sc後)

  方式一:編輯檔案:直接編輯對應的pvc擴容(pvc spec.resources.requests.storage字段)

  方式二:直接使用指令進行修改

kubectl patch pvc zookeeper-0 -p '{"spec":{"resources":{"requests":{"storage":"40Gi"}}}}'      

  二)基于openebs的pv、pvc實戰

  1、安裝openebs

kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml      

  安裝完,效果如下

]#  kubectl get pods -n openebs
NAME                                            READY   STATUS    RESTARTS   AGE
openebs-localpv-provisioner-6756f57d65-xftdx    1/1     Running   0          29d
openebs-ndm-9nc4r                               1/1     Running   0          22d
openebs-ndm-clqhr                               1/1     Running   0          124d
openebs-ndm-cluster-exporter-5c985f8b77-7j24q   1/1     Running   0          29d
openebs-ndm-node-exporter-4vwq4                 1/1     Running   0          22d
openebs-ndm-node-exporter-qd24s                 1/1     Running   0          84d
openebs-ndm-node-exporter-tdplv                 1/1     Running   0          124d
openebs-ndm-operator-9bdd87f58-759q4            1/1     Running   0          29d
openebs-ndm-zhsn4                               1/1     Running   0          84d

]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 124d
openebs-hostpath (default) openebs.io/local Delete WaitForFirstConsumer false      

  2、建立pvc

建立一個 PVC 資源對象,Pods 使用這個 PVC 就可以從 OpenEBS 動态 Local PV Provisioner 中請求 Hostpath Local PV

 

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: opspvc
  namespace: kube-ops
spec:
  storageClassName: openebs-hostpath
  accessModes:
    - ReadWriteOnce ## open
  resources:
    requests:
      storage: 5Gi      

   apply pvc.yaml并驗證(沒有pod使用這個pvc時,pvc處于pending狀态)

]# kubectl apply -f pvc.yaml 
persistentvolumeclaim/opspvc created

]# kubectl get pvc -n kube-ops 
NAME     STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS       AGE
opspvc   Pending                                      openebs-hostpath   77s      

  建立完pvc,建立pod使用pvc,發現pod也處于pending狀态,排除掉資源不足的原因,然後檢視事件發現原因

]# kubectl get po -n kube-ops 
NAME                        READY   STATUS    RESTARTS   AGE
jenkins2-5d577cc67d-g255r   0/1     Pending   0          31s      

  建議:出現pending這種情況,第一時間檢視事件定位問題所在

  ]# kubectl get events -n kube-ops

86s         Warning   ProvisioningFailed     persistentvolumeclaim/opspvc     failed to provision volume with StorageClass "openebs-hostpath": Only support ReadWriteOnce access mode      

   根據報錯,變更配置搞定

  三)基于nfs的pv、pvc實戰

  1、建立命名空間

kubectl create namespace kube-ops      

  2、建立pv、pvc

  pv-pvc.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: opspv
spec:
  capacity:
    storage: 20Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  nfs:
    server: 10.151.30.57
    path: /data/k8s

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: opspvc
  namespace: kube-ops
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi      
kubectl create -f pv-pvc.yaml