Kubernetes 이야기

Argo rollout을 통한 Blue/Green CD (Continuous Deployment) 구축하기 (2) - Blue/Green 배포 본문

Kubernetes/devops

Argo rollout을 통한 Blue/Green CD (Continuous Deployment) 구축하기 (2) - Blue/Green 배포

kmaster 2022. 2. 27. 13:41
반응형

Argo rollout - Blue / Green 배포

 

우선 아래와 같이 앱을 배포해보자.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rollouts-bluegreen
spec:
  replicas: 3
  selector:
    matchLabels:
      app: rollouts-bluegreen
  template:
    metadata:
      labels:
        app: rollouts-bluegreen
    spec:
      containers:
      - name: hello
        image: ghcr.io/kmaster8/hello:v1
        ports:
        - containerPort: 5050
apiVersion: v1
kind: Service
metadata:
  name: hello-v1
spec:
  type: ClusterIP
  selector:
    app: rollouts-bluegreen
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: hello-v2
spec:
  type: ClusterIP
  selector:
    app: rollouts-bluegreen
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: prod-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: test.prod.10.60.200.121.sslip.io
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-v1
            port:
              number: 5000
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: stg-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: test.stg.10.60.200.121.sslip.io
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-v2
            port:
              number: 5000
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: rollouts-bluegreen
spec:
  replicas: 3
  revisionHistoryLimit: 1
  workloadRef:
    apiVersion: apps/v1
    kind: Deployment
    name: rollouts-bluegreen
  strategy:
    blueGreen:
      activeService: hello-v1
      previewService: hello-v2
      autoPromotionEnabled: false

 

배포된 상태를 보자.

# kubectl get ro -n test
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
rollouts-bluegreen   3         3         3            3           3m44s

# kubectl get all -n test
NAME                                      READY   STATUS    RESTARTS   AGE
pod/rollouts-bluegreen-5695c8c6bc-2fqwg   1/1     Running   0          64s
pod/rollouts-bluegreen-5695c8c6bc-cx4rx   1/1     Running   0          64s
pod/rollouts-bluegreen-5695c8c6bc-xlxg5   1/1     Running   0          64s
pod/rollouts-bluegreen-6577685d65-gnbpp   1/1     Running   0          66s
pod/rollouts-bluegreen-6577685d65-jpdf4   1/1     Running   0          66s
pod/rollouts-bluegreen-6577685d65-llvvd   1/1     Running   0          66s

NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/hello-v1   ClusterIP   10.101.22.39    <none>        5000/TCP   65s
service/hello-v2   ClusterIP   10.104.70.136   <none>        5000/TCP   65s

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/rollouts-bluegreen   3/3     3            3           66s

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/rollouts-bluegreen-5695c8c6bc   3         3         3       64s
replicaset.apps/rollouts-bluegreen-6577685d65   3         3         3       66s

 

현재 deployment는 1개이고 service는 2개, ingress가 2개이다. service 2개가 동일한 replicaset를 연결하고 있다. 이 상태는 현재 다음과 같다.

 

# kubectl describe svc -n test hello-v1
Name:              hello-v1
Namespace:         test
Labels:            <none>
Annotations:       argo-rollouts.argoproj.io/managed-by-rollouts: rollouts-bluegreen
Selector:          app=rollouts-bluegreen,rollouts-pod-template-hash=5695c8c6bc
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.101.22.39
IPs:               10.101.22.39
Port:              <unset>  5000/TCP
TargetPort:        5000/TCP
Endpoints:         172.32.24.12:5000,172.32.24.20:5000,172.32.24.23:5000
Session Affinity:  None
Events:            <none>



# kubectl describe svc -n test hello-v2
Name:              hello-v2
Namespace:         test
Labels:            <none>
Annotations:       argo-rollouts.argoproj.io/managed-by-rollouts: rollouts-bluegreen
Selector:          app=rollouts-bluegreen,rollouts-pod-template-hash=5695c8c6bc
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.104.70.136
IPs:               10.104.70.136
Port:              <unset>  5000/TCP
TargetPort:        5000/TCP
Endpoints:         172.32.24.12:5000,172.32.24.20:5000,172.32.24.23:5000
Session Affinity:  None
Events:            <none>

 

# kubectl argo rollouts get rollout rollouts-bluegreen -n test
Name:            rollouts-bluegreen
Namespace:       test
Status:          ✔ Healthy
Strategy:        BlueGreen
Images:          ghcr.io/kmaster8/hello:v1 (stable, active)
Replicas:
  Desired:       3
  Current:       3
  Updated:       3
  Ready:         3
  Available:     3

NAME                                            KIND        STATUS     AGE    INFO
⟳ rollouts-bluegreen                            Rollout     ✔ Healthy  2m11s
└──# revision:1
   └──⧉ rollouts-bluegreen-5695c8c6bc           ReplicaSet  ✔ Healthy  2m9s   stable,active
      ├──□ rollouts-bluegreen-5695c8c6bc-2fqwg  Pod         ✔ Running  2m9s   ready:1/1
      ├──□ rollouts-bluegreen-5695c8c6bc-cx4rx  Pod         ✔ Running  2m9s   ready:1/1
      └──□ rollouts-bluegreen-5695c8c6bc-xlxg5  Pod         ✔ Running  2m9s   ready:1/1

