๐Ÿ“€์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋ณผ๋ฅจ(Volume)

๊น€์„ฑ์ธยท2023๋…„ 10์›” 14์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
30/62

์ปจํ…Œ์ด๋„ˆ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒํƒœ๊ฐ€ ์—†๋Š” ์•ฑ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•จ.

  • ์ƒํƒœ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ : ์ปจํ…Œ์ด๋„ˆ/ ๋…ธ๋“œ์— ์žฅ์•  ๋ฐœ์ƒ์‹œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ์ž์œ ๋กญ๊ฒŒ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋”ฐ๋Š” ๋œป

  • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์˜ฎ๊ธฐ๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ฒŒ ๋˜๋ฉด ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ๋˜๋Š”๋ฐ, ์ด๋ฅผ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•ด Volume ์ด๋ผ๋Š” ํŠน์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

  • ์ž„์‹œ ๋ถˆ๋ฅจ : emptyDir
  • ๋กœ์ปฌ ๋ถˆ๋ฅจ : hostPath
  • ๋„คํŠธ์›Œํฌ ๋ถˆ๋ฅจ : nfs
  • ํผ์‹œ์Šคํ„ดํŠธ ๋ถˆ๋ฅจ : pv, pvc

emptyDir

ํŒŒ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ํ˜ธ์ŠคํŠธ์˜ ๋””์Šคํฌ๋ฅผ ์ž„์‹œ๋กœ ์ปจํ…Œ์ด๋„ˆ์— ๋ถˆ๋ฅจ์œผ๋กœ ํ• ๋‹นํ•ด์„œ ์‚ฌ์šฉ
์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ณต์œ 

  • ํŒŒ๋“œ๊ฐ€ ์‚ฌ๋ผ์ง€๋ฉด emptyDir์— ํ• ๋‹นํ–ˆ๋˜ ๋ณผ๋ฅจ์˜ ๋ฐ์ดํ„ฐ๋„ ๊ฐ™์ด ์‚ฌ๋ผ์ง. (ํŒŒ๋“œ ๋‚ด์— ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ)
  • ๋ฉ”๋ชจ๋ฆฌ, ๋””์Šคํฌ์— ์‚ฌ์šฉ๋˜๋Š” ๋Œ€์šฉ๋Ÿ‰ ์—ฐ์‚ฐ๋ฐ์ดํ„ฐ์— ์‚ฌ์šฉ

https://kubernetes.io/ko/docs/concepts/storage/volumes/#emptydir

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: registry.k8s.io/test-webserver
    name: test-container
    volumeMounts:  # ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๋ถˆ๋ฅจ ๋งˆ์šดํŠธ ์œ„์น˜ 2)
    - mountPath: /cache
      name: cache-volume # ๋ถˆ๋ฅจ ๋งˆ์šดํŠธ ๋Œ€์ƒ ์ง€์ • v

  - image: another/image
    name: another-container
    volumeMounts:  # ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๋ถˆ๋ฅจ ๋งˆ์šดํŠธ ์œ„์น˜ 2)
    - mountPath: /another
      name: cache-volume # ๋ถˆ๋ฅจ ๋งˆ์šดํŠธ ๋Œ€์ƒ ์ง€์ • v
      readOnly: true # ํ•ด๋‹น ๋งˆ์šดํŠธ์—์„œ๋Š” ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ๋งŒ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    ports:
    - containerPort:80

  volumes: # ์ปจํ…Œ์ด๋„ˆ์˜ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ ๋Œ€์ƒ ์„ ์–ธ 1)
  - name: cache-volume  # ๋ถˆ๋ฅจ ์ด๋ฆ„ ์ง€์ • v
    emptyDir: {}

hostPath

