나그네소

kubernetes controller DEPLOYMENT 3탄 본문

Cloud/Kubernetes

kubernetes controller DEPLOYMENT 3탄

나그네소티 2022. 11. 18. 16:23

[Controller 종류]

오널은 Deployment에 대해 알아 본다.

 

1. DEPLOYMENT 의미

controller 종류중에 하나이며 Replicaset 과 동일한 역할 아지만 REPLICASET의 부모 역할을 하는게
DEPLOYMENT이다. Deployment는 Rolling update을 의해 만들어 졌다. 
  • ReplicaSet을 컨트롤해서 Pod수를 조절
  • Rolling Update & Rolling Back

참고 : 롤링 업데이트란?

서비스 중단 없이 Pod Instance의 버젼을 update 시켜 주는 것을 얘기 한다. 

 

2. Deployment definition

ReplicatSet definition
Deployment definition
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rs-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

: kind만 빼면 Replicaset이랑 완전히 똑같이 Controller 인것을 알 수 있다. 

 

3. deploy-nginx.yaml 예제

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
kuber-master:/home/son_kube/tmp] k get deployment,rs,pod
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deploy-nginx   3/3     3            3           4m55s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/deploy-nginx-5cfbcf5f65   3         3         3       4m55s

NAME                                READY   STATUS    RESTARTS   AGE
pod/deploy-nginx-5cfbcf5f65-5k86t   1/1     Running   0          4m55s
pod/deploy-nginx-5cfbcf5f65-986j2   1/1     Running   0          4m55s
pod/deploy-nginx-5cfbcf5f65-c7hhm   1/1     Running   0          4m55s

deploy-nginx : deployment
nginx-5cfbcf5f65 : controller
5k86t  : pod name
* pod -> rc -> deployment
: pod 부모가 컨트롤러 컨트로러의 부모 디플로인 것을 알 수 있다.

 

4. Deployment Rolling update & rolling back

  • Rolling update
    k set image deployment <deploy_name> <container_name>=<new_version_imnage>
  • Rollback
    • k rollout history deployment <deply_name>
    • k rollout undo deploy <deploy_name>

 

4-1) Deployment Rolling update Example

[deployment-exam1.yaml]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deploy
spec:
  selector:
    matchLabels:
      app: webui
  replicas: 3
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
      - image: nginx:1.14
        name: web
        ports:
        - containerPort: 80

// deployment create
kuber-master:/home/son_kube/tmp] k create -f deployment-exam1.yaml
deployment.apps/app-deploy created

// pod 확인
kuber-master:/home/son_kube/tmp] k get rs,deployment,pods
NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/app-deploy-86c8cbb88   3         3         3       13s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-deploy   3/3     3            3           13s

NAME                             READY   STATUS    RESTARTS   AGE
pod/app-deploy-86c8cbb88-hq9rg   1/1     Running   0          13s
pod/app-deploy-86c8cbb88-lw2gm   1/1     Running   0          13s
pod/app-deploy-86c8cbb88-qqdp9   1/1     Running   0          13s

 

4-2) Rolling Update & Rollback 기록을 한 볼까나~~~

// 현재 deployment 상태
kuber-master:/home/son_kube/tmp] k get deployment
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
app-deploy   3/3     3            3           2m57s

kuber-master:/home/son_kube/tmp] k rollout history deployment app-deploy
deployment.apps/app-deploy
REVISION  CHANGE-CAUSE
1         <none>

: <none> history 기록이 나오지 않고 있다. 이유는 deployment 생성 시 record라는 옵션을 
주지 않고 생성 했기에 알 수 없다.


// record를 주어 history 보기
k create -f deployment-exam1.yaml --record
: record 옵션을 주어 (history 기록을 하기 위에) 다시 생성 한다.

kuber-master:/home/son_kube/tmp] k get deployment
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
app-deploy   3/3     3            3           22s

kuber-master:/home/son_kube/tmp] k rollout history deployment app-deploy
deployment.apps/app-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-exam1.yaml --record=true
: <none> 없이 기록 된 것을 알 수 있다.

 

4-3) Rolling Update

//nginx 1.14->1.15 update 처리
kuber-master:/home/son_kube/tmp] k set image deployment app-deploy web=nginx:1.15 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/app-deploy image updated

kuber-master:/home/son_kube/tmp] k rollout status deployment app-deploy
Waiting for deployment "app-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "app-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "app-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "app-deploy" successfully rolled out
: 현재 상태의 과정을 볼 수 있다.

kuber-master:/home/son_kube/tmp] k get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP             NODE          NOMINATED NODE   READINESS GATES
app-deploy-cf8c7477c-4t8xt   1/1     Running   0          12s   10.244.1.251   kuber-node1   <none>           <none>
app-deploy-cf8c7477c-hv66v   1/1     Running   0          23s   10.244.2.65    kuber-node2   <none>           <none>
app-deploy-cf8c7477c-l62nh   1/1     Running   0          3s    10.244.2.66    kuber-node2   <none>           <none>

