[쿠버네티스] emptyDir 와 hostPath 실습

mj·2025년 1월 17일
0

ktb

목록 보기
24/27
post-thumbnail

실습 내용 출처 : https://github.com/ej31


사전준비

  • 클러스터 설치 (aws ubuntu ec2)
  • master node 1개, worker node 3개



🌱 emptyDir


pod 생성하기

vim empty.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: emptydir-multi-container
spec:
  replicas: 4
  selector:
    matchLabels:
      app: emptydir-multi-container
  template:
    metadata:
      labels:
        app: emptydir-multi-container
    spec:
      containers:
        - name: container-a
          image: ghcr.io/ej31/jeff-ex:1.3-amd64
          imagePullPolicy: IfNotPresent # 이미지가 로컬에 없을 때만 pull 
          volumeMounts:
            - name: shared-volume
              mountPath: /usr/src/app/shared
          ports:
            - containerPort: 3000      # express라서 기본 3000 포트를 사용함

        - name: container-b
          image: ghcr.io/ej31/jeff-ex-port2000:1.1-amd64
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: shared-volume
              mountPath: /usr/src/app/shared
          ports:
            - containerPort: 2000

      volumes:
        - name: shared-volume
          emptyDir: {}
kubectl apply -f empty.yaml

실행 결과

ubuntu@master:~/conf$ kubectl get pods
NAME                                        READY   STATUS    RESTARTS   AGE
emptydir-multi-container-85867957c5-4htxc   2/2     Running   0          41s
emptydir-multi-container-85867957c5-5x42n   2/2     Running   0          41s
emptydir-multi-container-85867957c5-ctsl7   2/2     Running   0          41s
emptydir-multi-container-85867957c5-kk7nf   2/2     Running   0          41s

4htxc Pod 내부의 container-a 와 container-b는 볼륨을 공유하고 있는것을 확인할 수 있다.

4htxc5x42n 끼리는 공유하지 않는다. ( Pod끼리는 공유X)

→ emptyDir : Pod 내부의 컨테이너끼리 볼륨 공유

# 실습한 deployment 삭제
ubuntu@master:~/conf$ kubectl delete deployment emptydir-multi-container
deployment.apps "emptydir-multi-container" deleted



🌱 hostPath


pod 생성하기

vim empty.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostpath-deployment-a
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hostpath-app-a
  template:
    metadata:
      labels:
        app: hostpath-app-a
    spec:
      nodeSelector:
        kubernetes.io/hostname: worker1
      containers:
      - name: jeff-server
        image: ghcr.io/ej31/jeff-ex:1.3-amd64
        ports:
        - containerPort: 3000
        volumeMounts:
        - name: shared-volume
          mountPath: /app/data
      volumes:
      - name: shared-volume
        hostPath:
          path: /tmp/hostpath-shared
          type: DirectoryOrCreate
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostpath-deployment-b
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hostpath-app-b
  template:
    metadata:
      labels:
        app: hostpath-app-b
    spec:
      nodeSelector:
        kubernetes.io/hostname: worker2
      containers:
      - name: jeff-server
        image: ghcr.io/ej31/jeff-ex:1.3-amd64
        ports:
        - containerPort: 3000
        volumeMounts:
        - name: shared-volume
          mountPath: /app/data
      volumes:
      - name: shared-volume
        hostPath:
          path: /tmp/hostpath-shared
          type: DirectoryOrCreate

실습 1)

replicas: 1인 경우 실습


실습 2)

hostpath.yaml의 내용을 replicas: 2으로 수정


워커 노드에서도 확인하기

두 Pod가 뜬 노드(예: worker1)에 SSH 접속 :

ssh worker1
cd /tmp/hostpath-shared
ls -al
cat fromA.txt

pod 삭제 :

kubectl delete -f multiple-pods-hostpath.yaml

master에서 pod는 삭제되어도 worker1의 /tmp/hostpath-shared는 남아있다.

profile
일단 할 수 있는걸 하자.

0개의 댓글

관련 채용 정보