天天看點

Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

什麼是資料卷 (Volumns)

在Kubernetes Pod中的容器系統存儲檔案是臨時的,Pod如果異常重新開機将會恢複到鏡像的原始狀态進而會丢失所有的狀态包括系統存儲檔案,或者同一個Pod中多個容器需要共享資料。資料卷誕生就是為了解決這一類資料持久化及資料共享的場景,并與容器的生命周期分離開。

什麼是PV (PersistentVolume)

PV是定義存儲資料的方式,例如存儲類、存儲實作等,是由叢集管理者定義的叢集資源中的對象。

PV AccessModes 通路模式

ReadOnlyMany(ROX) 允許被多個節點以隻讀的模式進行挂載。

ReadWriteOnce(RWO)允許被單個節點以讀寫的模式進行挂載。

ReadWriteMany(RWX)允許被多個節點以讀寫的模式進行挂載。

PV Reclaim Policy 回收機制

Retain(保留) 當PVC與PV解除綁定關系後PV處于保留狀态,如果有同樣聲明的PVC時PV将繼續可以被綁定使用,期間資料不會被删除。

Recycle(回收) 當PVC與PV解除綁定關系後PV将會被回收,如果有新的聲明的PVC時PV将繼續可以被綁定使用,但在解除關系時會被删除資料。

Delete(删除) 當PVC與PV解除綁定關系後PV将會被删除,同時PVC資料也不會保留。

PV Phase 運作狀态

Available PV處于可用狀态,可以被PVC綁定。

Bound PV已被PVC所綁定。

Released PVC已删除,但PV還未被叢集回收。

Failed PV自動回收失敗。

PV 基本操作

通過YAML資源定義清單建立PV

kubectl apply -f nginx-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
spec:
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Delete
  accessModes:
  - ReadWriteMany
  hostPath:
    path: /data/volume/nginx-pv           
Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

檢視PV詳細資訊

kubectl describe pv

Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

什麼是PVC (PersistentVolumeClaim)

PVC是聲明定義存儲資料使用的請求,被挂載到Pod中進行使用。通常由開發人員進行配置使用,不用關心資料存儲底層具體的實作方式,隻關心與業務相關的資料存儲大小,通路方式等。

PV和PVC的綁定規則
  1. 按照通路模式來比對
  2. 按照按照容量大小來選擇最合适的比對,盡量的節省資源
  3. 如通路模式和容量大小都一樣,按照标簽來比對,如果以上條件都不滿足,則随機比對

PV與PVC是一對一關系,一個PVC隻能綁定到一個PV,如果PV被綁定後,其他PVC則無法在使用此PV。

PVC 基本操作

通過kubectl建立PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pvc
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 500M           
Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

kubectl describe pvc

Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

将PVC挂載在Pod上當成資料卷使用

kubectl apply -f nginx-pod-pvc.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.16
      volumeMounts:
      - name: nginx-volume
        mountPath: /data
  volumes:
    - name: nginx-volume
      persistentVolumeClaim:
        claimName: nginx-pvc           

檢視Pod挂載的持久卷資訊

kubectl describe pod

Kubernetes - 6.3 Config and Storage - PersistentVolume and PersistentVolumeClaim

繼續閱讀