argo rollout을 생성하면 서비스에 rollouts-pod-template-hash=5695c8c6bc 처럼 pod의 hash tag 가 생성된다. 현재는 앱이 v1만 있기 때문에 동일한 hash 를 바라보고 있다. 그림을 보면 아래와 같다.

 

이제 hello 앱의 이미지를 업데이트해보자.

 

# kubectl argo rollouts set image rollouts-bluegreen hello=ghcr.io/kmaster8/hello:v2 -n test
deployment "rollouts-bluegreen" image updated

 

# kubectl argo rollouts get rollout rollouts-bluegreen -n test
Name:            rollouts-bluegreen
Namespace:       test
Status:          ॥ Paused
Message:         BlueGreenPause
Strategy:        BlueGreen
Images:          ghcr.io/kmaster8/hello:v1 (stable, active)
                 ghcr.io/kmaster8/hello:v2 (preview)
Replicas:
  Desired:       3
  Current:       6
  Updated:       3
  Ready:         3
  Available:     3

NAME                                            KIND        STATUS     AGE    INFO
⟳ rollouts-bluegreen                            Rollout     ॥ Paused   3m51s
├──# revision:2
│  └──⧉ rollouts-bluegreen-7884dc94cb           ReplicaSet  ✔ Healthy  5s     preview
│     ├──□ rollouts-bluegreen-7884dc94cb-8r9qr  Pod         ✔ Running  5s     ready:1/1
│     ├──□ rollouts-bluegreen-7884dc94cb-n8qt9  Pod         ✔ Running  5s     ready:1/1
│     └──□ rollouts-bluegreen-7884dc94cb-qmj9l  Pod         ✔ Running  5s     ready:1/1
└──# revision:1
   └──⧉ rollouts-bluegreen-5695c8c6bc           ReplicaSet  ✔ Healthy  3m49s  stable,active
      ├──□ rollouts-bluegreen-5695c8c6bc-2fqwg  Pod         ✔ Running  3m49s  ready:1/1
      ├──□ rollouts-bluegreen-5695c8c6bc-cx4rx  Pod         ✔ Running  3m49s  ready:1/1
      └──□ rollouts-bluegreen-5695c8c6bc-xlxg5  Pod         ✔ Running  3m49s  ready:1/1

 

이미지 업데이트를 하면 아래와 같은 그림이 구성된다.

 

 

도메인을 브라우저에서 호출하면 이제 v1과 v2가 각각 보이게 될 것이다. 기존 사용자는 v1이 보이고 내부 테스트 사용자는 v2를 볼 수 있도록 구성한 것이다.

 

테스트가 정상적이라 하면 이제 green으로 모두 배포하자.

 

# kubectl argo rollouts promote rollouts-bluegreen -n test
rollout 'rollouts-bluegreen' promoted

# kubectl argo rollouts get rollout rollouts-bluegreen -n test
Name:            rollouts-bluegreen
Namespace:       test
Status:          ✔ Healthy
Strategy:        BlueGreen
Images:          ghcr.io/kmaster8/hello:v2 (stable, active)
Replicas:
  Desired:       3
  Current:       3
  Updated:       3
  Ready:         3
  Available:     3

NAME                                            KIND        STATUS         AGE    INFO
⟳ rollouts-bluegreen                            Rollout     ✔ Healthy      6m37s
├──# revision:2
│  └──⧉ rollouts-bluegreen-7884dc94cb           ReplicaSet  ✔ Healthy      2m51s  stable,active
│     ├──□ rollouts-bluegreen-7884dc94cb-8r9qr  Pod         ✔ Running      2m51s  ready:1/1
│     ├──□ rollouts-bluegreen-7884dc94cb-n8qt9  Pod         ✔ Running      2m51s  ready:1/1
│     └──□ rollouts-bluegreen-7884dc94cb-qmj9l  Pod         ✔ Running      2m51s  ready:1/1
└──# revision:1
   └──⧉ rollouts-bluegreen-5695c8c6bc           ReplicaSet  • ScaledDown   6m35s
      ├──□ rollouts-bluegreen-5695c8c6bc-2fqwg  Pod         ◌ Terminating  6m35s  ready:0/1
      ├──□ rollouts-bluegreen-5695c8c6bc-cx4rx  Pod         ◌ Terminating  6m35s  ready:0/1
      └──□ rollouts-bluegreen-5695c8c6bc-xlxg5  Pod         ◌ Terminating  6m35s  ready:0/1

 

이제 test.prod.10.60.200.121.sslip.io 호출을 해보면 v2 로 이전되었음을 확인할 수 있다.

 

반응형
Comments