마짱짱의 지식창고

[GCP] GKE에 Cloud Deploy를 사용하여 CD환경 구축하기 본문

Cloud/GCP

[GCP] GKE에 Cloud Deploy를 사용하여 CD환경 구축하기

마짱짱 2023. 2. 20. 11:32
반응형

0. 개요

Cloud Deploy는 GKE, Cloud Run, Anthos 에 CD환경을 구축해주는 PaaS 제품 입니다.

( https://cloud.google.com/deploy?hl=ko )

 

이번 포스팅에서는 Cloud Deploy를 사용하여 CD 파이프라인을 구축하고 Application의 Release를 형성하여 GKE에 배포합니다.

 

1. 필요한 변수 사전 정의

export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-west1
gcloud config set compute/region $REGION

Cloud Shell 에서 ID 및 Resion 설정

 

 

2. GKE 생성

gcloud services enable \
container.googleapis.com \
clouddeploy.googleapis.com

GCP container,deploy API 활성화

 

 

gcloud container clusters create test --node-locations=us-west1-a --num-nodes=1  --async
gcloud container clusters create staging --node-locations=us-west1-a --num-nodes=1  --async
gcloud container clusters create prod --node-locations=us-west1-a --num-nodes=1  --async

3가지 환경(test,staging,prod)의 GKE 생성

 

 

3. Artifact Registry 생성

gcloud services enable artifactregistry.googleapis.com

GCP Artifact Registry API 활성화

 

gcloud artifacts repositories create web-app \
--description="Image registry for tutorial web app" \
--repository-format=docker \
--location=$REGION

Docker기반 Artifact Registry 생성

 

 

4. Application을 Image로 빌드하여 Artifact Registry에 저장

cd ~/
git clone https://github.com/GoogleCloudPlatform/cloud-deploy-tutorials.git
cd cloud-deploy-tutorials
git checkout c3cae80 --quiet
cd tutorials/base

Sample Code 가져오기

 

envsubst < clouddeploy-config/skaffold.yaml.template > web/skaffold.yaml
cat web/skaffold.yaml
### skaffold.yaml

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: skaffold/v2beta7
kind: Config
build:
  artifacts:
    - image: leeroy-web
      context: leeroy-web
    - image: leeroy-app
      context: leeroy-app
  googleCloudBuild:
    projectId: qwiklabs-gcp-02-52f43ce02350
deploy:
  kubectl:
    manifests:
      - leeroy-web/kubernetes/*
      - leeroy-app/kubernetes/*
portForward:
  - resourceType: deployment
    resourceName: leeroy-web
    port: 8080
    localPort: 9000

skaffold을 이용하여 Image 만들기위한  설정

해당 설정은 다음과 같은 빌드를 합니다.

  • 빌드될 두 개의 컨테이너 이미지(아티팩트)
  • 이미지를 빌드하는 데 사용되는 Google Cloud Build 프로젝트

 

gcloud services enable cloudbuild.googleapis.com

GCP Cloud Build API 활성화

 

cd web
skaffold build --interactive=false \
--default-repo $REGION-docker.pkg.dev/$PROJECT_ID/web-app \
--file-output artifacts.json
cd ..

skaffold 명령어를 통해 Artifact Reistory에 Image 저장

 

 

5. Cloud Deploy를 이용하여 Pipeline 생성

gcloud config set deploy/region $REGION
cp clouddeploy-config/delivery-pipeline.yaml.template clouddeploy-config/delivery-pipeline.yaml
gcloud beta deploy apply --file=clouddeploy-config/delivery-pipeline.yaml
### delivery-pipeline.yaml

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: deploy.cloud.google.com/v1beta1
kind: DeliveryPipeline
metadata:
  name: web-app
description: web-app delivery pipeline
serialPipeline:
 stages:
 - targetId: test
 - targetId: staging
 - targetId: prod

delivery-pipeline.yaml을 이용하여 Cloud Deploy 생성

 

6. 배포대상 타겟 설정

 

CONTEXTS=("test" "staging" "prod")
for CONTEXT in ${CONTEXTS[@]}
do
    gcloud container clusters get-credentials ${CONTEXT} --region ${REGION}
    kubectl config rename-context gke_${PROJECT_ID}_${REGION}_${CONTEXT} ${CONTEXT}
done

3개의 GKE환경을 Context 정의하기

 

 

for CONTEXT in ${CONTEXTS[@]}
do
    kubectl --context ${CONTEXT} apply -f kubernetes-config/web-app-namespace.yaml
done
### kubernetes-config/web-app-namespace.yaml

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Namespace
metadata:
  name: web-app

환경별 Namespace 생성

 

for CONTEXT in ${CONTEXTS[@]}
do
    envsubst < clouddeploy-config/target-$CONTEXT.yaml.template > clouddeploy-config/target-$CONTEXT.yaml
    gcloud beta deploy apply --file clouddeploy-config/target-$CONTEXT.yaml
done
### clouddeploy-config/target-$CONTEXT.yaml

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: deploy.cloud.google.com/v1beta1
kind: Target
metadata:
  name: prod
description: prod cluster
requireApproval: true
gke:
  cluster: projects/qwiklabs-gcp-01-2900a7f24ec7/locations/us-west1/clusters/prod

Delivery Target 설정하기

 

 

 

 

7. Release 만들기

gcloud beta deploy releases create web-app-001 \
--delivery-pipeline web-app \
--build-artifacts web/artifacts.json \
--source web/
### artifact.json

{"builds":[{"imageName":"leeroy-web","tag":"us-west1-docker.pkg.dev/qwiklabs-gcp-02-52f43ce02350/web-app/leeroy-web:c3cae80@sha256:8da797712d05cbfdfc46803331a3f0f8cc69ad101b23a38c276cb08b4aa66925"},{"imageName":"leeroy-app","tag":"us-west1-docker.pkg.dev/qwiklabs-gcp-02-52f43ce02350/web-app/leeroy-app:c3cae80@sha256:7893f91e6fd5f124956fea16ba64061013ad4e9f06a05998693572bf04ea1941"}

Test Cluster에 배포

 

kubectx test
kubectl get all -n web-app

배포된 것 확인

 

8. Staging Cluster로 배포하기

test에 정상적으로 배포 된 것을 확인 했으니 staging환경에 배포

gcloud beta deploy releases promote \
--delivery-pipeline web-app \
--release web-app-001

위 코드 입력하면 다음 staging으로 배포 하겠습니까? y

 

정상적으로 Staging Cluster에도 배포 완료

 

9. Prod Cluster에 배포하기

gcloud beta deploy releases promote \
--delivery-pipeline web-app \
--release web-app-001

staging에 배포하는것과 동일하게 진행하게되면

 

Pending 걸린 모습을 볼 수 있음

 

gcloud beta deploy rollouts approve web-app-001-to-prod-0001 \
--delivery-pipeline web-app \
--release web-app-001

해당 명령어를 통해 승인 후 배포 완료

 

---

출처

https://www.cloudskillsboost.google/focuses/52828?catalog_rank=%7B%22rank%22%3A3%2C%22num_filters%22%3A0%2C%22has_search%22%3Atrue%7D&parent=catalog&search_id=22463857 

 

 

반응형