[K8S] Deployment, ReplicaSet 생성 / Scaling / Rolling Update / Rollback

HYEOB KIM·2022년 7월 15일
1

kubernetes

목록 보기
10/14

Deployment

  • 쿠버네티스 클러스터에서 애플리케이션을 배포하는 가장 일반적인 방식
  • ReplicaSet 컨트롤러를 통해 replica 수 보장scaling 가능
  • Rolling update 또는 Roll back 지원

yaml 파일 작성법

쿠버네티스 공식 문서 - Deployment를 참고하세요.

  • replicas: 파드 개수 보장
  • selector: 어떤 파드?
  • template: 파드에 대한 정보
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Replicas를 정의한 Deployment 생성

# 입력을 저장하려면 [Ctrl] + [D]
% cat > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        
% kubectl apply -f nginx-deployment.yaml

# nginx-deployment-xxxxxx가 접두사로 붙은 3개의 파드 확인
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              163m
eshop-frontend                      4/4     Running       0              3h22m
front-end-8dc556958-2hfqt           1/1     Running       0              3h59m
front-end-8dc556958-4qcd9           1/1     Running       0              3h59m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              3h59m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8kmjk   1/1     Running       0              46s
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              46s
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              46s

# nginx-deployment-66b6c48dd5은 ReplicaSet의 이름
% kubectl get replicaset nginx-deployment-66b6c48dd5
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-66b6c48dd5   3         3         3       34s

# 임의로 파드 하나를 삭제해보기
% kubectl delete pod nginx-deployment-66b6c48dd5-8kmjk
pod "nginx-deployment-66b6c48dd5-8kmjk" deleted

# 파드가 자동으로 재생성된 것을 확인
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              165m
eshop-frontend                      4/4     Running       0              3h24m
front-end-8dc556958-2hfqt           1/1     Running       0              4h1m
front-end-8dc556958-4qcd9           1/1     Running       0              4h1m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              4h1m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              2m51s
nginx-deployment-66b6c48dd5-g98sg   1/1     Running       0              5s
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              2m51s

애플리케이션 Scaling

  • Deployment는 ReplicaSet 컨트롤러를 이용해 Pod Scaling을 지원합니다.
    아래와 같은 방식으로 명령합니다.
% kubectl scale deployment <deploy_name> --replicas=N
  • Scale-out : 애플리케이션 Pod 수를 확장하여 처리 능력을 향상
  • Scale-in : 애플리케이션 Pod 수를 축소하여 리소스 낭비 최소화
# scale-out
% kubectl scale deployment nginx-deployment --replicas=4
deployment.apps/nginx-deployment scaled

% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              167m
eshop-frontend                      4/4     Running       0              3h26m
front-end-8dc556958-2hfqt           1/1     Running       0              4h3m
front-end-8dc556958-4qcd9           1/1     Running       0              4h3m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              4h3m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              4m50s
nginx-deployment-66b6c48dd5-g98sg   1/1     Running       0              2m4s
nginx-deployment-66b6c48dd5-gk49c   1/1     Running       0              4s
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              4m50s

# scale-in
% kubectl scale deployment nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled

% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              167m
eshop-frontend                      4/4     Running       0              3h27m
front-end-8dc556958-2hfqt           1/1     Running       0              4h4m
front-end-8dc556958-4qcd9           1/1     Running       0              4h4m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              4h4m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              5m11s
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              5m11s

CKA 문제 유형 1

  • 작업 클러스터 : k8s

a. webserver 라는 이름으로 deployment를 생성하세요.

  • Name: webserver
  • 2 replicas
  • label: app_env_stage=dev
  • container name: webserver
  • container image: nginx:1.14

b. 다음, webserver Deployment의 pod 수를 3개로 확장하세요.

1. Deployment yaml 파일 생성

% cat > webserver.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

2. yaml 파일 수정

% vim webserver.yaml

# 아래 내용대로 수정
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webserver
spec:
  replicas: 2
  selector:
    matchLabels:
      app_env_stage: dev
  template:
    metadata:
      labels:
        app_env_stage: dev
    spec:
      containers:
      - name: webserver
        image: nginx:1.14

3. Deployment 생성

% kubectl apply -f webserver.yaml
deployment.apps/webserver created

% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              3h22m
eshop-frontend                      4/4     Running       0              4h2m
front-end-8dc556958-2hfqt           1/1     Running       0              4h39m
front-end-8dc556958-4qcd9           1/1     Running       0              4h39m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              4h39m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              40m
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              40m
webserver-5586594bbf-6xvnm          1/1     Running       0              4s
webserver-5586594bbf-c9p5f          1/1     Running       0              4s

4. scale-out 설정

# ReplicaSet 조회
% kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
front-end-8dc556958           2         2         2       134d
nginx-79488c9578              2         2         2       111d
nginx-deployment-66b6c48dd5   2         2         2       41m
webserver-5586594bbf          2         2         2       75s

# scale-out
% kubectl scale deployment webserver-5586594bbf --replicas=3
replicaset.apps/webserver-5586594bbf scaled

# 파드가 3개로 늘었는지 조회
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              3h24m
eshop-frontend                      4/4     Running       0              4h3m
front-end-8dc556958-2hfqt           1/1     Running       0              4h40m
front-end-8dc556958-4qcd9           1/1     Running       0              4h40m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              4h40m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-8mwqt   1/1     Running       0              41m
nginx-deployment-66b6c48dd5-rpmwd   1/1     Running       0              41m
webserver-5586594bbf-6xvnm          1/1     Running       0              89s
webserver-5586594bbf-c9p5f          1/1     Running       0              89s
webserver-5586594bbf-ws8t6          1/1     Running       0              2s

CKA 문제 유형 2

  • 작업 클러스터: k8s

devops 네임스페이스를 통해 eshop-order 애플리케이션이 배포되었습니다. 서비스 트래픽 증가로 eshop-order에서 동작되는 Pod 수를 scale-out 해야 합니다. Pod 수를 5개로 확장하고, 동작중인 5개의 Pod 이름을 /opt/REPORT/2022/eshop-order-pod-list에 기록하세요.

Rolling Update

  • Rolling Update: 동작 중인 애플리케이션의 서비스 중단 없이 점진적으로 Pod 업데이트하는 방법
% kubectl set image deployment <deploy_name> <container_name>=<new_version_image> --record
  • Rolling Update를 실행하면, 새로운 ReplicaSet을 생성한 후 새로운 ReplicaSet에 업데이트된 버전의 파드를 하나씩 생성하고, 파드가 running되면 기존 ReplicaSet에 있는 파드의 수를 하나씩 줄입니다. 이러한 방식으로 점진적으로 서비스 중단 없이 Pod를 업데이트하게 됩니다.
  • 새로운 ReplicaSet을 생성하는 이유는 나중에 Rollback을 하기 위해 history를 남겨주는 역할을 합니다.

실습

앞서 생성한 nginx-deployment deployment를 이용합니다.

# nginx-deployment의 정보 확인
% kubectl describe deployments.apps nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Fri, 15 Jul 2022 13:34:06 +0900
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.14.2
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-66b6c48dd5 (3/3 replicas created)
Events:
  Type    Reason             Age                            From                   Message
  ----    ------             ----                           ----                   -------
  Normal  ScalingReplicaSet  <invalid> (x2 over <invalid>)  deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  <invalid> (x2 over <invalid>)  deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 3

# nginx를 1.15 버전으로 업데이트
% kubectl set image deployment nginx-deployment nginx=nginx:1.15 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated

# 기존에 있던 deployment는 replicas가 0이 되고, 새로 생성된 deployment의 replicas가 3이 된 것을 확인
% kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
front-end-8dc556958           2         2         2       134d
nginx-79488c9578              2         2         2       111d
nginx-deployment-66b6c48dd5   0         0         0       70m
nginx-deployment-698676d7f8   3         3         3       69s
webserver-5586594bbf          3         3         3       30m

