본문 바로가기
CSP (Cloud Service Provider)/GCP

GKE로 배포 관리하기 (1)순차적 업데이트

by BTC_문돌이 2022. 5. 13.

안녕하세요 BTC Hallo 팀입니다. 오늘부터 3주동안 GKE로 배포 관리하는 법에 대해서 차근차근 업로드 해보도록 하겠습니다. 

 

DevOps 방식에서는 정기적으로 여러 배포를 사용하여 '지속적 배포', 'Blue/Green 배포', 'Canary 배포'와 같은 애플리케이션 배포 시나리오를 관리합니다. 여러 이기종 배포가 사용되는 일반적인 시나리오를 처리할 수 있도록 컨테이너를 확장해보고, 관리해보았습니다.

 

영역 설정

#영역을 us-central1-a로 설정
gcloud config set compute/zone us-central1-a

 

이 실습에서 사용할 샘플 코드 가져오기

gsutil -m cp -r gs://spls/gsp053/orchestrate-with-kubernetes .
cd orchestrate-with-kubernetes/kubernetes

 

#n1-standard-1 노드 3개로 클러스터 생성
gcloud container clusters create bootcamp --num-nodes 3 --scopes "https://www.googleapis.com/auth/projecthosting,storage-rw"

 

배포 만들기

#deployments/auth.yaml 구성 파일 업데이트
vi deployments/auth.yaml

 

#배포의 containers 섹션에 있는 image를 다음과 같이 변경
...
containers:
- name: auth
  image: kelseyhightower/auth:1.0.0
...
#배포 구성 파일 확인
cat deployments/auth.yaml

 

#배포 구성 파일 출력 내용
apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
        track: stable
    spec:
      containers:
        - name: auth
          image: "kelseyhightower/auth:1.0.0"
          ports:
            - name: http
              containerPort: 80
            - name: health
              containerPort: 81
...

 

배포를 통해 어떻게 하나의 복제본이 생성되고 버전 1.0.0의 인증 컨테이너를 사용하는지 확인합니다.

kubectl create 명령어를 실행하여 인증 배포를 만들면 배포 매니페스트의 데이터에 따라 하나의 포드가 생성되며, replicas 필드에 지정된 숫자를 변경하여 포드의 수를 조정할 수 있습니다.

#배포 객체 생성
kubectl create -f deployments/auth.yaml

#배포 생성여부 확인
kubectl get deployments

 

배포가 생성되면, Kubernetes에서는 배포에 관한 ReplicaSet를 만든다. 배포에 관한 ReplicaSet가 생성되었는지 확인할 수 있습니다.

kubectl get replicasets

배포의 일부로 생성된 포드를 볼 수 있다. ReplicaSet가 생성될 때 Kubernetes에서 단일 포드를 생성합니다.

kubectl get pods
#인증 서비스 생성
kubectl create -f services/auth.yaml
#hello 배포 생성 및 노출
kubectl create -f deployments/hello.yaml
kubectl create -f services/hello.yaml

 

#frontend 배포 생성 및 노출
kubectl create secret generic tls-certs --from-file tls/
kubectl create configmap nginx-frontend-conf --from-file=nginx/frontend.conf
kubectl create -f deployments/frontend.yaml
kubectl create -f services/frontend.yaml

#프런트엔드용 ConfigMap 생성
#외부 IP를 가져와서 프런트엔드와 연결함으로써 프런트엔드와 상호작용
# 서비스 확인
kubectl get services frontend

curl -ks https://<EXTERNAL-IP>

#kubectl의 출력 템플릿 기능을 사용하여 curl을 한 줄 명령어로 사용가능
curl -ks https://`kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"`

 

 

배포 확장

이제 배포가 생성되었으므로 확장할 수 있다. spec.replicas 필드를 업데이트하면 됩니다. 

replicas 필드를 가장 쉽게 업데이트하는 방법은 kubectl scale 명령어를 사용하는 것입니다.

#hello 포드를 5개로 확장
kubectl scale deployment hello --replicas=5

#hello 포드 5개가 실행되고 있는지 확인
kubectl get pods | grep hello- | wc -l

#hello 포드를 3개로 축소
kubectl scale deployment hello --replicas=3

#포드 개수 다시 확인
kubectl get pods | grep hello- | wc -l

 

 

순차적 업데이트

배포는 순차적 업데이트 메커니즘을 통해 이미지를 새 버전으로 업데이트하도록 지원합니다. 배포가 새 버전으로 업데이트되면 새 ReplicaSet가 만들어지고, 이전 ReplicaSet의 복제본이 감소하면서 새 ReplicaSet의 복제본 수가 천천히 증가합니다.

순차적 업데이트 트리거하기

배포를 업데이트하려면 다음 명령어를 실행합니다.

kubectl edit deployment hello

배포의 containers 섹션에 있는 image를 다음과 같이 변경합니다.

...
containers:
  image: kelseyhightower/hello:2.0.0
...

 

편집기에서 저장하면, 업데이트된 배포가 클러스터에 저장되고 Kubernetes에서 순차적 업데이트가 시작됩니다.

Kubernetes에서 생성한 새로운 ReplicaSet를 확인합니다.

kubectl get replicaset

출시 기록에 새로운 항목이 표시된다.

kubectl rollout history deployment/hello

 

순차적 업데이트 일시중지하기

실행 중인 출시에 문제가 발생하면 일시중지하여 업데이트를 중지합니다.

kubectl rollout pause deployment/hello

현재 출시 상태를 확인합니다.

kubectl rollout status deployment/hello

포드에서 직접 확인할 수도 있습니다.

kubectl get pods -o jsonpath --template='{range .items[*]}{.metadata.name}{"\t"}{"\t"}{.spec.containers[0].image}{"\n"}{end}'

 

순차적 업데이트 재개하기

출시가 일시중지되었으므로 일부 포드는 새 버전이고 일부 포드는 이전 버전입니다. resume 명령어를 사용하여 출시를 계속 진행할 수 있습니다.

kubectl rollout resume deployment/hello

출시가 완료되면 status 명령어를 실행할 때 다음이 표시됩니다.

kubectl rollout status deployment/hello

(출력 내용)

deployment "hello" successfully rolled out

업데이트 롤백하기

새 버전에서 버그가 발견되었다면, 새 버전에 문제가 있는 것으로 간주되므로 이전 버전으로 롤백하여 문제를 조사한 다음 제대로 수정된 버전을 출시할 수 있습니다.

rollout 명령어를 사용하여 이전 버전으로 롤백합니다.

kubectl rollout undo deployment/hello

기록에서 롤백을 확인합니다.

kubectl rollout history deployment/hello

 

모든 포드가 이전 버전으로 롤백되었는지 확인합니다.

kubectl get pods -o jsonpath --template='{range .items[*]}{.metadata.name}{"\t"}{"\t"}{.spec.containers[0

댓글