01-08 08:57
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[์‹ค์Šต] ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Controller ํ™œ์šฉ ๋ณธ๋ฌธ

Infra/Kubernetes

[์‹ค์Šต] ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Controller ํ™œ์šฉ

miinsun 2023. 6. 9. 16:11

๐Ÿ’ป ์‹ค์Šต ํ™˜๊ฒฝ

OS :  Ubuntu 20.04 LTS
RAM : <= 4GIB
CPU : <= 2Core

 

๐Ÿ“Œ ReplicaSet & ์Šค์ผ€์ผ ์•„์›ƒ

๋งˆ์Šคํ„ฐ์—์„œ ๋…ธ๋“œ 'rs-controller.yaml' ํ…œํ”Œ๋ฆฟ์„ ์ƒ์„ฑํ•œ๋‹ค.

$ vim rs-controller.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: app-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
        - name: nginx
          image: nginx:1.14

# ReplicaSet ์ƒ์„ฑ
$ kubectl create -f rs-controller.yaml

# pod ํ™•์ธ
$ kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
app-rs-ck4n7   1/1     Running   0          3s    10.36.0.1   node1.example.com   <none>           <none>
app-rs-j7mxm   1/1     Running   0          3s    10.44.0.2   node2.example.com   <none>           <none>
app-rs-v9dd8   1/1     Running   0          3s    10.44.0.1   node2.example.com   <none>           <none>

์ƒ์„ฑ๋œ Pod๋ฅผ ๋ณด๋ฉด Node1๊ณผ Node2์— ๋ชจ๋‘ app์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ€ป label ์ฃผ์˜

replicaset์€ label๋กœ Pod๋ฅผ ์‹๋ณ„ํ•จ

# pod์˜ ๋ผ๋ฒจ ํ™•์ธ
$ kubectl get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
app-rs-ck4n7   1/1     Running   0          117s   app=webui
app-rs-j7mxm   1/1     Running   0          117s   app=webui
app-rs-v9dd8   1/1     Running   0          117s   app=webui

# label ์ œ๊ฑฐ
$ kubectl label pod/app-rs-ck4n7 app-
pod/app-rs-ck4n7 unlabeled

# label ํ™•์ธ
# replica์— ํ•ด๋‹นํ•˜๋Š” ๋ผ๋ฒจ์ด 3๊ฐœ์—์„œ 2๊ฐœ๋กœ ์ค„์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์‹œ 1๊ฐœ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
$ kubectl get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
app-rs-ck4n7   1/1     Running   0          3m51s   <none>
app-rs-j7mxm   1/1     Running   0          3m51s   app=webui
app-rs-thwjb   1/1     Running   0          8s      app=webui
app-rs-v9dd8   1/1     Running   0          3m51s   app=webui

# label ์ถ”๊ฐ€
$ kubectl label pod/app-rs-ck4n7 app=webui
pod/app-rs-ck4n7 labeled
# webui๋ฅผ ๊ฐ–๋Š” ๋ผ๋ฒจ์ด 4๊ฐœ๊ฐ€ ๋๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ์„ฑ ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ์งง์€ app-rs-thwjb๊ฐ€ ์ œ๊ฑฐ ๋˜์—ˆ๋‹ค
root@master:~/test# kubectl get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
app-rs-ck4n7   1/1     Running   0          7m20s   app=webui
app-rs-j7mxm   1/1     Running   0          7m20s   app=webui
app-rs-v9dd8   1/1     Running   0          7m20s   app=webui

 

- ReplicaSet ScaleOut

๋™์ž‘ ์ค‘์ธ pod์˜  scale์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ edit, scale, apply๋กœ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

๊ทธ ์ค‘ scale์„ ์ด์šฉํ•ด ๋™์ž‘์ค‘์ธ replicaSet์˜ scale์„ ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

# scale์„ ์‚ฌ์šฉํ•œ update
$ kubectl scale rs app-rs --replicas=10
replicaset.apps/app-rs scaled

# pod๊ฐ€ 3๊ฐœ์—์„œ 10๊ฐœ๋กœ ๋Š˜์—ˆ๋‹ค
$ kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
app-rs-8m7m5   1/1     Running   0          18s   10.36.0.3   node1.example.com   <none>           <none>
app-rs-ck4n7   1/1     Running   0          10m   10.36.0.1   node1.example.com   <none>           <none>
app-rs-dw2f2   1/1     Running   0          18s   10.44.0.4   node2.example.com   <none>           <none>
app-rs-j7mxm   1/1     Running   0          10m   10.44.0.2   node2.example.com   <none>           <none>
app-rs-lgx8f   1/1     Running   0          18s   10.36.0.5   node1.example.com   <none>           <none>
app-rs-nmtc6   1/1     Running   0          18s   10.36.0.4   node1.example.com   <none>           <none>
app-rs-q7xtb   1/1     Running   0          18s   10.44.0.3   node2.example.com   <none>           <none>
app-rs-qf7pz   1/1     Running   0          18s   10.44.0.5   node2.example.com   <none>           <none>
app-rs-rgglj   1/1     Running   0          18s   10.36.0.2   node1.example.com   <none>           <none>
app-rs-v9dd8   1/1     Running   0          10m   10.44.0.1   node2.example.com   <none>           <none>

# nginx ์ ‘์†
$ curl 10.36.0.3
<!DOCTYPE html>
<html>
...

 

- ReplicaSet delete

ํ˜„์žฌ ๋™์ž‘์ค‘์ธ pod๋“ค์€ replicaSet์— ์ข…์†๋˜๊ธฐ ๋•Œ๋ฌธ์— replicaSet์„ ์‚ญ์ œํ•˜๋ฉด ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.

