본문으로 바로가기

쿠버네티스 - Deployment

category Kubernetes 2021. 7. 17. 04:20
반응형

Deployment란 controller Replicaset을 제어해주는 부모역할을 함.

  • Replicaset을 컨트롤해서 Pod수를 조절

목적 : Rolling update 

Rolling update? : 서비스의 중단 없이 업데이트를 수행하는 것.

 

출처 : 따배쿠 유튜브 강의 https://www.youtube.com/watch?v=L5LDBWrP6QU&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=19 

Deployment는 Replicaset을 통해 파드를 두개 실행 시킨다.

차이점은 kind 부분밖에 없다.

그전에 만들었던 replica yaml 파일의 kind를 deployment로 수정 후 deployment를 생성해 봤다.

deployment 생성 yaml 파일

 

kubectl get pods 조회한 결과 
kubectl get rs 조회결과
kubectl get deployment로 조회 결과

빨강색 네모 : Pod의 이름

파랑색 네모 : replicaset의 이름

초록색 네모 : Deployment의 이름 

파드-레플리카셋-디플로이먼트 순으로 자식 > 부모 구조로 이루어져 있는것을 확인할 수 있다

 

Deployment의 목적인 롤링 업데이트를 해주기 위한 명령어

kubectl set image deployment [deploy_name] [container_name]=[new_version_image]

 

RollBack 명령어

kubectl rollout history deployment [deploy_name]

kubectl rollout undo deploy [deploy_name]

 

 

실습 : Deployment를 yaml 파일로 생성하고, 롤링 업데이트, 롤백 해보기.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: plpl
spec:
  replicas: 2
  selector:
    matchLabels:
      app: main
  template:
    metadata:
      name: apache-rs
      labels:
        name: apache
        app: main
        rel: stable
    spec:
      containers:
      - name: web
        image: nginx:1.14

<deployment-exam1.yaml 파일>

1. deployment 생성

kubectl create -f deployment-exam1.yaml --record

--record 옵션을 사용하는 이유는 업데이트 과정을 history로 기록해주기 때문이다.  

describe 옵션으로 실행중인 Pod 이미지 확인

nginx:1.14 버전 이미지를 사용하고 있는것을 확인 하였다.

2. 롤링 업데이트(httpd 2.1 버전에서 2.2버전 업데이트)

kubectl set image deployment plpl web=nginx:1.15 --record

명령을 실행하고 나니 두개의 컨테이너가 실행 되면서 두개의 컨테이너가 종료되는것을 볼 수 있었다.

다시 describe 옵션으로 컨테이너의 이미지 확인.

1.15버전으로 업데이트 된것을 확인 하였다

 

업데이트 과정을 확인 하고 싶다면 

kubectl rollout status deployment [deployment_name]

이렇게 롤링 업데이트 과정이 출력된다.

 

 

롤백 해보기

kubectl rollout history deployment [deployment_name]

--record 옵션을 넣어서 실행했던 명령들이 다 기록됐다.

 

kubectl rollout undo deployment [deployment_name] = 바로 전단계로 롤 백 된다.

바로 전단계가 아닌 history에서 출력된 내용 REVSION 번호로 접근해 돌아갈 수도 있다. 

kubectl rollout undo deployment [deployment_name] --to-revision=2  // REVISION 2번으로 롤백 된다.

 

확인 : Image 가 1.15버전으로 돌아갔다.(여기엔 안적었지만 1.16버전으로 업데이트를 한번 더 진행했음)

롤아웃 CHANGE-CAUSE 부분은 yaml 파일의 annotations:를  추가하면 더 깔끔하게 나타낼 수 있다.

 

deployment-exam.yaml 파일에 annotations 추가 후 새로운 디플로이먼트 생성 후 조회.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: plpl
  annotations:
    kubernetes.io/change-cause: version 1.14
spec:
  replicas: 2
  selector:
    matchLabels:
      app: main
  template:
    metadata:
      name: apache-rs
      labels:
        name: apache
        app: main
        rel: stable
    spec:
      containers:
      - name: web
        image: nginx:1.14

좀더 깔끔하게 보이는 것을  확인 할 수 있다.

반응형

'Kubernetes' 카테고리의 다른 글

쿠버네티스 - Statefulset  (0) 2021.07.18
쿠버네티스 - Daemonset  (0) 2021.07.18
쿠버네티스 - Replicaset  (0) 2021.07.17
Controller - ReplicationController란  (0) 2021.07.16
Pod에 환경변수 설정하기/Pod 실행 패턴  (0) 2021.07.15