Day 13:StorageClass 有提到 StorageClass 的主要作用就是讓我們可以在建立 PVC 時,動態的幫助我們配置相對應的 PV。而 Day 14:local-path-provisioner
則提到 PVC 建立後,會在 HostPath 建立名稱包含 PVC ID 的資料夾。
基於這些資訊,我想到了可以去觀察 PV 這個元件,這是 PVC gitlab-minio
所屬的 PV 的 Yaml 檔。
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
creationTimestamp: "2024-10-01T14:34:43Z"
finalizers:
- kubernetes.io/pv-protection
name: pvc-de673ad8-35dc-477c-b81e-ecda44dfdc6d
resourceVersion: "4483"
uid: 36bb118f-9f64-4ddb-a23f-2d726cd4fd33
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: gitlab-minio
namespace: gitlab
resourceVersion: "4400"
uid: de673ad8-35dc-477c-b81e-ecda44dfdc6d
hostPath:
path: /var/local-path-provisioner/pvc-de673ad8-35dc-477c-b81e-ecda44dfdc6d_gitlab_gitlab-minio
type: DirectoryOrCreate
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- local-dev-worker2
persistentVolumeReclaimPolicy: Retain
storageClassName: standard-retain
volumeMode: Filesystem
status:
lastPhaseTransitionTime: "2024-10-01T14:34:43Z"
phase: Bound
觀察上述 Yaml 可以觀察到幾個部分:
綜合以上幾點可以得知,這個 PV 是如何定義和 PVC 與 StorageClass 還有 HostPath 之間的關係。
如果將這個 Yaml 改成不指定 Node、不指定對應的 PVC ID,只根據 PVC Name 來配對。再保留使用相同的 HostPath 路徑與 StorageClass。這樣是否在建立新 PVC 時,是否就可以繼承這個些資料了?
以下是我將所有 PV 修改完成的 Yaml:
apiVersion: v1
items:
- apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
name: pvc-2876a885-8908-4f44-b90e-d3f1dd3ee3f5
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 8Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: data-gitlab-postgresql-0
namespace: gitlab
hostPath:
path: /var/local-path-provisioner/pvc-2876a885-8908-4f44-b90e-d3f1dd3ee3f5_gitlab_data-gitlab-postgresql-0
type: DirectoryOrCreate
persistentVolumeReclaimPolicy: Retain
storageClassName: standard-retain
volumeMode: Filesystem
- apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
name: pvc-2d31279f-e9c8-45a6-aec9-8141120efce4
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 8Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: redis-data-gitlab-redis-master-0
namespace: gitlab
hostPath:
path: /var/local-path-provisioner/pvc-2d31279f-e9c8-45a6-aec9-8141120efce4_gitlab_redis-data-gitlab-redis-master-0
type: DirectoryOrCreate
persistentVolumeReclaimPolicy: Retain
storageClassName: standard-retain
volumeMode: Filesystem
- apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
name: pvc-3e6e871a-d20e-4938-83c1-e092cb96844b
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 50Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: repo-data-gitlab-gitaly-0
namespace: gitlab
hostPath:
path: /var/local-path-provisioner/pvc-3e6e871a-d20e-4938-83c1-e092cb96844b_gitlab_repo-data-gitlab-gitaly-0
type: DirectoryOrCreate
persistentVolumeReclaimPolicy: Retain
storageClassName: standard-retain
volumeMode: Filesystem
- apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
name: pvc-de673ad8-35dc-477c-b81e-ecda44dfdc6d
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: gitlab-minio
namespace: gitlab
hostPath:
path: /var/local-path-provisioner/pvc-de673ad8-35dc-477c-b81e-ecda44dfdc6d_gitlab_gitlab-minio
type: DirectoryOrCreate
persistentVolumeReclaimPolicy: Retain
storageClassName: standard-retain
volumeMode: Filesystem
kind: List
metadata:
resourceVersion: ""
selfLink: ""
將 GitLab Helm Release 刪除,再刪除 PVCs、PVs、ConfigMaps、Secrets 後,我先將 PV 建立起來。
kubectl apply -f pv-new.yaml
再將使用昨天相同的指令部署,部署完成後發現,新的 PVC 在建立時,成功繼承這些資料!!!
如果是第一次建立 GitLab,不包含這些 PVC 的資料會發生什麼事?
由於 hostPath 的 type 為DirectoryOrCreate
,所以就算資料夾不存在,它也會自動建立,所以不用擔心。