kuber-master:/home/son_kube/tmp] k describe pods app-deploy-cf8c7477c-l62nh
....
Image:          nginx:1.15
.....

참고] 진행 상태를 멈추었다가 다시 진행 할 수 있는 명령어도 있다. 

kuber-master:/home/son_kube/tmp] k set image deployment app-deploy web=nginx:1.15 --record

//멈쳐...
kuber-master:/home/son_kube/tmp] k rollout pause deployment app-deploy
deployment.apps/app-deploy paused

//다시해라~
kuber-master:/home/son_kube/tmp] k rollout resume deployment app-deploy
deployment.apps/app-deploy resumed
: 상태를 확인하면 update 진행 할 수 있다.

* 현재의 업데이트 상태를 확인
kuber-master:/home/son_kube/tmp] k rollout history deployment app-deploy
deployment.apps/app-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-exam1.yaml --record=true
2         kubectl set image deployment app-deploy web=nginx:1.15 --record=true
3         kubectl set image deployment app-deploy web=nginx:1.16 --record=true
4         kubectl set image deployment app-deploy web=nginx:1.17 --record=true

 

4-4) deployment-exam2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
  annotations:
    kubernetes.io/change-cause: version 1.15
spec:
  progressDeadlineSeconds: 600
  revisionHistoryLimit: 10
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
      - name: web
        image: nginx:1.15
        ports:
        - containerPort: 80
progressDeadlineSeconds 10분안에 update 하지 못하면 중지 하겠다
: maxSurge rolling update 처리 시 run되고 있는 pod수의 비율을 조절 할 수 있다
  replicas=3 50% = 1.5(반올림) : 2 ==> 3(기존replica수) + 2 = 5개 //총5개 
  replicas=3 25% = 0.75(반올림):1 ==> 3(기존replca수) + 1 = 4개 //총4개
  runiung중인것을 총4개까지만 유지 한다. // 얼만나 빠르게 update 하는지를 컨토롤 할 수 있다.
: maxUnavailable  rolling update 처리 시 없어지는 pod수의 비율을 조절 할 수 있다.
: RollingUpdate 바로 update 처리 하겠다. 

 

5. Rollback

 

5-1) rollback을 해보자.

kuber-master:/home/son_kube/tmp] k rollout history deployment app-deploy
deployment.apps/app-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-exam1.yaml --record=true
2         kubectl set image deployment app-deploy web=nginx:1.15 --record=true
3         kubectl set image deployment app-deploy web=nginx:1.16 --record=true
4         kubectl set image deployment app-deploy web=nginx:1.17 --record=true
  • 현재 nginx:1.17 단계인데 바로 전단계로 rollback
    kuber-master:/home/son_kube/tmp] k rollout undo deployment app-deploy
    : 바로 전단계로 Rollback 처리 된다. (위의 예제이면 1.1.6 version 으로)
  • revision( 버젼을 지정하여 rollback)
kuber-master:/home/son_kube/tmp] k rollout undo deployment app-deploy --to-revision=2
deployment.apps/app-deploy rolled back
kuber-master:/home/son_kube/tmp] k rollout history deployment app-deploy
deployment.apps/app-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-exam1.yaml --record=true
3         kubectl set image deployment app-deploy web=nginx:1.16 --record=true
4         kubectl set image deployment app-deploy web=nginx:1.17 --record=true
5         kubectl set image deployment app-deploy web=nginx:1.15 --record=true

: 위와 같이 revision을 지정하여 rollback 처리 할 수 도 있다. 

 

6. yaml 파일을 가지고 deployment rolling update & rollback

[deployment-exam2.yaml]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
  annotations:
    kubernetes.io/change-cause: version 1.15
spec:
  progressDeadlineSeconds: 600
  revisionHistoryLimit: 10
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
      - name: web
        image: nginx:1.14
        ports:
        - containerPort: 80
annotations 이용하여 kubernetes 동작을 제어 할수 있게 해준다.
: annotations 이용하여 kubernetes version을 바꾸어 주면 (1.1.6) rolling update 해준다.

 

6-1) example

- k create -f deployment-exam2.yaml
- kuber-master:/home/son_kube/tmp] k edit deployment deploy-nginx
- annotations:
    deployment.kubernetes.io/revision: "1"
    kubernetes.io/change-cause: version 1.16(1.15->1.16)
- image: nginx:1.16 (1.15->1.16)
kuber-master:/home/son_kube/tmp] k rollout history deployment deploy-nginx
deployment.apps/deploy-nginx
REVISION  CHANGE-CAUSE
1         version 1.14
2         version 1.15
3         version 1.16