실습 내용 출처 : https://github.com/ej31
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는 볼륨을 공유하고 있는것을 확인할 수 있다.
4htxc
와 5x42n
끼리는 공유하지 않는다. ( Pod끼리는 공유X)
→ emptyDir : Pod 내부의 컨테이너끼리 볼륨 공유
# 실습한 deployment 삭제
ubuntu@master:~/conf$ kubectl delete deployment emptydir-multi-container
deployment.apps "emptydir-multi-container" deleted
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
replicas: 1
인 경우 실습
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
는 남아있다.