# deployment가 nginx:1.15로 잘 업데이트 되었는지 확인
% kubectl describe deployments.apps nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Fri, 15 Jul 2022 13:34:06 +0900
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 2
                        kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.15 --record=true
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.15
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-698676d7f8 (3/3 replicas created)
Events:
  Type    Reason             Age                            From                   Message
  ----    ------             ----                           ----                   -------
  Normal  ScalingReplicaSet  <invalid> (x2 over <invalid>)  deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 3
  Normal  ScalingReplicaSet  <invalid>                      deployment-controller  Scaled up replica set nginx-deployment-698676d7f8 to 1
  Normal  ScalingReplicaSet  <invalid> (x3 over <invalid>)  deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  <invalid>                      deployment-controller  Scaled up replica set nginx-deployment-698676d7f8 to 2
  Normal  ScalingReplicaSet  <invalid>                      deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 1
  Normal  ScalingReplicaSet  <invalid>                      deployment-controller  Scaled up replica set nginx-deployment-698676d7f8 to 3
  Normal  ScalingReplicaSet  <invalid>                      deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 0
  
# 파드 정보를 통해 nginx:1.15로 업데이트 되었다는 이벤트 로그 확인
% kubectl describe pod nginx-deployment-698676d7f8-2g4mc
Name:         nginx-deployment-698676d7f8-2g4mc
Namespace:    default
Priority:     0
Node:         k8s-worker1/10.0.2.11
Start Time:   Fri, 15 Jul 2022 14:43:39 +0900
Labels:       app=nginx
              pod-template-hash=698676d7f8
Annotations:  <none>
Status:       Running
IP:           10.244.1.49
IPs:
  IP:           10.244.1.49
Controlled By:  ReplicaSet/nginx-deployment-698676d7f8
Containers:
  nginx:
    Container ID:   docker://da7389994e4994a5c750ab407987b3858d251c8ed26f24ef9de04397973dbd04
    Image:          nginx:1.15
    Image ID:       docker-pullable://nginx@sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 15 Jul 2022 14:43:40 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-nqflw (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-nqflw:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <invalid>  default-scheduler  Successfully assigned default/nginx-deployment-698676d7f8-2g4mc to k8s-worker1
  Normal  Pulled     <invalid>  kubelet            Container image "nginx:1.15" already present on machine
  Normal  Created    <invalid>  kubelet            Created container nginx
  Normal  Started    <invalid>  kubelet            Started container nginx

Rollback

  • 동작 중인 애플리케이션 서비스 중단 없이 이전 버전으로 되돌리기
% kubectl rollout history deployment <deployment 이름>
# 바로 이전 단계로 되돌리기
% kubectl rollout undo deployment <deployment 이름>

# 특정 히스토리로 되돌리기
% kubectl rollout undo deployment <deployment 이름> --to-revision=NUMBER

실습

# nginx-deployment 의 히스토리 조회
% kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment nginx-deployment nginx=nginx:1.15 --record=true

# nginx:1.14 버전으로 되돌리기
% kubectl rollout undo deployment nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back

# Rollback 되었는지 확인
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              4h13m
eshop-frontend                      4/4     Running       0              4h52m
front-end-8dc556958-2hfqt           1/1     Running       0              5h29m
front-end-8dc556958-4qcd9           1/1     Running       0              5h29m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              5h29m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-bgl5t   1/1     Running       0              53s
nginx-deployment-66b6c48dd5-cck85   1/1     Running       0              52s
nginx-deployment-66b6c48dd5-nsmz9   1/1     Running       0              55s
webserver-5586594bbf-6xvnm          1/1     Running       0              50m
webserver-5586594bbf-c9p5f          1/1     Running       0              50m
webserver-5586594bbf-ws8t6          1/1     Running       0              26m

% kubectl describe pod nginx-deployment-66b6c48dd5-bgl5t
Name:         nginx-deployment-66b6c48dd5-bgl5t
Namespace:    default
Priority:     0
Node:         k8s-worker1/10.0.2.11
Start Time:   Fri, 15 Jul 2022 15:04:14 +0900
Labels:       app=nginx
              pod-template-hash=66b6c48dd5
Annotations:  <none>
Status:       Running
IP:           10.244.1.55
IPs:
  IP:           10.244.1.55
