hoonii2

[k8s] PV, PVC 의 Life Cycle 및 재활용 본문

개념 공부/(인프라) 04. 컨테이너

[k8s] PV, PVC 의 Life Cycle 및 재활용

hoonii2 2023. 8. 1. 16:37

1. 개요

기존 사용중인 PV 를 다른 Claim 에서 사용하고자 할 때 알아두어야 할 개념을 작성하였습니다.

 

2. PV 의 Life Cycle

PV 에는 4가지 상태가 있습니다.

- Available : PVC 에 할당될 수 있는 상태

- Bound : PVC 에 할당된 상태

- Released : PVC 에서 할당이 해제되어 정보 유지 중인 상태

- Fail : 여러 이유로 문제가 생긴 상태

 

PV 가 할당이 해제되었을 떄 동작하는 방식인 Reclaim 의 정책은 아래와 같습니다.

( spec.persistentVolumeReclaimPolicy )

- Retain : 할당이 해제되어도 내부 내용을 지우지 않고 Released 상태로 유지합니다. 또한 기존 할당되었던 PVC 정보도 유지하여 다른 Claim 에 재할당 되는 것을 방지합니다.

- Delete : 할당 해제 시 PV 디스크 정보 자체를 지웁니다.

- Recycle : 할당 해제 시 디스크 내용을 초기화합니다.

 

3. PV 의 재활용

저의 경우 elasticsearch 로깅 데이터를 목적으로 사용하던 PV 가 Claim 이 변경되는 경우였기에 기존 데이터를 유지한 채로 Claim 만 변경을 해야 했습니다.

 

때문에 Recalim 정책은 Retain 으로 구성해두었고 PVC 를 삭제하여 PV 의 Bound 를 Release 로 변경하였습니다.

root@k8s-master:/var/log/elasticsearch/data# kubectl get pv -n logging -A
NAME                      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                                                 STORAGECLASS   REASON   AGE
elasticsearch-master-pv   30Gi       RWO            Retain           Released   default/elasticsearch-master-elasticsearch-master-0

 

여기서 Released 인 경우 실제로 기존 PVC 할당 정보를 저장해두는지 확인했습니다.

  spec:
    accessModes:
    - ReadWriteOnce
    capacity:
      storage: 30Gi
    claimRef:
      apiVersion: v1
      kind: PersistentVolumeClaim
      name: elasticsearch-master-elasticsearch-master-0
      namespace: default
      resourceVersion: "824031"
      uid: 6f27832f-b832-4e35-957e-56692428eddf

// PV 정보 중 일부

 

위 정보를 초기화하여 Available 및 다시 Bound 가 가능하도록 patch 를 통해 변경하였습니다.

root@k8s-master:~# kubectl patch pv elasticsearch-master-pv -p '{"spec":{"claimRef": null}}'
persistentvolume/elasticsearch-master-pv patched

 

이후 정상적으로 Availiable 되었다가 Bound 로 새로운 Claim 에 할당되는 것을 확인할 수 있습니다.

 

4. 참고

https://kubernetes.io/docs/tasks/administer-cluster/change-pv-reclaim-policy/

 

Change the Reclaim Policy of a PersistentVolume

This page shows how to change the reclaim policy of a Kubernetes PersistentVolume. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this

kubernetes.io

 

Comments