Kubernetes - (8) Storage

임쿠쿠·2022년 5월 21일
0

kubernetes

목록 보기
8/8
post-thumbnail

1. Docker Storage

  • 도커는 cache를 통해 이전 layer를 재사용하여 빠르게 애플리케이션 이미지를 리빌딩하고 새로운 소스코드를 업데이트 한다.

Image Layers : docker build 시, image layer 생성되며 빌드 후에는 새롭게 build 하지 않는 이상 수정할 수 없다.(Read Only)

Container Layer : docker run 시, container layer가 생성되며 애플리케이션의 로그파일과 같은 데이터 들이 컨테이너에 의해 저장된다. 다만 해당 파일들은 컨테이너가 실행중일 때만 이용가능하며 종료 시 모든 기록은 삭제된다.(Read Write)

  • 컨테이너에 의해 생성된 데이터를 영구적으로 보존하기 위해서 volume을 마운트한다.

2. Kubernetes Volumes

  • Node 내 Pod과 Volume 마운트 시, volumes의 path와 type 명시
  • 각각의 컨테이너에 volumeMounts filed 명시

  • Node 내 directory로 volume 생성 시, 멀티 Node 환경에서 데이터 공유가 쉽지 않으므로, AWS EBS와 같은 파일 시스템을 사용한다.

Practice

// 로그 조회
kubectl exec POD명 -- cat /log/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
// 기존 POD 삭제 후 재생성
kubectl replace -f web.yaml --force

3. Persistent Volumes

  • POD 생성할때 마다 manifest file에 volume을 명시하는 것은, large environment에서 작업이 많아지고 수정할 때도 불편하다. 이를 위해 중앙에서 매니징 역할을 해줄 수 있는 Persistent Volume 필요

  • 유저는 필요한 스토리지를 persistent volume pool에서 선택하기만 하면된다.

Persistent Volume Claims

  • Node가 PV 이용하기 위해서는 Persistent Volume Claim 필요
  • Persistent Volume Claims를 생성 시, 쿠버네티스는 Claims와 Persistent Volume을 바인딩한다.
  • POD을 PVC와 연결한다.

Practice

// PersistentVolume 생성
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-log
spec:
  persistentVolumeReclaimPolicy: Retain
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 100Mi
  hostPath:
    path: /pv/log
// PersistentVolumeClaim 생성
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: claim-log-1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Mi

*중요 : PV와 PVC의 accessMode가 일치하지 않을 시 PVC는 pending 상태가된다.

// POD에 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

*중요 : POD에 연결된 PVC는 도중에 PVC를 삭제해도 POD이 삭제되기 전까지 terminating 상태가 된다.

4. Storage Class

  • PVC를 자동으로 Provisioning 시(Cloud 스토리지), storage class를 생성하여 이를 PVC에 명시한다.
// SC 생성
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: delayed-volume-sc
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
profile
Pay it forward

0개의 댓글