Controlled By:  ReplicaSet/nginx-deployment-66b6c48dd5
Containers:
  nginx:
    Container ID:   docker://74ac2dc9ee30a68cd4060197d4f62a469e02b0b383db88af997c5cb514faf28f
    Image:          nginx:1.14.2
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 15 Jul 2022 15:04:15 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-c4ddz (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-c4ddz:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <invalid>  default-scheduler  Successfully assigned default/nginx-deployment-66b6c48dd5-bgl5t to k8s-worker1
  Normal  Pulled     <invalid>  kubelet            Container image "nginx:1.14.2" already present on machine
  Normal  Created    <invalid>  kubelet            Created container nginx
  Normal  Started    <invalid>  kubelet            Started container nginx

# history 확인
% kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
2         kubectl set image deployment nginx-deployment nginx=nginx:1.15 --record=true
3         <none>

CKA 문제 유형 3

  • 작업 클러스터: k8s

a. Deployment를 이용해 nginx 파드를 3개 배포한 다음 컨테이너 이미지 버전을 rolling update하고 update record를 기록합니다.

  • name: eshop-payment
  • Image : nginx
  • Image version: 1.16
  • update image version: 1.17
  • label: app=payment, environment=production

b. 컨테이너 이미지를 previous version으로 roll back 합니다.

% cat > eshop-payment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
% vim eshop-payment.yaml

# 아래와 같이 수정
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eshop-payment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: payment
      environment: production
  template:
    metadata:
      labels:
        app: payment
        environment: production
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
# deployment 생성
% kubectl apply -f eshop-payment.yaml
deployment.apps/eshop-payment created

# 파드가 제대로 생성되었는지 조회
% kubectl get pod
NAME                                READY   STATUS              RESTARTS       AGE
eshop-cart-app                      2/2     Running             0              4h27m
eshop-frontend                      4/4     Running             0              5h6m
eshop-payment-54cb87c674-k9zjn      0/1     ContainerCreating   0              4s
eshop-payment-54cb87c674-lj6mw      0/1     ContainerCreating   0              4s
eshop-payment-54cb87c674-lxlmb      0/1     ContainerCreating   0              4s
front-end-8dc556958-2hfqt           1/1     Running             0              5h43m
front-end-8dc556958-4qcd9           1/1     Running             0              5h43m
front-end-8dc556958-fvlpx           1/1     Terminating         2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating         3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running             0              5h43m
nginx-79488c9578-qwnfk              1/1     Running             2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating         1 (111d ago)   111d
nginx-deployment-66b6c48dd5-bgl5t   1/1     Running             0              14m
nginx-deployment-66b6c48dd5-cck85   1/1     Running             0              14m
nginx-deployment-66b6c48dd5-nsmz9   1/1     Running             0              14m
webserver-5586594bbf-6xvnm          1/1     Running             0              64m
webserver-5586594bbf-c9p5f          1/1     Running             0              64m
webserver-5586594bbf-ws8t6          1/1     Running             0              39m
# nginx:1.16 -> nginx:1.17로 Rolling Update
% kubectl set image deployment eshop-payment nginx=nginx:1.17 --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/eshop-payment image updated

# 업데이트 확인
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              4h31m
eshop-frontend                      4/4     Running       0              5h11m
eshop-payment-856fdc9db9-5vkdp      1/1     Running       0              37s
eshop-payment-856fdc9db9-b5f6k      1/1     Running       0              50s
eshop-payment-856fdc9db9-jbckr      1/1     Running       0              38s
front-end-8dc556958-2hfqt           1/1     Running       0              5h47m
front-end-8dc556958-4qcd9           1/1     Running       0              5h47m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              5h47m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-bgl5t   1/1     Running       0              18m
nginx-deployment-66b6c48dd5-cck85   1/1     Running       0              18m
nginx-deployment-66b6c48dd5-nsmz9   1/1     Running       0              18m
webserver-5586594bbf-6xvnm          1/1     Running       0              68m
webserver-5586594bbf-c9p5f          1/1     Running       0              68m
webserver-5586594bbf-ws8t6          1/1     Running       0              44m

% kubectl describe pod eshop-payment-856fdc9db9-5vkdp
Name:         eshop-payment-856fdc9db9-5vkdp
Namespace:    default
Priority:     0
Node:         k8s-worker1/10.0.2.11
Start Time:   Fri, 15 Jul 2022 15:22:32 +0900
Labels:       app=payment
              environment=production
              pod-template-hash=856fdc9db9
Annotations:  <none>
Status:       Running
IP:           10.244.1.62
IPs:
  IP:           10.244.1.62
Controlled By:  ReplicaSet/eshop-payment-856fdc9db9
Containers:
  nginx:
    Container ID:   docker://4bf70d504ccd3e93894884fa0e939c59b074570faafa6990b1ffaf5c5e0daef8
    Image:          nginx:1.17
    Image ID:       docker-pullable://nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 15 Jul 2022 15:22:33 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-4hnlh (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-4hnlh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <invalid>  default-scheduler  Successfully assigned default/eshop-payment-856fdc9db9-5vkdp to k8s-worker1
  Normal  Pulled     <invalid>  kubelet            Container image "nginx:1.17" already present on machine
  Normal  Created    <invalid>  kubelet            Created container nginx
  Normal  Started    <invalid>  kubelet            Started container nginx
# nginx:1.17 -> nginx:1.16로 Rollback
% kubectl rollout history deployment eshop-payment
deployment.apps/eshop-payment
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment eshop-payment nginx=nginx:1.17 --record=true

% kubectl rollout undo deployment eshop-payment
deployment.apps/eshop-payment rolled back

# 롤백 확인
% kubectl get pod
NAME                                READY   STATUS        RESTARTS       AGE
eshop-cart-app                      2/2     Running       0              4h34m
eshop-frontend                      4/4     Running       0              5h14m
eshop-payment-54cb87c674-7sn2k      1/1     Running       0              15s
eshop-payment-54cb87c674-btfbm      1/1     Running       0              16s
eshop-payment-54cb87c674-n4ldd      1/1     Running       0              18s
front-end-8dc556958-2hfqt           1/1     Running       0              5h51m
front-end-8dc556958-4qcd9           1/1     Running       0              5h51m
front-end-8dc556958-fvlpx           1/1     Terminating   2 (111d ago)   134d
front-end-8dc556958-vcr4s           1/1     Terminating   3 (111d ago)   134d
nginx-79488c9578-5f5q7              1/1     Running       0              5h51m
nginx-79488c9578-qwnfk              1/1     Running       2 (110d ago)   111d
nginx-79488c9578-xpsvp              1/1     Terminating   1 (111d ago)   111d
nginx-deployment-66b6c48dd5-bgl5t   1/1     Running       0              22m
nginx-deployment-66b6c48dd5-cck85   1/1     Running       0              22m
nginx-deployment-66b6c48dd5-nsmz9   1/1     Running       0              22m
webserver-5586594bbf-6xvnm          1/1     Running       0              72m
webserver-5586594bbf-c9p5f          1/1     Running       0              72m
webserver-5586594bbf-ws8t6          1/1     Running       0              47m

% kubectl describe pod eshop-payment-54cb87c674-7sn2k
Name:         eshop-payment-54cb87c674-7sn2k
Namespace:    default
Priority:     0
Node:         k8s-worker1/10.0.2.11
Start Time:   Fri, 15 Jul 2022 15:26:03 +0900
Labels:       app=payment
              environment=production
              pod-template-hash=54cb87c674
Annotations:  <none>
Status:       Running
IP:           10.244.1.65
IPs:
  IP:           10.244.1.65
Controlled By:  ReplicaSet/eshop-payment-54cb87c674
Containers:
  nginx:
    Container ID:   docker://e71e0728451e67363d05b88a875145853d619a45e493d1dbc59e9ccdd4de76db
    Image:          nginx:1.16
    Image ID:       docker-pullable://nginx@sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 15 Jul 2022 15:26:03 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-s2d94 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-s2d94:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <invalid>  default-scheduler  Successfully assigned default/eshop-payment-54cb87c674-7sn2k to k8s-worker1
  Normal  Pulled     <invalid>  kubelet            Container image "nginx:1.16" already present on machine
  Normal  Created    <invalid>  kubelet            Created container nginx
  Normal  Started    <invalid>  kubelet            Started container nginx
  
% kubectl rollout history deployment eshop-payment
deployment.apps/eshop-payment
REVISION  CHANGE-CAUSE
2         kubectl set image deployment eshop-payment nginx=nginx:1.17 --record=true
3         <none>
profile
Devops Engineer

0개의 댓글