๐ŸŽจ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค [๋ฉ€ํ‹ฐ ์ปจํ…Œ์ด๋„ˆ, init ์ปจํ…Œ์ด๋„ˆ]

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

[DevOps] ๐ŸณDocker & Kubernetes

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

๋ฉ€ํ‹ฐ ์ปจํ…Œ์ด๋„ˆ

ํ•œ ํฌ๋“œ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ๋„คํŠธ์›Œํฌ ์ธํ„ฐํŽ˜์ด์Šค, IPC, ๋ณผ๋ฅจ ๋“ฑ์„ ๊ณต์œ  ๊ฐ€๋Šฅํ•จ.

  • ์ง€์—ญ์„ฑ ๋ณด์žฅ
  • ๋ฆฌ์†Œ์Šค ๋ชจ๋‹ˆํ„ฐ๋ง ์šฉ๋„๋กœ ๊ตฌ์ถ•ํ•˜๊ธฐ๋„ ํ•จ.
apiVersion: v1
kind: Pod
metadata:
  name: mc1
spec:
  volumes:
  - name: html
    emptyDir: {}
  containers:
  - name: 1st
    image: nginx
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      
  - name: 2nd
    image: debian
    volumeMounts:
    - name: html
      mountPath: /html
    command: ["/bin/sh", "-c"]
    args:
      - while true; do
          date >> /html/index.html;
          sleep 1;
        done

ํŒŒ๋“œ๋‚ด์— ์ปจํ…Œ์ด๋„ˆ ๊ฐ„ ํฌํŠธ๋ฒˆํ˜ธ ์•ˆ ๊ฒน์น˜๊ฒŒ ์ฃผ์˜


Init ์ปจํ…Œ์ด๋„ˆ

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

ํŒŒ๋“œ์˜ ์•ฑ ์ปจํ…Œ์ด๋„ˆ๋“ค์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ํŠน์ˆ˜ํ•œ ์ปจํ…Œ์ด๋„ˆ.

  • ์•ฑ ์ด๋ฏธ์ง€์—๋Š” ์—†๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋˜๋Š” ์„ค์ • ์Šคํฌ๋ฆฝํŠธ ๋“ฑ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

Init ์ปจํ…Œ์ด๋„ˆ

  • ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋Š” ํ•ญ์ƒ ์™„๋ฃŒ๋ฅผ ๋ชฉํ‘œ๋กœ ์‹คํ–‰๋œ๋‹ค.
  • ๊ฐ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋Š” ๋‹ค์Œ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹œ์ž‘๋˜๊ธฐ ์ „์— ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ๋งŒ์•ฝ ํŒŒ๋“œ์˜ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํŒจํ•˜๋ฉด, kubelet์€ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„ฑ๊ณตํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์žฌ์‹œ์ž‘ํ•œ๋‹ค. -> (์™„์ „ํžˆ ์ดˆ๊ธฐํ™”๊ฐ€ ์ง„ํ–‰ ๋œ ๋‹ค์Œ ์ฃผ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰)
  • ๊ทธ๋Ÿฌ๋‚˜, ๋งŒ์•ฝ ํŒŒ๋“œ์˜ restartPolicy ๋ฅผ ์ ˆ๋Œ€ ํ•˜์ง€ ์•Š์Œ(Never)์œผ๋กœ ์„ค์ •ํ•˜๊ณ , ํ•ด๋‹น ํŒŒ๋“œ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋™์•ˆ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํŒจํ•˜๋ฉด, ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ์ „์ฒด ํŒŒ๋“œ๋ฅผ ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

initContainers.container ํ•ญ๋ชฉ์— ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€
.status.initContainerStatuses : ์ปจํ…Œ์ด๋„ˆ ์ƒํƒœ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋จ.

์ผ๋ฐ˜ ์ปจํ…Œ์ด๋„ˆ์™€ ์ฐจ์ด์ 

  • ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋Š” ์•ฑ ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฆฌ์†Œ์Šค ์ƒํ•œ(limit), ๋ณผ๋ฅจ, ๋ณด์•ˆ ์„ธํŒ…์„ ํฌํ•จํ•œ ๋ชจ๋“  ํ•„๋“œ์™€ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•œ๋‹ค.
  • ๋งŒ์•ฝ ๋‹ค์ˆ˜์˜ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํŒŒ๋“œ์— ์ง€์ •๋˜์–ด ์žˆ๋‹ค๋ฉด, kubelet์€ ํ•ด๋‹น ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์‹คํ–‰ํ•œ๋‹ค.
  • ๊ฐ ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋Š” ๋‹ค์Œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ๊ผญ ์„ฑ๊ณตํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“  ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ๋“ค์ด ์‹คํ–‰ ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ, kubelet์€ ํŒŒ๋“œ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ํ‰์†Œ์™€ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค

Init ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ

์ฝ”๋“œ

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app.kubernetes.io/name: MyApp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

containers: ์ฃผ ์ปจํ…Œ์ด๋„ˆ
initContainers: ์ดˆ๊ธฐํ™” ์ปจํ…Œ์ด๋„ˆ
-> myservice, mydb๊ฐ€ ์‹คํ–‰๋œ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์ „๊นŒ์ž ์ดˆ๊ธฐํ™”

  • initService ์‹คํ–‰ ํ™•์ธ ์„œ๋น„์Šค
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377

์‹คํ–‰

init ์ปจํ…Œ์ด๋„ˆ ๋จผ์ € ์‹คํ–‰ํ•˜๋ฉด Status : Init์œผ๋กœ ๊ณ„์† ์ง„ํ–‰๋จ

Init ์กฐ๊ฑด ๋งŒ์กฑํ•˜๊ณ  Running์œผ๋กœ ์ƒํƒœ ๋ณ€๊ฒฝ


profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

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