쿠버네티스 교육/강의 내용 정리

220628_3_k8s_볼륨_emptyDir

kimhope 2022. 6. 28. 15:28
728x90

emptyDir


emptyDir란?

임시로 데이터를 저장하는 빈 볼륨

  • 호스트의 디스크를 임시로 컨테이너에 볼륨으로 할당하여 사용
  •  하나의 노드 내에서 여러 파드가 emptyDir를 공유할 수 있음
  • 동일한 파드 내에서 컨테이너 간에 데이터를 공유할 때 유용함
  • 컨테이너에 문제가 발생하여 재시작되더라도 파드는 살아있으므로 emptyDir에 저장해둔 데이터는 계속 사용 가능함
  • 파드가 사라지면 emptyDir에 할당해서 사용했던 볼륨의 데이터도 함께 사라짐

 

특징

  • 볼륨이지만 데이터를 영구 저장하지 않음
  • 파드의 라이프사이클과 동일함
  • 간편하게 설정 가능
  • 디스크 / 메모리를 이용함

 

 


 

실습

두 개의 컨테이너가 하나의 볼륨을 공유하여 사용

  • 레플리카셋 생성 파일
  • 하나의 파드에 두 개의 컨테이너 생성( web-server / html-fortune )
  • 1. .spec.template.spec.volumes: 사용하려는 볼륨 선언
  • - .name: 볼륨 이름 지정
  • - .emptyDir: emptyDir 사용을 위해 빈 값( {} )으로 설정
  • 2. .spec.template.spec.containers.volumeMounts: 마운트할 볼륨 지정
  • - .name: 마운트할 볼륨 이름
  • - .mountPath: 마운트포인트
$ cat myapp-rs-emptydir.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-rs-fortune
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp-rs-fortune
  template:
    metadata:
      labels:
        app: myapp-rs-fortune
    spec:
      containers:
      - name: web-server
        image: nginx:alpine
        volumeMounts:	#-----------------------2
        - name: web-fortune
          mountPath: /usr/share/nginx/html
          readOnly: true
        ports:
        - containerPort: 80
      - name: html-generator
        image: ghcr.io/c1t1d0s7/fortune
        volumeMounts:	#-----------------------2
        - name: web-fortune
          mountPath: /var/htdocs
      volumes:	#-----------------------1
      - name: web-fortune
        emptyDir: {}

 

  • 로드밸런서 서비스 생성 파일
$ cat myapp-svc-emptydir.yml
apiVersion: v1
kind: Service
metadata:
  name: myapp-svc-fortune
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: myapp-rs-fortune

 

  • 레플리카셋, 서비스 생성
$ kubectl create -f myapp-rs-emptydir.yml -f myapp-svc-emptydir.yml
replicaset.apps/myapp-rs-fortune created
service/myapp-svc-fortune created

 

  • 컨테이너에 접속하여 볼륨 마운트가 제대로 되었는지 확인
  • web-fortune 볼륨에 마운트하여 정상 작동하는 것을 확인할 수 있음
$ kubectl exec myapp-rs-fortune-2tl24 -c web-server -- cat /usr/share/nginx/html/index.html

$ kubectl exec myapp-rs-fortune-2tl24 -c html-generator -- cat /var/htdocs/index.html

 

  • 별도의 파드를 생성하여 컨테이너들이 정상 작동하는지 확인
$ kubectl run nettool -it --image=c1t1d0s7/network-multitool --rm=true bash
If you don't see a command prompt, try pressing enter.
bash-5.0#
bash-5.0# curl http://myapp-svc-fortune
You will be run over by a bus.

 

728x90