[CKA] Pod에서 PVC 사용

Pod에서 PVC 사용의 핵심 개념과 구성 방법, 실습 풀이를 정리합니다.

Using PVCs in PODs

PVC를 생성하면 다음과 같이 볼륨 섹션의 persistenceVolumeClaim 섹션 아래에 PVC 클레임 이름을 지정하여 POD 정의 파일에서 PVC를 사용합니다.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim

ReplicaSet 또는 Deployment의 경우에도 마찬가지입니다.


kubectl exec webapp -- cat /log/app.log

호스트의 /var/log/webapp에 로그를 남기도록 볼륨을 구성합니다.

 kubectl get po webapp -o yaml > webapp.yaml

호스트의 /var/log/webapp에 로그를 남기는 Pod 정의를 생성합니다.

kubectl run webapp --image=example/event-simulator --dry-run=client -o yaml > pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: webapp
  name: webapp
spec:
  volumes:
  - name: log-volume
    hostPath:
      path: /var/log/webapp
      type: Directory
  containers:
  - image: example/event-simulator
    name: webapp
    resources: {}
    volumeMounts:
      - mountPath: /log
        name: log-volume
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
kubectl create pv pv-log --storage=100Mi --host-path=/pv/log --access-modes=ReadWriteMany --reclaim-policy=Retain --dry-run=client -o yaml > pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-log
spec:
  capacity:
    storage: 100Mi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /pv/log

hostPath 경로 주의할 것



apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: webapp
  name: webapp
spec:
  volumes:
  - name: log-volume
    persistentVolumeClaim:
      claimName: claim-log-1
  containers:
  - image: example/event-simulator
    name: webapp
    resources: {}
    volumeMounts:
      - mountPath: /log
        name: log-volume
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Retain 정책에서는 PVC를 삭제해도 PV와 데이터가 남고 상태가 Released가 됩니다. 다시 사용하려면 관리자가 이전 claim 정보를 정리하는 등 수동 조치가 필요합니다.

Pod가 PVC를 사용 중일 때 PVC 삭제를 요청하면 스토리지 객체 보호 기능에 따라 실제 삭제가 미뤄질 수 있습니다.
Pod 자체는 삭제할 수 있으며, Pod를 삭제해도 일반적인 PVC는 자동으로 삭제되지 않습니다. PVC를 별도로 삭제하면 Retain 정책의 PV는 Released 상태가 됩니다.