Persistent Volume(PV)와 Persistent Volume Claim(PVC)
1. Persistent Volume, Persistent Volume Claim, Deployment 개념 이해
✅ Deployment
Pod를 관리하는 컨트롤러입니다.
지정한 수(replica)의 Pod가 항상 실행되도록 보장합니다.
Pod가 삭제되거나 장애가 나도 새로운 Pod가 자동으로 생성됩니다.
✅ Persistent Volume (PV)
클러스터에서 사용할 수 있는 스토리지 자원을 정의하는 객체입니다.
실제 물리 디스크, NFS(Network File System), 클라우드 스토리지 등 다양한 스토리지를 추상화하여 제공합니다.
쉽게 말해, "창고(스토리지)를 미리 준비해 놓은 상태"라고 보시면 됩니다.
✅ Persistent Volume Claim (PVC)
Pod가 필요할 때 스토리지를 요청하는 방법입니다.
Pod는 직접 PV를 요청하지 않고, PVC를 통해 간접적으로 PV를 사용합니다.
비유하자면, PVC는 "창고를 빌려 달라는 신청서" 같은 개념입니다.
2. 코드 설명
deployment.yml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-pvc-deployment spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: nginx-storage mountPath: /usr/share/nginx/html volumes: - name: nginx-storage persistentVolumeClaim: claimName: nginx-pvc
✅ nginx 컨테이너를 실행하면서 /usr/share/nginx/html 경로에 PVC(nginx-pvc)를 연결합니다.
persistent_volume.yml
apiVersion: v1 kind: PersistentVolume metadata: name: nginx-pv spec: capacity: storage: 1Mi accessModes: - ReadWriteOnce hostPath: type: DirectoryOrCreate path: "/mnt/data"
✅ PV를 정의하는 코드입니다.
capacity: 1Mi → 1MB 공간 제공
accessModes: ReadWriteOnce → 하나의 노드에서 Read/Write 가능
hostPath: /mnt/data → 노드의 로컬 디렉토리를 스토리지로 사용
persistent_volume_claim.yml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nginx-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Mi
✅ PVC를 정의하는 코드입니다.
Pod가 "1MB 짜리 스토리지를 주세요"라고 요청하는 것과 같습니다.
PVC는 PV에 매칭되어 실제 스토리지를 할당받습니다.
3. 실습 과정과 실행 결과
리소스 생성:
kubectl apply -f ./9_persistent_volume/
deployment.apps/nginx-pvc-deployment created persistentvolume/nginx-pv created persistentvolumeclaim/nginx-pvc created
Pod 확인:
kubectl get pods
NAME READY STATUS RESTARTS AGE nginx-pvc-deployment-674b4d56f9-9v5b2 1/1 Running 0 26s
Pod 상세 확인:
kubectl describe pod nginx-pvc-deployment-674b4d56f9-9v5b2
Mounts: /usr/share/nginx/html from nginx-storage (rw) Volumes: nginx-storage: Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace) ClaimName: nginx-pvc
➡️ PVC가 /usr/share/nginx/html에 잘 연결된 것을 확인할 수 있습니다.
4. 데이터 영속성 테스트
Pod 내부에 접속 후, index.html 작성:
kubectl exec -it nginx-pvc-deployment-674b4d56f9-9v5b2 -- /bin/sh # echo "<h1>Persistent Volume Test</h1>" > /usr/share/nginx/html/index.html # exit
브라우저 접근 (포트포워딩):
kubectl port-forward nginx-pvc-deployment-674b4d56f9-9v5b2 8080:80
➡️ 브라우저에서 http://localhost:8080 접속 시, 작성한 HTML이 표시됩니다.
Pod 삭제 후 재생성:
kubectl delete pod nginx-pvc-deployment-674b4d56f9-9v5b2
새로운 Pod이 생성되고 다시 접속해도 작성한 index.html 파일은 그대로 유지됩니다.
5. 응용 포인트
데이터 보존: Pod가 삭제·재시작되어도 데이터는 PVC/PV에 남아 유지됩니다.
유연성: 로컬 디렉토리(hostPath) 외에도 클라우드 스토리지(AWS EBS, GCP Persistent Disk 등)로 확장 가능.
접근 모드 활용:
RWO(ReadWriteOnce): 한 노드에서만 쓰기 가능
ROX(ReadOnlyMany): 여러 노드에서 읽기만 가능
RWX(ReadWriteMany): 여러 노드에서 동시에 읽기/쓰기 가능 (NFS, Ceph 등)
✅ 이 전체 내용 요약
Deployment: Pod를 관리하고 보장해주는 컨트롤러
PV: 실제 스토리지를 정의하는 리소스
PVC: Pod가 스토리지를 요청하는 방법
Pod에 PVC를 연결하면, 데이터가 Pod 라이프사이클과 무관하게 보존됨
Persistent Volume은 데이터 영속성을 보장하는 핵심 기술이며, 실무에서도 필수적으로 사용됨
댓글 ( 0)
댓글 남기기