CKA (Certified Kubernetes Administrator)/Kode Kloud

07.Storage - Persistent Volume Claims

seulseul 2022. 1. 25. 15:35
LABS – CERTIFIED KUBERNETES ADMINISTRATOR WITH PRACTICE TESTS > STORAGE
 
Storage

01. Persistent Volume Claims
02. Storage Class

 

# kubectl cheat sheet
alias k=kubectl
complete -F __start_kubectl k

 

 

01. info

We have deployed a POD. Inspect the POD and wait for it to start running.

In the current(default) namespace.

 

02. 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
 
 

 

03. If the POD was to get deleted now, would you be able to view these logs.

# pod 삭제

k delete pod webapp

# log 조회 명령어
k exec webapp -- cat /var/app.log
The logs are stored in the Container's file system that lives only as long as the Container does.
로그는 컨테이너가 존재하는 한 지속되는 컨테이너의 파일 시스템에 저장됩니다.

Once the pod is destroyed, you cannot view the logs again.
포드가 파괴되면 로그를 다시 볼 수 없습니다.

 

04. 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
Use the command kubectl run to create a pod 

and use the flag --dry-run=client -oyaml 

to generate the manifest file.

In the manifest file add spec.volumes and spec.containers.volumeMounts property.

 

Solution manifest file to create a webapp pod with given properties as follows:

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
Then run the command kubectl create -f <file-name>.yaml to create a pod.

 

05. 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:
  persistentVolumeReclaimPolicy: Retain
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 100Mi
  hostPath:
    path: /pv/log
Then run the command kubectl create -f <file-name>.yaml to create a PV from manifest file.

 

06. Let us claim some of that storage for our application.

Create a Persistent Volume Claim with the given specification.

 
Solution manifest file to create a claim-log-1 PVC with given properties as follows:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: claim-log-1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Mi
Then run kubectl create -f <file-name>.yaml to create a PVC from the manifest file.

 

07. What is the state of the Persistent Volume Claim?

 

ask :) PENDING

 

root@controlplane:~# k get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-log   100Mi      RWX            Retain           Available                                   9m5s
root@controlplane:~# k get pvc
NAME          STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
claim-log-1   Pending                                                     3m43s

08. What is the state of the Persistent Volume?

 

ask : AVAILABLE

 

 

09. Why is the claim not bound to the available Persistent Volume?

1) Reclaim Policy not set correctly

2) Capacity Mismatch

3) PV and PVC name mismatch

4) Access Modes Mismatch

 

Run the command: kubectl get pv,pvc and look at the Access Modes.

 

root@controlplane:~# k get pv,pvc
NAME                      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
persistentvolume/pv-log   100Mi      RWX            Retain           Available                                   13m

NAME                                STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/claim-log-1   Pending                                                     8m23s

 

 

-- k describe pvc 

결과 해당 문구 확인 가능.

 

no persistent volumes available for this claim and no storage class is set.

이 클레임에 사용할 수 있는 영구 볼륨이 없고 설정된 스토리지 클래스가 없습니다.



root@controlplane:~# k describe pvc   
Name:          claim-log-1
Namespace:     default
StorageClass:  
Status:        Pending
Volume:        
Labels:        <none>
Annotations:   <none>
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      
Access Modes:  
VolumeMode:    Filesystem
Used By:       <none>
Events:
  Type    Reason         Age                  From                         Message
  ----    ------         ----                 ----                         -------
  Normal  FailedBinding  3s (x25 over 5m52s)  persistentvolume-controller  no persistent volumes available for this claim and no storage class is set

 

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

 

Set the Access Mode on the PVC to ReadWriteMany.

6 번문제의 yaml 파일에서 AccessMode 를 ReadWriteMany 로 변경해주면된다.

 

 

11. You requested for 50Mi, how much capacity is now available to the PVC?

 

 

ask : 100Mi

 

Run the command: kubectl get pvc and look under the capacity section.
root@controlplane:~# kubectl get pvc
NAME          STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
claim-log-1   Bound    pv-log   100Mi      RWX                           18s​


 

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
To delete the webapp pod first:

$ kubectl delete po webapp
Add --force flag in above command, if you would like to delete the pod without any delay.

To create a new webapp pod with given properties as follows:

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
Then run the command kubectl create -f <file-name>.yaml to create a pod from the definition file.

 

13. What  is the Reclaim Policy set on the Persistent Volume pv-log? 

 

ask : Retain

root@controlplane:~# k get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS   REASON   AGE
pv-log   100Mi      RWX            Retain           Bound    default/claim-log-1                           15m

 

14. What would happen to the PV if the PVC was destroyed?

1) The PV is made available again

2) The PV is scrubbed

3) The PV is deleted as well

4) The PV is not deleted but not available

 

15. Try deleting the PVC and notice what happens.

1) The PVC is deleted

2) The PVC is stuck in 'terminating' state

 

16. Why is the PVC stuck in Terminating state?

1) The PVC is being used by a POD

2) The PVC is waiting for the PV to be deleted

3) The PVC is in the process of scrubbing

 

The PVC was still being used by the webapp pod when we issued the delete command.

삭제 명령을 실행했을 때 Webapp 팟(Pod)에서 PVC를 계속 사용하고 있었습니다.

Until the pod is deleted, the PVC will remain in a  terminating state.

포드가 삭제될 때까지 PVC는 종료 상태로 유지됩니다.

 

17. Let us now delete the webapp Pod.

Once deleted, wait for the pod to fully terminate.


  • Name: webapp
k delete pod webapp

 

18. What is the state of the PVC now?

 

ask : 

Deleted

 

19. What is the state of the Persistent Volume now?

 

ask : Released

root@controlplane:~# k get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                 STORAGECLASS   REASON   AGE
pv-log   100Mi      RWX            Retain           Released   default/claim-log-1                           29m

 

 

 

 


 

PV (Persistent Volume) 

- 시스템 관리자가 실제 물리 디스크를 생성한 후에, 이 디스크를 PersistentVolume 이라는 이름으로 쿠버네티스에 등록합니다.

- 시스템 관리자가 생성한 물리 디스크를 쿠버네티스 클러스터에 표현한 것이 PV 입니다.

 

 

 

 

 


Bookmark

 

https://kubernetes.io/ko/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

 

스토리지로 퍼시스턴트볼륨(PersistentVolume)을 사용하도록 파드 설정하기

이 페이지는 스토리지에 대해 퍼시스턴트볼륨클레임(PersistentVolumeClaim)을 사용하도록 파드를 설정하는 방법을 보여준다. 과정의 요약은 다음과 같다. 클러스터 관리자로서, 물리적 스토리지와

kubernetes.io