Azure Kubernetes Service에 배포하기

Azure Kubernetes Service에 배포하기

지속적 배포(CD) 워크플로의 일부로 프로젝트를 Azure Kubernetes Service(AKS)에 배포하는 방법을 알아봐요. 컨테이너 이미지를 Azure Container Registry(ACR)에 빌드하고, Kubernetes 컨텍스트를 설정한 뒤 애플리케이션을 배포하는 워크플로를 살펴봐요.

출처: 문서

본문

사전 요구 사항

GitHub Actions 워크플로를 만들기 전에 먼저 다음 설정 단계를 완료해야 해요.

  1. 대상 AKS 클러스터와 Azure Container Registry(ACR)를 만들어요. 자세한 내용은 Azure 문서의 Quickstart: Azure 포털을 사용해 AKS 클러스터 배포하기 - Azure Kubernetes ServiceQuickstart - 포털에서 레지스트리 만들기 - Azure Container Registry를 참고하세요.

  2. Azure 자격 증명을 저장하기 위해 AZURE_CREDENTIALS라는 시크릿을 만들어요. 이 정보를 찾고 시크릿 구조를 만드는 방법에 대한 자세한 내용은 the Azure/login action documentation을 참고하세요.

워크플로 만들기

사전 요구 사항을 완료했다면 워크플로를 만들 수 있어요.

다음 예제 워크플로는 코드가 저장소로 푸시될 때 프로젝트를 Azure Kubernetes Service에 빌드하고 배포하는 방법을 보여줘요.

워크플로 env 키 아래에서 다음 값을 변경해요:

  • AZURE_CONTAINER_REGISTRY를 컨테이너 레지스트리 이름으로
  • PROJECT_NAME을 프로젝트 이름으로
  • RESOURCE_GROUP을 AKS 클러스터가 있는 리소스 그룹으로
  • CLUSTER_NAME을 AKS 클러스터 이름으로

이 워크플로는 azure/k8s-bake 액션helm 렌더 엔진을 사용해요. helm 렌더 엔진을 사용한다면 CHART_PATH 값을 helm 파일 경로로 변경해요. CHART_OVERRIDE_PATH를 오버라이드 파일 경로의 배열로 변경해요. 다른 렌더 엔진을 사용한다면 azure/k8s-bake 액션에 보내는 입력 파라미터를 업데이트해요.

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Build and deploy to Azure Kubernetes Service

env:
  AZURE_CONTAINER_REGISTRY: MY_REGISTRY_NAME # set this to the name of your container registry
  PROJECT_NAME: MY_PROJECT_NAME              # set this to your project's name
  RESOURCE_GROUP: MY_RESOURCE_GROUP          # set this to the resource group containing your AKS cluster
  CLUSTER_NAME: MY_CLUSTER_NAME              # set this to the name of your AKS cluster
  REGISTRY_URL: MY_REGISTRY_URL              # set this to the URL of your registry
  # If you bake using helm:
  CHART_PATH: MY_HELM_FILE                   # set this to the path to your helm file
  CHART_OVERRIDE_PATH: MY_OVERRIDE_FILES     # set this to an array of override file paths

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6

    - name: Azure Login
      uses: azure/login@14a755a4e2fd6dff25794233def4f2cf3f866955
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    - name: Build image on ACR
      uses: azure/CLI@61bb69d64d613b52663984bf12d6bac8fd7b3cc8
      with:
        azcliversion: 2.29.1
        inlineScript: |
          az configure --defaults acr=${{ env.AZURE_CONTAINER_REGISTRY }}
          az acr build -t -t ${{ env.REGISTRY_URL }}/${{ env.PROJECT_NAME }}:${{ github.sha }}

    - name: Gets K8s context
      uses: azure/aks-set-context@94ccc775c1997a3fcfbfbce3c459fec87e0ab188
      with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
          resource-group: ${{ env.RESOURCE_GROUP }}
          cluster-name: ${{ env.CLUSTER_NAME }}
      id: login

    - name: Configure deployment
      uses: azure/k8s-bake@61041e8c2f75c1f01186c8f05fb8b24e1fc507d8
      with:
        renderEngine: 'helm'
        helmChart: ${{ env.CHART_PATH }}
        overrideFiles: ${{ env.CHART_OVERRIDE_PATH }}
        overrides: |
          replicas:2
        helm-version: 'latest'
      id: bake

    - name: Deploys application
      uses: Azure/k8s-deploy@dd4bbd13a5abd2fc9ca8bdcb8aee152bb718fa78
      with:
        manifests: ${{ steps.bake.outputs.manifestsBundle }}
        images: |
          ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.PROJECT_NAME }}:${{ github.sha }}
        imagepullsecrets: |
          ${{ env.PROJECT_NAME }}

더 알아보기 (Learn more)