๊ทธ๋ ‡์ง€๋งŒ replicaSet์„ ์‚ญ์ œํ•  ๋•Œ --cascade ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด replicaSet๋งŒ์„ ์‚ญ์ œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

# Replicaset์„ ์‚ญ์ œํ•˜๋ฉด ์—ฐ๊ด€๋œ ๋ชจ๋“  pod๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.
$ kubectl delete rs app-rs
replicaset.apps "app-rs" deleted
$ kubectl get pod
No resources found in default namespace.

# cascade ์˜ต์…˜ ์ถ”๊ฐ€
$ kubectl delete rs app-rs --cascade=false

# ReplicaSet์€ ์‚ญ์ œ๋˜์—ˆ์ง€๋งŒ pod๋Š” ์‚ญ์ œ๋˜์ง€ ์•Š์•˜๋‹ค
$ kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
app-rs-8m7m5   1/1     Running   0          6m36s
app-rs-ck4n7   1/1     Running   0          17m
app-rs-dw2f2   1/1     Running   0          6m36s
app-rs-j7mxm   1/1     Running   0          17m
app-rs-v9dd8   1/1     Running   0          17m

 

 

๐Ÿ“Œ Deploy & RollingUpdate 

๋งˆ์Šคํ„ฐ์—์„œ ๋…ธ๋“œ 'deploy-controller.yaml' ํ…œํ”Œ๋ฆฟ์„ ์ƒ์„ฑํ•œ๋‹ค.

$ vim deploy-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-app
spec:
  replicas: 5
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.14

# deploy ์ƒ์„ฑ
$ kubectl create -f deploy-controller.yaml
deployment.apps/deploy-app created

# ์ƒ์„ฑ๋œ resource ํ™•์ธ
$ kubectl get po,rs,deploy
NAME                              READY   STATUS    RESTARTS   AGE
pod/deploy-app-7944565747-2vh77   1/1     Running   0          17s
pod/deploy-app-7944565747-dcdgw   1/1     Running   0          17s
pod/deploy-app-7944565747-j7w4b   1/1     Running   0          17s
pod/deploy-app-7944565747-mr972   1/1     Running   0          17s
pod/deploy-app-7944565747-nnmct   1/1     Running   0          17s

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/deploy-app-7944565747   5         5         5       17s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deploy-app   5/5     5            5           17s

 

- RollingUpdate

nginx์ด๋ฏธ์ง€๋ฅผ ๊ธฐ์กด 1.14์—์„œ 1.15๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•œ๋‹ค.

# nginx ์ด๋ฏธ์ง€ upgrade
$ kubectl set image deploy deploy-app nginx-container=nginx:1.15 --record
deployment.apps/deploy-app image updated

# ๋ฐฐํฌ ์ค‘์ง€
$ kubectl rollout pause deploy deploy-app

# ๋ฐฐํฌ ์žฌ์‹คํ–‰
$ kubectl rollout resume deploy deploy-app

์•„๋ž˜ ์‚ฌ์ง„์„ ๋ณด๋ฉด ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ๊ธฐ๊ณ , ๊ธฐ์กด์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” ์‚ญ์ œ๋˜๋Š” ๋ชจ์Šต์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- Update ๋ฒ„์ „ ๊ด€๋ฆฌ

์ง€๊ธˆ๊นŒ์ง€ ๋ฒ„์ „์„ ๋ฐ”๊ฟจ๋˜ ๊ธฐ๋ก์„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๋ฒ„์ „ ๊ธฐ๋ก ํ™•์ธ
$ kubectl rollout history deployment deploy-app
deployment.apps/deploy-app
REVISION  CHANGE-CAUSE
1         kubectl set image deploy deploy-app web=nginx:1.15 --record=true
2         kubectl set image deploy deploy-app nginx-container=nginx:1.15 --record=true
3         kubectl set image deploy deploy-app nginx-container=nginx:1.16 --record=true
4         kubectl set image deploy deploy-app nginx-container=nginx:1.17 --record=true
6         kubectl set image deploy deploy-app nginx-container=nginx:1.17 --record=true
7         kubectl set image deploy deploy-app nginx-container=nginx:1.18 --record=true

# ๋ฒ„์ „ 1.15(2)๋กœ ๋กค๋ฐฑ
$ kubectl rollout undo deployment deploy-app --to-revision=2
deployment.apps/deploy-app rolled back

# Pod ํ™•์ธ
$ kubectl describe pod deploy-app-78444bbfbb-4kq4w
...
Containers:
  nginx-container:
    Container ID:   docker://7b738c9728153f190184394ef8353585e2904c33327509b424142e7af910ee3f
    Image:          nginx:1.15
    Image ID:       docker-pullable://nginx@sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
    Port:           <none>
    Host Port:      <none>
    State:          Running

 

-  Annotation์„ ์ด์šฉํ•œ history ๊ด€๋ฆฌ

# nginx:1.14๋กœ deploy template ์ƒ์„ฑ
$ vim deploy-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-app
  annotations:
    kubernetes.io/change-cause: version 1.14
spec:
  replicas: 5
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.14

# deploy ์ƒ์„ฑ
$ kubectl create -f deploy-controller.yaml

# nginx:1.15๋กœ ์ˆ˜์ •
$ vim deploy-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-app
  annotations:
    kubernetes.io/change-cause: version 1.15
spec:
  replicas: 5
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.15
        
# ๋ฐ”๋€ ๋‚ด์—ญ ์ ์šฉ
$ kubectl apply -f deploy-controller.yaml

# ๋ฒ„์ „ ํ™•์ธ
$ kubectl rollout history deployment deploy-app
deployment.apps/deploy-app
REVISION  CHANGE-CAUSE
1         version 1.14
2         version 1.15
Comments