본문 바로가기
Compute/kubernetis

[따배씨] 20. Persistent Volume Claim을 사용하는 Pod 운영 / CKA 시험 문제 학습

by 조청유곽 2025. 2. 1.
반응형

이 포스팅은 아래의 유튜브 채널 "따배"를 따라서 학습한 내용입니다.  

 

 

[관련 이론]

 

 

PV / PVC Bound 상태란?

Kubernetes에서 **PersistentVolume (PV)**과 **PersistentVolumeClaim (PVC)**는 
서로 연결될 때 Bound 상태가 됩니다.
즉, PVC가 PV를 성공적으로 요청하고 할당받았을 때 "Bound" 상태가 됩니다.

 


 

 

[Precondition]

(1) 테스트 환경

(1.1) Rocky Linux Cluster 

: 직접 구성

[root@k8s-master ~]# k get nodes -o wide
NAME         STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                            KERNEL-VERSION                  CONTAINER-RUNTIME
k8s-master   Ready    control-plane   30d   v1.27.2   192.168.56.30   <none>        Rocky Linux 8.10 (Green Obsidian)   4.18.0-553.33.1.el8_10.x86_64   containerd://1.6.32
k8s-node1    Ready    <none>          30d   v1.27.2   192.168.56.31   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
k8s-node2    Ready    <none>          30d   v1.27.2   192.168.56.32   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
[root@k8s-master ~]#

 

(1.2) Ubuntu Cluster 

: kodekloud 테스트 환경 활용

controlplane ~ ➜  kubectl get nodes -o wide
NAME           STATUS   ROLES           AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
controlplane   Ready    control-plane   9m6s    v1.31.0   192.6.94.6    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26
node01         Ready    <none>          8m31s   v1.31.0   192.6.94.9    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26

https://learn.kodekloud.com/user/courses/udemy-labs-certified-kubernetes-administrator-with-practice-tests

 

(2) 사전 필요 설정 

- 문제 풀이에 사용할 persistent volume을 미리 생생 

   : youtube 강좌와 다름 / 필자 임의로 생성 

[root@k8s-master ~]# cat 20-pv1.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume1
  labels:
    type: local
spec:
  storageClassName: app-hostpath-sc
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
[root@k8s-master ~]# 
[root@k8s-master ~]# cat 20-pv2.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume2
  labels:
    type: local
spec:
  storageClassName: app-hostpath-sc
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
[root@k8s-master ~]# 
[root@k8s-master ~]# k get persistentvolume
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS      REASON   AGE
app-config   1Gi        RWX            Retain           Available           az-c                       40m
pv-volume1   10Gi       RWX            Retain           Available           app-hostpath-sc            88s
pv-volume2   1Gi        RWX            Retain           Available           app-hostpath-sc            2s
[root@k8s-master ~]#

 

 

[Question]

Create a new PersistentVolumeClaim:
- Name: app-volume
- StorageClass: app-hostpath-sc
- Capacity: 10Mi

Create a new Pod which mounts the PersistentVolumeClaim as a volume:
- Name: web-server-pod
- Image: nginx
- Mount path: /usr/share/nginx/html

- Configure the new Pod to have ReadWriteMany access on the volume.

 

 

[Solve]

(1) PersistentVolumeClaim 생성 

[root@k8s-master ~]# vi 20-pvc.yaml
[root@k8s-master ~]# cat 20-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-volume
spec:
  storageClassName: app-hostpath-sc
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Mi
[root@k8s-master ~]# k apply -f 20-pvc.yaml 
persistentvolumeclaim/app-volume created
[root@k8s-master ~]#

 

 

: pvc가 생성되면 storageclass가 동일한 pv에 bound된다.

 

 

(2) pod 생성 

[root@k8s-master ~]# vi 20-pod.yaml
[root@k8s-master ~]# cat 20-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: web-server-pod
spec:
  containers:
    - name: web-server-pod
      image: nginx
      volumeMounts:    
        - name: pv-volume2
          mountPath: /user/share/nginx/html
  volumes:
    - name: pv-volume2
      persistentVolumeClaim:
        claimName: app-volume
[root@k8s-master ~]# 
[root@k8s-master ~]# k apply -f 20-pod.yaml 
pod/web-server-pod created

 

 

: describe로 pod의 container mounts 확인 

 

 

반응형