๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- linux, aws, docker, k8s

[์ฟ ๋ฒ„๋„คํ‹ฐ์Šค] k8s์˜ ๊ธฐ๋ณธ ๋‹จ์œ„์ธ ํฌ๋“œ (Pods)์™€ ํฌ๋“œ ์ƒ์„ฑํ•˜๊ธฐ

by Wonit 2021. 5. 13.

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ์ปจํ…Œ์ด๋„ˆ๋“ค์˜ Life cycle ์—์„œ ๋ถ€ํ„ฐ scheudling ๊นŒ์ง€ ์ „๋ฐ˜์ ์ธ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ ๋„๊ตฌ๋ผ๊ณ  ํ•˜์˜€๋‹ค.

 

์ด๋Ÿฐ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ํฌ์ปค์Šค๊ฐ€ ์ปจํ…Œ์ด๋„ˆ์— ๋งž์ถฐ์ ธ ์žˆ๋‹ค.

 

๋„์ปค์—์„œ๋Š” ๊ธฐ๋ณธ ๋‹จ์œ„๊ฐ€ ์ปจํ…Œ์ด๋„ˆ ์˜€์—ˆ์ง€๋งŒ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ๋Š” ๊ธฐ๋ณธ ๋‹จ์œ„๋ฅผ ํฌ๋“œ, Pods ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

์˜ค๋Š˜์€ ์ด ํฌ๋“œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

 

ํฌ๋“œ, Pods

ํฌ๋“œ๋Š” ์•ž์„œ ์ด์•ผ๊ธฐ ํ•˜์˜€๋“ฏ ์ปจํ…Œ์ด๋„ˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ๋™ํ•˜๊ธฐ ์œ„ํ•ด ๊ผญ ํ•„์š”ํ•œ ์˜ค๋ธŒ์ ํŠธ(Object) ์ด๋‹ค.

 

์ฆ‰, ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ ๋‹จ์œ„๋ฅผ ๋ง ํ•˜๋ฉฐ ์šฐ๋ฆฌ๋Š” ํฌ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ€๋™์‹œ๊ณ  ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ํฌ๋“œ๋ฅผ ์ด์šฉํ•ด ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.

 

ํฌ๋“œ ๋‚ด๋ถ€์˜ ์ปจํ…Œ์ด๋„ˆ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Shared Context ์—์„œ ์‹คํ–‰๋˜์–ด์„œ ๋ฆฌ๋ˆ…์Šค์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ ์ปจํŠธ๋กค ๊ทธ๋ฃน (cgroup)์„ ํ•จ๊ป˜ ๊ณต์œ ํ•œ๋‹ค.

 

 

์ด ๊ฐœ๋…์ด ํฌ๋“œ์™€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋ถ„ ์ง“๋Š” ์•„์ฃผ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด ๋˜๋Š”๋ฐ ์ž˜ ๊ธฐ์–ต ํ•ด๋‘๋„๋ก ํ•˜์ž

๊ทธ๋ฆฌ๊ณ  ํ•˜๋‚˜์˜ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ์—์„œ ํ•˜๋‚˜์˜ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ–ˆ๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์˜ ํฌ๋“œ๋Š” ํ•˜๋‚˜์˜ ํฌ๋“œ์—์„œ ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๊ฐœ๋…์€ ํ›„์— ๋ฐฐ์šธ Sidecar ํŒจํ„ด์ด๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ, ๋ณดํ†ต ์‹ค์งˆ์ ์ธ ๊ธฐ๋Šฅ ๋™์ž‘์„ ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…Œ์ด๋„ˆ ํ•˜๋‚˜์™€ ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์กฐ์ž‘ํ•˜๋Š” ์ฐฝ๊ตฌ์™€ ๋น„์Šทํ•œ ์ปจํ…Œ์ด๋„ˆ ํ•˜๋‚˜๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๊ตฌ์„ฑํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

 

ํฌ๋“œ ๋‚ด์— ๊ณต์œ ๋˜๋Š” ๋ฆฌ์†Œ์Šค

์•„๊นŒ ์•ž์—์„œ ์ด์•ผ๊ธฐ ํ–ˆ๋˜ Shared Context์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ์ด์•ผ๊ธฐ ํ•ด๋ณด์ž.

 

๋งŒ์•ฝ ํ•˜๋‚˜์˜ ํฌ๋“œ์—์„œ ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ๋™ ์‹œ์ผฐ๋‹ค๊ณ  ํ•ด๋ณด์ž.

 

๊ทธ๋Ÿผ ํฌ๋“œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ 2๊ฐœ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. Pod Storage
  2. Pod Networking

๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ ๋„์ปค๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ์–ด๋• ์„๊นŒ? ์šฐ์„  ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋งŒ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๊ณ  ๋„์ปค ๋ธŒ๋ฆฟ์ง€ ๋„คํŠธ์›Œํฌ(Bridge Network)๋ฅผ ๊ตฌ์„ฑํ–ˆ์–ด์•ผ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์ด์ œ ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•

ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค.

  1. ์ง์ ‘ ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ
  2. ํฌ๋“œ๋ฅผ ๊ฐ์‹ธ๋Š” ์ƒ์œ„ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

 