ํŒŒ๋“œ๊ฐ€ ์‹คํ–‰๋œ ํ˜ธ์ŠคํŠธ์˜ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํŒŒ๋“œ์— ๋งˆ์šดํŠธํ•จ.
์ปจํ…Œ์ด๋„ˆ์™€ ๋…ธ๋“œ(ํ˜ธ์ŠคํŠธ) ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ณต์œ 

  • ํŒŒ๋“œ๋ฅผ ์žฌ ์‹œ์ž‘ํ•˜์—ฌ๋„ ํ˜ธ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚จ์•„์žˆ์Œ.
  • ๋…ธ๋“œ(ํ˜ธ์ŠคํŠธ) - ํŒŒ๋“œ - ์ปจํ…Œ์ด๋„ˆ
  • ํ˜ธ์ŠคํŠธ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ(์™ธ๋ถ€)์™€ ํŒŒ๋“œ ๋‚ด๋ถ€์˜ ์ €์žฅ์†Œ๋ฅผ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ
  • ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ํฌ๋“œ๋ผ๋ฆฌ ๋ฐ์ดํ„ฐ ๊ณต์œ  ๋ถˆ๊ฐ€๋Šฅ, ์˜๊ตฌ ์Šคํ† ๋ฆฌ์ง€

๊ฒฝ๊ณ :
HostPath ๋ณผ๋ฅจ์—๋Š” ๋งŽ์€ ๋ณด์•ˆ ์œ„ํ—˜์ด ์žˆ์œผ๋ฉฐ, ๊ฐ€๋Šฅํ•˜๋ฉด HostPath๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. HostPath ๋ณผ๋ฅจ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•„์š”ํ•œ ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ๋งŒ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๊ณ  ReadOnly๋กœ ๋งˆ์šดํŠธํ•ด์•ผ ํ•œ๋‹ค.

AdmissionPolicy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ์˜ HostPath ์•ก์„ธ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒฝ์šฐ, readOnly ๋งˆ์šดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ •์ฑ…์ด ์œ ํšจํ•˜๋ ค๋ฉด volumeMounts ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์ง€์ •๋˜์–ด์•ผ ํ•œ๋‹ค.

hostPath ํ™•์ธ


kubectl describe pod fluentbit-gke-jt8p8 --namespace kube-system

์›Œ์ปค๋…ธ๋“œ๋กœ hostPath ์‚ฌ์šฉ

https://kubernetes.io/ko/docs/concepts/storage/volumes/#hostpath

[์›Œ์ปค๋…ธ๋“œ] ๋ณผ๋ฅจ ๋งˆ์šดํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ

[๋งˆ์Šคํ„ฐ๋…ธ๋“œ] ๋ณผ๋ฅจ ๋งˆ์šดํŠธํ•  Pod ์‹คํ–‰

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-hostpath-pod
spec:
  containers:
  - name: kubernetes-hostpath-pod
    image: arisu1000/simple-container-app:latest
    volumeMounts:
    - mountPath: /test-volume
      name: hostpath-vol
    ports:
    - containerPort: 8080
  volumes:
  - name: hostpath-vol
    hostPath:
      path: /home/usr/hostPathDir
      type: Directory

[๋งˆ์Šคํ„ฐ ๋…ธ๋“œ] ํŒŒ๋“œ์•ˆ์— ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์— ์ ‘์†ํ•˜๊ณ , ํŒŒ์ผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ธฐ

kubectl exec kubernetes-hostpath-pod -it -- sh

~ # cd /test-volume
/test-volume # ls
/test-volume # echo "pod Wrote" > pod.txt
/test-volume # ls
pod.txt

[์›Œ์ปค ๋…ธ๋“œ] ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ ํ™•์ธ

[๋งˆ์Šคํ„ฐ ๋…ธ๋“œ] ํ•ด๋‹น Pod์ด ์–ด๋Š ๋…ธ๋“œ์—์„œ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ

[์›Œ์ปค๋…ธ๋“œ, ๋งˆ์Šคํ„ฐ๋…ธ๋“œ] ๋ฐ˜๋Œ€๋กœ ์›Œ์ปค๋…ธ๋“œ์—์„œ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋งˆ์Šคํ„ฐ๋…ธ๋“œ์—์„œ ํ™•์ธํ•˜๊ธฐ

์„œ๋กœ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋œ ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค!


GCE Persistence Disk

https://kubernetes.io/ko/docs/concepts/storage/volumes/#gcepersistentdisk


nfs

apt install nfs-common nfs-kernel-server portmap

