[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
- Volume Name: pv-log
- Storage: 100Mi
- Access Modes: ReadWriteMany
- Host Path: /pv/log
- Reclaim Policy: Retain
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-log
spec:
capacity:
storage: 100Mi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /pv/log
hostPath 경로 주의할 것
- Volume Name: claim-log-1
- Storage Request: 50Mi
- Access Modes: ReadWriteOnce
- AccessMode가 일치하지 않는 경우엔 할당될 수 없으니 주의
- PVC는
storageClassName: slow를 요구하는데 PV는 storageClassName이 설정되어 있지 않거나 그 반대 경우라면 할당되지 않을 수 있습니다.
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: {}
- PVC를 삭제해도:
- PV는 삭제되지 않음
- PV의 상태가
Bound에서Released로 변경됨 - PV 내의 데이터는 보존됨
- Reclaim Policy 종류:
Retain: PVC 삭제 후 PV 보존 (수동 처리 필요)Delete: PVC 삭제시 PV도 자동 삭제Recycle: (Deprecated) PV 데이터 삭제 후 재사용