Recent Comments
Link
Recent Posts
Today
Total
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
관리 메뉴

Study Memory Work

[K8S/CKA 자격증] Storage - Volume mount 본문

Infra/Kubernetes

[K8S/CKA 자격증] Storage - Volume mount

Hera Choi 2023. 1. 27. 19:40

 Kubernetes Volume

  • Kubernetes Storage 의 추상화 개념. Pod에 바인딩 되는 볼륨을 마운트 하고 마치 로컬 파일 시스템에 있는 것처럼 접근이 가능하다.

 

Kubernetes Storage

# storage 선언
volumes : 
- name: html
  hostPath:
    path: /hostdir_or_file

Mount

# Volume Mount 선언
volumeMounts:
- name: html
  mountPath: /webdata

Volume Type

emptyDir, HostPath, gitRepo, nfs, gcePersistentDisk, awsElastic-BlockStore, azureDisk, cinder, cephfs, iscsi, flocker, configMap, secret ..... 

 

 Kubernetes Volume - emptyDir

  • Volume을 통해 컨테이너 간 데이터 공유
  • emtpyDit 볼륨은 빈 디렉토리로 시작
  • Pod를 삭제하면 볼륨 내용도 손실됨
  • 동인한 Pod에서 실행되는 컨테이너 간에 파일을 공유할 때 유용

 

 Kubernetes Volume - hostPath

: worker node의 로컬디크스에 container Pod가 access하도록 지원하는 것. 
로그를 쌓는 폴더를 shared storage로 mount 해 놓으면 어느 Pod에서 컨테이너가 작동하든 log는 한 곳에 쌓을 수 있다. 
log 수집용 agent가 hostPath를 많이 사용한다.  

  • 노드의 파일시스템의 디렉토리나 파일을 컨테이너에 마운트
  • 노드에 디렉토리나 파일을 생성하여 마운트
  • hostPath 는 type 지시어를 이용해 mount 구성의 요구를 추가할 수 있다.

emptyDir는 container와 lifecycle을 같이 하는 임시적인 성격이 있지만 hostPath는 영구적. but Node 내로 범위가 국한적이다.

volumes:
- name: html
  hostPath:
    type: FileOrCreate
    path: /hostdir_or_file

[문제 1] emptyDir Volume을 공유하는 multi-pod 운영

작업 클러스터 : k8s
다음 조건에 맞춰서 nginx 웹서버 pod가 생성한 로그파일을 받아서 STDOUT으로 출력하는 busybox 컨테이너를 운영하시오.

  • Pod Name: weblog
  • Web container:
    • Image: nginx:1.17
    • Volume mount : /var/log/nginx
    • readwrite
  • Log container:
    • Image: busybox
    • Command: /bin/sh, -c, "tail -n+1 -f /data/access.log"
    • Volume mount : /data
    • readonly
  • emptyDir 볼륨을 통한 데이터 공유
$ kubectl run weblog --image=nginx:1.17 --dry-run-client -o yaml > weblog.yaml
$ vi weblog.yaml

###
apiVersion: v1
Kind: Pod
metadata:
	name: weblog
spec:
	containers:
    - image: nginx:1.17
      name: web
      volumeMounts:			# volume mount
      - mountPath: /var/log/nginx
        name: log-volume
        
    - image: busybox
      name: log
      args : [/bin/sh, -c, "tail -n+1 -f /data/access.log"]	
      volumeMounts:			# volume mount
      - mountPath: /data
        name: log-volume
        readOnly: true
        
      volumes:				# volume 선언
      - name: log-volume
        emptyDir: {}
###

$ kubectl apply -f weblog.yaml

# 2. 확인
$ kubectl get pod
$ kubectl describe weblog		# volume 체크
$ kubectl logs weblog -c log

 

 

[문제 2] HostPath Volume 구성  ( 실제 문제 유형은 아님. 연습용)

작업 클러스터 : k8s

  • /data/cka/fluentd.yaml 파일에 다음 조건에 맞게 볼륨 마운트를 설정하시오.
  • Worker node의 도커 컨테이너 디렉토리를 동일 디렉토리로 pod에 마운트 하시오.
  • Worker node의 /var/log 디렉토리를 fluentd Pod에 동일이름의 디렉토리 마운트하시오.
# /data/cka/fluentd.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
spec:
  selector:
    matchLabels:
      name: fluentd
  template:
    metadata:
      labels:
        name: fluentd
    spec:
      containers:
      - name: fluentd
        image: fluentd

 

-- 답

# 1. yaml 파일 오픈하여 volume mount 설정 
$ vi /data/cka/fluentd.yam

###
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
spec:
  selector:
    matchLabels:
      name: fluentd
  template:
    metadata:
      labels:
        name: fluentd
    spec:
      containers:
      - name: fluentd
        image: fluentd
        volumeMounts:
    	- mountPath: /var/lib/docker/containers
          name: docker-vol
        - mountPath: /var/log
          name: docker-log
      volumes:
  	  - name: docker-vol
        hostPath:
          path: /var/lib/docker/containers
          type: Directory
      - name: docker-log
        hostPath:
          path: /var/log
          type: Directory
###