Kubernetes/KoudKloud - CKAD with Tests

Volume 관련 명령어

비번변경 2021. 12. 16. 20:29

Pod는 수시로 삭제되었다가 재생성되는데, 삭제 시에 포드 내부에 있는 데이터도 함께 삭제된다.

유지해야 하는 데이터가 존재한다면 Volume을 구성해 Pod에 마운트해야 한다.

 

이 글에서는 Volume 구성 및 PV/PVC를 이용한 구성 및 확인 명령어에 대해 정리한다.

 

Volume (HostPath) 구성 포드 생성

yaml 파일에 .spec.containes.volumeMounts 필드와 .spec.volumes 필드를 작성하여 구성한다.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: webapp
  name: webapp
spec:
  containers:
  - image: kodekloud/event-simulator
    name: webapp
    volumeMounts:
    - mountPath: /log
      name: test-volume
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  volumes:
  - name: test-volume
    hostPath:
      path: /var/log/webapp
status: {}

 

PV(HostPath) 생성

yaml 정의 파일을 작성하여 생성한다.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-log
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /pv/log

 

PV 목록 확인

get 명령어로 생성한 pv 목록을 확인할 수 있다.

kubectl get pv

kubectl get pv

 

PVC 생성

yaml 파일을 작성하여 pvc를 생성한다.

PVC는 AccessMode가 동일하고, 요구 리소스가 보다 많은 PV에 바인딩된다.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: claim-log-1 
spec:
  accessModes:
    - ReadWriteOnce # PV 값 확인
  volumeName: # PV 값 확인
  storageClassName: "" # PV 값 확인. 공백인 경우 동적 프로비저닝 비활성화. 설정이 없으면 기본 스토리지 클래스 지정
  resources:
    requests:
      storage: 50Mi # PV보다 적게
      

 

PVC 목록 확인

get 명령어를 이용해 생성한 PVC 목록을 확인할 수 있다.

kubectl get pvc

kubectl get pvc

 

PVC 상세 정보 확인

describe 명령어를 이용해 PVC의 이름, 상태, AccessMode, Event 등의 상세 정보를 확인할 수 있다.

kubectl describe pvc <NAME>

# 예시
kubectl describe pvc claim-log-1

kubectl describe pvc &lt;NAME&gt;

 

PVC 업데이트

edit 명령으로 수정이 불가능해 삭제 후 재생성이 필요하다.

 

PVC 구성 포드 생성

yaml 파일에 .spec.containes.volumeMounts 필드와 .spec.volumes 필드를 작성하여 구성한다.

hostPath로 Volume을 구성할 때와의 차이점은 .spec.volumes.persistentVolumeClaim 필드이다.

apiVersion: v1
kind: Pod
metadata:
  name: webapp
spec:
  containers:
  - image: kodekloud/event-simulator
    imagePullPolicy: Always
    name: webapp
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /log
      name: mypd
 volumes:
  - name: mypd
    persistentVolumeClaim:
      claimName: claim-log-1

 

PVC 삭제

delete 명령을 이용해 PVC를 삭제한다. 삭제 시에는 해당 PVC를 사용 중인 포드가 없을 때 정상적으로 완료된다.

kubectl delete pvc <NAME>

# 예시
kubectl delete pvc claim-log-1
728x90