안녕하세요, 쯀리입니다.
7강을 시작해볼까요? Persistent Volume Claims에 대해 알아보겠습니다.
https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Persistent Volume Claims
Persistent Volume Claims(PVC)는 Kubernetes에서 스토리지 리소스를 관리하고 사용하기 위한 중요한 개념이에요. PVC는 사용자가 스토리지를 요청할 수 있도록 해주며, 이 요청은 클러스터 관리자가 제공한 Persistent Volume(PV)에 의해 충족돼요. PVC와 PV를 통해 스토리지의 동적 및 정적 프로비저닝을 가능하게 해주죠.
주요 개념
- Persistent Volume (PV):
- 클러스터 관리자가 프로비저닝한 스토리지 리소스.
- NFS, iSCSI, 클라우드 제공자 스토리지 등 다양한 스토리지 백엔드를 사용할 수 있어요.
- PV는 클러스터의 리소스로서 독립적으로 존재해요.
- Persistent Volume Claim (PVC):
- 사용자가 스토리지를 요청할 때 사용하는 리소스.
- 스토리지의 크기, 접근 모드(ReadWriteOnce, ReadOnlyMany, ReadWriteMany) 등을 지정할 수 있어요.
- PVC는 사용자의 요청을 나타내며, PV와 바인딩되어 스토리지를 사용할 수 있게 돼요.
- Storage Class:
- 동적 프로비저닝을 지원하기 위해 사용돼요.
- PVC가 생성될 때, 지정된 Storage Class에 따라 새로운 PV가 동적으로 생성돼요.
Quiz.
1. We have deployed a POD. Inspect the POD and wait for it to start running.
In the current(default) namespace.
controlplane ~ ➜ k get pods
NAME READY STATUS RESTARTS AGE
webapp 1/1 Running 0 16s
2. The application stores logs at location /log/app.log. View the logs.
You can exec in to the container and open the file:
kubectl exec webapp -- cat /log/app.log
controlplane ~ ➜ kubectl exec webapp -- cat /log/app.log
[2024-07-28 12:35:29,020] INFO in event-simulator: USER2 logged in
[2024-07-28 12:35:30,021] INFO in event-simulator: USER4 is viewing page2
[2024-07-28 12:35:31,022] INFO in event-simulator: USER1 logged out
[2024-07-28 12:35:32,023] INFO in event-simulator: USER4 logged out
[2024-07-28 12:35:33,024] INFO in event-simulator: USER1 logged out
[2024-07-28 12:35:34,026] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2024-07-28 12:35:34,026] INFO in event-simulator: USER2 logged in
[2024-07-28 12:35:35,027] INFO in event-simulator: USER2 is viewing page1
[2024-07-28 12:35:36,028] INFO in event-simulator: USER3 is viewing page1
[2024-07-28 12:35:37,029] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2024-07-28 12:35:37,029] INFO in event-simulator: USER3 logged in
....
3. If the POD was to get deleted now, would you be able to view these logs.
삭제된 POD에 대한 log는 당연히 안보이겠죠?
4. Configure a volume to store these logs at /var/log/webapp on the host.
Use the spec provided below.
Name: webapp
Image Name: kodekloud/event-simulator
Volume HostPath: /var/log/webapp
Volume Mount: /log
apiVersion: v1
kind: Pod
metadata:
name: webapp
spec:
containers:
- name: event-simulator
image: kodekloud/event-simulator
env:
- name: LOG_HANDLERS
value: file
volumeMounts:
- mountPath: /log
name: log-volume
volumes:
- name: log-volume
hostPath:
# directory location on host
path: /var/log/webapp
# this field is optional
type: Directory
5. Create a Persistent Volume with the given specification.
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
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /pv/log
6. Let us claim some of that storage for our application. Create a Persistent Volume Claim with the given specification.
Volume Name: claim-log-1
Storage Request: 50Mi
Access Modes: ReadWriteOnce
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: claim-log-1
spec:
resources:
requests:
storage: 50Mi
accessModes:
- ReadWriteOnce
PVC와 PC현재 상태
controlplane ~ ➜ k get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
claim-log-1 Pending <unset> 9s
controlplane ~ ➜ k get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-log 100Mi RWX Retain Available <unset> 3m45s
- RWO - ReadWriteOnce
- ROX - ReadOnlyMany
- RWX - ReadWriteMany
- RWOP - ReadWriteOncePod
7. What is the state of the Persistent Volume Claim?
Pending
8. What is the state of the Persistent Volume?
Available
9. Why is the claim not bound to the available Persistent Volume?
PVC(Persistent Volume Claim)와 PV(Persistent Volume)는 바인딩되기 위해 서로 호환되는 accessMode를 가져야 해요. 즉, PVC가 요청한 accessMode가 PV가 제공할 수 있는 accessMode와 일치해야 해요. 그렇지 않으면 PVC와 PV가 바인딩되지 않아요.
10. Update the Access Mode on the claim to bind it to the PV.
Delete and recreate the claim-log-1.
Volume Name: claim-log-1
Storage Request: 50Mi
PVol: pv-log
Status: Bound
controlplane ~ ➜ cat claim-log-1.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: claim-log-1
spec:
resources:
requests:
storage: 50Mi
accessModes:
- ReadWriteMany
Access mode를 ReadWriteMany f로 수정해줍니다.
controlplane ~ ✖ k delete pvc claim-log-1
persistentvolumeclaim "claim-log-1" deleted
controlplane ~ ➜ k apply -f claim-log-1.yaml
persistentvolumeclaim/claim-log-1 created
controlplane ~ ➜ k get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
claim-log-1 Bound pv-log 100Mi RWX <unset> 5s
11. You requested for 50Mi, how much capacity is now available to the PVC?
100Mi
12. Update the webapp pod to use the persistent volume claim as its storage.
Replace hostPath configured earlier with the newly created PersistentVolumeClaim.
Name: webapp
Image Name: kodekloud/event-simulator
Volume: PersistentVolumeClaim=claim-log-1
Volume Mount: /log
4번에서 작성해두었던 webapp.yaml에서 hostPath부분을 지우고 PVC를 작성해주었습니다.
apiVersion: v1
kind: Pod
metadata:
name: webapp
spec:
containers:
- name: event-simulator
image: kodekloud/event-simulator
env:
- name: LOG_HANDLERS
value: file
volumeMounts:
- mountPath: /log
name: log-volume
volumes:
- name: log-volume
persistentVolumeClaim:
claimName: claim-log-1
13. What is the Reclaim Policy set on the Persistent Volume pv-log?
controlplane ~ ➜ k get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-log 100Mi RWX Retain Bound default/claim-log-1 <unset> 8m37s
14. What would happen to the PV if the PVC was destroyed?
PV는 삭제되지 않지만 available하지 않게됩니다.
15. Try deleting the PVC and notice what happens.
If the command hangs, you can use CTRL + C to get back to the bash prompt OR check the status of the pvc from another terminal
controlplane ~ ➜ k get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
claim-log-1 Bound pv-log 100Mi RWX <unset> 9m25s
controlplane ~ ➜ k delete pvc claim-log-1
persistentvolumeclaim "claim-log-1" deleted
^C
controlplane ~ ✖ k get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
claim-log-1 Terminating pv-log 100Mi RWX <unset> 10m
The PVC is stucked in 'Terminating' State
16. Why is the PVC stuck in Terminating state?
해당 PVC에 묶여있는 webapp이라는 Pod가 삭제되어야 PVC가 삭제됩니다.
The PVC is being used by a POD
17. Let us now delete the webapp Pod.
Once deleted, wait for the pod to fully terminate.
controlplane ~ ➜ k delete pod webapp
pod "webapp" deleted
controlplane ~ ➜ k get pvc
No resources found in default namespace.
Pod 삭제되면 PVC도 동일하게 삭제되는것을 볼 수 있습니다.
18. What is the state of the PVC now? Deleted
19. What is the state of the Persistent Volume now?Released
controlplane ~ ➜ k get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-log 100Mi RWX Retain Released default/claim-log-1 <unset> 21m
PVC와 PV는 상태 저장 애플리케이션을 Kubernetes에서 운영할 때 필수적이에요. 예를 들어, 데이터베이스, 파일 저장소, 로그 저장소 등 지속적인 스토리지가 필요한 애플리케이션에서 유용하게 사용돼요. PVC를 통해 필요한 스토리지를 쉽게 요청하고 사용할 수 있으며, PV를 통해 다양한 스토리지 백엔드를 활용할 수 있죠.
PVC와 PV를 잘 이해하고 활용하면, Kubernetes 클러스터에서 스토리지를 효율적으로 관리하고 사용할 수 있어요.
다음시간에는 Storage Class에 대해 알아볼게요
참조
※ Udemy Labs - Certified Kubernetes Administrator with Practice Tests
'IT 잡지식 > DevOps' 카테고리의 다른 글
[CKA] KodeKloud - Explore Environment (0) | 2024.08.08 |
---|---|
[CKA] KodeKloud - Storage Class (0) | 2024.07.29 |
[CKA] KodeKloud - Network Policy (0) | 2024.07.28 |
[CKA] KodeKloud - Security Contexts (0) | 2024.07.24 |
[CKA] KodeKloud - Image Security (0) | 2024.07.20 |