ํŒŒ๋“œ ํ•˜๋‚˜์—์„œ ์•ˆ์ •์„ฑ์ด ๋†’์€ ์™ธ๋ถ€ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ๋ถˆ๋ฅจ์œผ๋กœ ์„ค์ •ํ•œ ํ›„ ํ•ด๋‹น ํŒŒ๋“œ์— NFS ์„œ๋ฒ„๋ฅผ ์„ค์ •.
-> ๋‹ค๋ฅธ ํŒŒ๋“œ๋Š” ํ•ด๋‹น ํŒŒ๋“œ์˜ NFS ์„œ๋ฒ„๋ฅผ nfs ๋ถˆ๋ฅจ์œผ๋กœ ๋งˆ์šดํŠธํ•จ.

nfs ์„œ๋ฒ„

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-server
  labels:
    app: nfs-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-server
  template:
    metadata:
      labels:
        app: nfs-server
    spec:
      containers:
      - name: nfs-server
        image: arisu1000/nfs-server:latest
        ports:
        - name: nfs
          containerPort: 2049
        - name: mountd #NFS ์„œ๋ฒ„ ํ”„๋กœ์„ธ์Šค (์š”์ฒญ์„ ์ง€์ • ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ) 
          containerPort: 20048
        - name: rpcbind #NFS ์„œ๋ฒ„ ํ”„๋กœ์„ธ์Šค (์‹œ์Šคํ…œ์—์„œ RPC ์„œ๋น„์Šค ๊ด€๋ฆฌ)
          containerPort: 111
        securityContext: #๋ณด์•ˆ ์„ค์ •  (์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰์ค‘์ธ ํ˜ธ์ŠคํŠธ ์žฅ์น˜์˜ ์ ‘๊ทผ๊ถŒํ•œ ์„ค์ •)
          privileged: true #(๋ชจ๋“  ํ˜ธ์ŠคํŠธ ์žฅ์น˜ ์ ‘๊ทผ ๊ฐ€๋Šฅ)
        volumeMounts: # ๋ถˆ๋ฅจ ๋งˆ์šดํŠธ ์„ค์ •
        - mountPath: /exports
          name: hostpath-vol
      volumes:
      - name: hostpath-vol
        hostPath:
          path: /tmp
          type: Directory

nfs ํด๋ผ์ด์–ธํŠธ

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubernetes-nfsapp-pod
  labels:
    app: nfs-client
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nfs-client
  template:
    metadata:
      labels:
        app: nfs-client
    spec:
      containers:
      - name: kubernetes-nfsapp-pod
        image: arisu1000/simple-container-app:latest
        volumeMounts:
        - mountPath: /test-nfs
          name: nfs-vol
        ports:
        - containerPort: 8080
      volumes:
      - name: nfs-vol
        nfs:
          server: 10.0.2.15
          path: "/"   

ContainerCreating

Events:
  Type     Reason       Age                  From               Message
  ----     ------       ----                 ----               -------
  Normal   Scheduled    3m36s                default-scheduler  Successfully assigned default/kubernetes-nfsapp-pod-78d656b47b-2ckhf to worker-1
  Warning  FailedMount  88s (x9 over 3m35s)  kubelet            MountVolume.SetUp failed for volume "nfs-vol" : mount failed: exit status 32
Mounting command: mount
Mounting arguments: -t nfs 10.0.2.15:/ /var/lib/kubelet/pods/0d1ade48-bd9f-4680-aa24-2138bdf3a0ba/volumes/kubernetes.io~nfs/nfs-vol
Output: mount: /var/lib/kubelet/pods/0d1ade48-bd9f-4680-aa24-2138bdf3a0ba/volumes/kubernetes.io~nfs/nfs-vol: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.

๋งˆ์šดํŒ…์— ํ•„์š”ํ•œ ํŒŒ์ผ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋žจ์ด ์„ค์น˜๋˜์ง€ ์•Š์•„์„œ ๊ทธ๋ ‡๋‹ค๊ณ ํ•œ๋‹ค.
๋ฃจํŠธ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰: apt install nfs-common nfs-kernel-server portmap
์›Œ์ปค๋…ธ๋“œ ๋งˆ์Šคํ„ฐ๋…ธ๋“œ ๋ชจ๋‘ ์‹คํ–‰ํ•ด์ฃผ์–ด์•ผํ•จ

์‹คํ–‰ ๊ฒฐ๊ณผ

nfs ์„œ๋ฒ„ ํŒŒ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” worker-2์˜ hostPath์— nfs-client๊ฐ€ ์ž‘์„ฑํ•œ ํŒŒ์ผ์ด ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋˜์„œ ์ž‘์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€