์šฐ๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„ ํฌ๋“œ๋ฅผ ๊ฐ์‹ธ๋Š” ์ƒ์œ„ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งŽ์ด ๋ณผ ๊ฒƒ์ด๋‹ค.


๊ทธ๋ž˜๋„ ๊ฐ„๋‹จํ•˜๋‹ˆ ๋‘ ๋ฐฉ๋ฒ• ๋ชจ๋‘๋ฅผ ์•Œ์•„๋ณด์ž.

 

์ง์ ‘ ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ

์ง์ ‘ ํฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” yml ํŒŒ์ผ์— kind๋ฅผ Pod ๋กœ ์ง€์ •ํ•˜๊ณ  spec์— ํฌ๋“œ๊ฐ€ ์‹คํ–‰์‹œํ‚ฌ ์ปจํ…Œ์ด๋„ˆ ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: service-container-name
      images: my-service:v1.0
      ports:
        - containerPort: 80
          protocol: TCP
  • apiVersion
    • yml ํŒŒ์ผ์—์„œ ์ •์˜ํ•œ ์˜ค๋ธŒ์ ํŠธ์˜ API ๋ฒ„์ „์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • kind
    • ํ•ด๋‹น yml ํŒŒ์ผ์— ์กด์žฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค
  • metadata
    • ์ฃผ์„, ์ด๋ฆ„, ๋ผ๋ฒจ๊ณผ ๊ฐ™์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
  • spec
    • ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

ํฌ๋“œ๋ฅผ ๊ฐ์‹ธ๋Š” ์ƒ์œ„ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์šฐ๋ฆฌ๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

ํฌ๋“œ์˜ ์ƒ์œ„ ๋ฆฌ์†Œ์Šค๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • ReplicaSet
  • StatefulSet
  • Deployment

์ด๋“ค์€ ํฌ๋“œ๋ฅผ ์กฐ์ž‘ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์ƒ์œ„ ๋ฆฌ์†Œ์Šค๋“ค์ธ๋ฐ, ์ƒ์œ„ ๋ฆฌ์†Œ์Šค์—์„œ ํฌ๋“œ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” PodTemplate ์— ํฌ๋“œ์˜ spec, ์†์„ฑ์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์•˜์ง€๋งŒ ReplicaSet ์˜ ๊ฒฝ์šฐ๋ฅผ ๊ฐ„๋‹จํžˆ ๋ด๋ณด์ž.

 

apiVersion: v1
kind: ReplicaSet # ํฌ๋“œ์˜ ์ƒ์œ„ ๋ฆฌ์†Œ์Šค์ธ ๋ ˆํ”Œ๋ฆฌ์นด ์…‹
metadata:
  name: my-replicaset
spec:
  replicas: 4
  selector:
    matchLabels:
      app: my-pod-label
  template: # Pod Template (ํฌํŠธ ์†์„ฑ ์ •์˜)
    metadata:
      name: my-pod
      labels:
        app: my-pod-label
    spec:
      containers:
        - name: nginx-pod
          image: nginx
          ports:
            - containerPort: 80

kubectl๋กœ yml ์— ํ•ด๋‹น ํ•˜๋Š” ํฌ๋“œ ์˜ค๋ธŒ์ ํŠธ ์‹คํ–‰ํ•˜๊ธฐ

์ด์ œ kubectl ์„ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  2๊ฐ€์ง€ yml ํŒŒ์ผ์„ ์ด์šฉํ•ด ํฌ๋“œ๋“ค์„ ๊ตฌ๋™์‹œ์ผœ๋ณด์ž.

  1. apply ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ yml ํŒŒ์ผ ์‹คํ–‰ํ•˜๊ธฐ
  2. pods ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ
  3. replica set ์ƒํƒœํ™•์ธํ•˜๊ธฐ

apply ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ yml ํŒŒ์ผ ์‹คํ–‰ํ•˜๊ธฐ

yml ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๊ฒ ์ง€๋งŒ ๋Œ€ํ‘œ์ ์œผ๋กœ kubectl apply ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

apply ๋ช…๋ น์–ด์—๋Š” ์˜ต์…˜์„ ์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ -f ์˜ต์…˜์„ ์ค˜์„œ yml ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ค๋„๋ก ํ•ด๋ณด์ž.

 

$ kubectl apply -f my-pod.yml
pod/my-pod created

$ kubectl apply -f my-replicaset.yml
replicaset.apps/my-replicaset created

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด created ์ƒํƒœ๋กœ ํฌ๋“œ์™€ replicaset์ด ๋ชจ๋‘ ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

 

pods ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ

pod ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” kubectl get pods ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

pod๋งŒ ์ƒ์„ฑํ•œ yml๊ณผ replicset ์œผ๋กœ ์ƒ์„ฑํ•œ ํฌ๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž˜ ๋„์›Œ์กŒ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

 

$ kubectl get pods

NAME            READY   STATUS    RESTARTS   AGE
my-pod          1/1     Running   0          3m55s
my-replicaset   1/1     Running   0          2m51s
my-replicaset   1/1     Running   0          2m51s
my-replicaset   1/1     Running   0          2m51s
my-replicaset   1/1     Running   0          2m51s

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ํฌ๋“œ๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋Œ“๊ธ€