將雲端原生應用程式手動部署至 Azure Kubernetes Service

已完成

您必須先手動將現有的 eShop 應用程式部署至 Azure Kubernetes Service (AKS),才能將網站部署自動化。 您可以使用 Azure CLI 命令和 bash 腳本來建立 Azure 資源,並將應用程式部署至 AKS。 最後,您會建立 Azure Active Directory (Azure AD) 服務主體,以允許 GitHub Actions 部署至 AKS 和 Azure Container Registry。

命令會建立下列資源,以部署 eShop 應用程式的更新版本。

  • 布建 Azure Container Registry (ACR),然後將映像推送至登錄。
  • 布建 AKS 叢集,然後將容器部署至叢集。
  • 測試部署。
  • 建立服務主體,以允許 GitHub Actions 部署至 AKS 和 Azure Container Registry。

這很重要

開始之前,請確定您已完成 必要條件

開啟開發環境

您可以選擇使用裝載本練習的 GitHub Codespace,或在 Visual Studio Code 本機上完成該練習。

GitHub Codespaces 設定

https://github.com/MicrosoftDocs/mslearn-dotnet-cloudnative-devops 存放庫派生到您自己的 GitHub 帳戶。 然後在新的分支上:

  1. 選取 [程式碼]
  2. 選取 Codespaces 索引標籤。
  3. 選取+圖示以建立您的程式碼空間。

GitHub 需要幾分鐘的時間才能建立及設定 Codespace。 程式完成時,您會看到練習的程式代碼檔案。

選擇性:Visual Studio Code 設定

若要使用 Visual Studio Code,請將 https://github.com/MicrosoftDocs/mslearn-dotnet-cloudnative-devops 存放庫派生到您自己的 GitHub 帳戶,並在本機複製它。 然後:

  1. 安裝任何 系統要求 ,以在 Visual Studio Code 中執行開發容器。
  2. 確定 Docker 是執行狀態。
  3. 在新的 Visual Studio Code 視窗中,開啟複製存放庫的資料夾
  4. Ctrl+Shift+P 以開啟命令選擇區。
  5. 搜尋:>開發容器:重建並在容器中重新開啟
  6. Visual Studio Code 會在本機上建立您的開發容器。

建置容器

  1. 在終端機窗格中,執行此 dotnet CLI 命令:

    dotnet publish /p:PublishProfile=DefaultContainer 
    

建立 Azure 資源

  1. 在終端機窗格中,使用下列 Azure CLI 命令登入 Azure:

    az login --use-device-code
    
  2. 檢視選取的 Azure 訂用帳戶。

    az account show -o table
    

    如果選取錯誤的訂用帳戶,請使用 az account set 命令來選取正確的訂用帳戶。

  3. 執行下列 Azure CLI 命令以取得 Azure 區域清單及其相關聯的名稱:

    az account list-locations -o table
    

    找出最接近您的區域,並在下一個步驟中加以使用,方法是取代 [Closest Azure region]

  4. 執行下列 bash 語句:

    export LOCATION=[Closest Azure region]
    export RESOURCE_GROUP=rg-eshop
    export CLUSTER_NAME=aks-eshop
    export ACR_NAME=acseshop$SRANDOM
    

    上述命令會建立您將在下一個 Azure CLI 命令中使用的環境變數。 您必須將 LOCATION 變更為您附近的 Azure 區域;例如 eastus。 如果您想要資源群組、AKS 叢集或 ACR 的不同名稱,請變更這些值。 若要在 Azure 入口網站中檢視您的新存放庫,請在容器登錄的存取控制 (IAM) 中將自己指派為應用程式合規性自動化系統管理員

  5. 執行下列 Azure CLI 命令:

    az group create --name $RESOURCE_GROUP --location $LOCATION
    az acr create --resource-group $RESOURCE_GROUP --name $ACR_NAME --sku Basic
    az acr login --name $ACR_NAME
    

    如果您在執行az acr login --name $ACR_Name時收到驗證錯誤,您需要在 Azure 中的 [設定 - 存取金鑰] 底下,將新建立的容器註冊中的管理員使用者開啟。 Azure 會提示您輸入這些認證以繼續。 您也可以再次使用 az login --use-device-code進行驗證。

    這些命令會建立資源群組來包含 Azure 資源、適用於映像的 ACR,然後登入 ACR。 可能需要幾分鐘的時間,才能看到此輸出:

      ...
      },
      "status": null,
      "systemData": {
        "createdAt": "2023-10-19T09:11:51.389157+00:00",
        "createdBy": "",
        "createdByType": "User",
        "lastModifiedAt": "2023-10-19T09:11:51.389157+00:00",
        "lastModifiedBy": "",
        "lastModifiedByType": "User"
      },
      "tags": {},
      "type": "Microsoft.ContainerRegistry/registries",
      "zoneRedundancy": "Disabled"
    }
    Login Succeeded
    
  6. 若要標記映像並將其推送至您所建立的 ACR,請執行下列命令:

    docker tag store $ACR_NAME.azurecr.io/storeimage:v1
    docker tag products $ACR_NAME.azurecr.io/productservice:v1
    
    docker push $ACR_NAME.azurecr.io/storeimage:v1
    docker push $ACR_NAME.azurecr.io/productservice:v1
    

    您可以使用此指令來檢查推送映像是否順利完成:

    az acr repository list --name $ACR_NAME --output table
    
  7. 建立 AKS,並使用下列命令將其連線到 ACR:

    az aks create --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --node-count 1 --generate-ssh-keys --node-vm-size Standard_B2s --network-plugin azure --attach-acr $ACR_NAME
    
    az aks get-credentials --name $CLUSTER_NAME --resource-group $RESOURCE_GROUP
    

    上述命令會建立單一節點 AKS 叢集、將其連線到 ACR,然後將本機電腦連線到 AKS 叢集。 上述命令可能需要幾分鐘的時間才能完成。

  8. 使用此指令檢查新的 AKS 是否可以從 ACR 提取映像:

    az aks check-acr --acr $ACR_NAME.azurecr.io --name $CLUSTER_NAME --resource-group $RESOURCE_GROUP
    

    您應該會看到類似下列訊息的輸出:

    [2023-10-19T13:33:09Z] Loading azure.json file from /etc/kubernetes/azure.json
    [2023-10-19T13:33:09Z] Checking managed identity...
    [2023-10-19T13:33:09Z] Cluster cloud name: AzurePublicCloud
    [2023-10-19T13:33:09Z] Kubelet managed identity client ID: 00001111-aaaa-2222-bbbb-3333cccc4444
    [2023-10-19T13:33:09Z] Validating managed identity existance: SUCCEEDED
    [2023-10-19T13:33:09Z] Validating image pull permission: SUCCEEDED
    [2023-10-19T13:33:09Z] 
    Your cluster can pull images from acseshop1251599299.azurecr.io!
    

    您現在可以對新的 AKS 叢集執行 kubectl 命令。 從輸出複製完整的 ACR URL;例如,上述 URL 是 acseshop1251599299。

  9. 檢查 AKS 叢集的狀態:

    kubectl get nodes -A
    

    您應該會看到類似下列訊息的輸出:

    NAME                                STATUS   ROLES   AGE     VERSION
    aks-nodepool1-37200563-vmss000000   Ready    agent   3h44m   v1.26.6
    

設定 Kubernetes 部署指令清單

現在 eShop 映像位於 ACR 中,您可以更新 AKS 部署指令清單以使用這些新映像。

  1. 在 Visual Studio Code 的 [檔案總管] 面板中,選取位於專案根目錄中的 deployment.yml 檔案。

  2. 取代第 17 行:

    - image: [replace with your ACR name].azurecr.io/storeimage:v1
    

    貼上上一個步驟中複製的 ACR 名稱 – 這一行看起來應該類似下列 yaml:

    - image: acseshop1251599299.azurecr.io/storeimage:v1
    
  3. 針對第 65 行重複這些步驟:

    - image: [replace with your ACR name].azurecr.io/productservice:v1
    

    使用 CTRL+S 儲存檔案。

  4. 在終端機窗格中,使用下列 kubernetes 命令部署 NGINX 輸入控制器:

    kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.3/deploy/static/provider/cloud/deploy.yaml
    

    上述 kubectl 命令會新增服務和元件,以允許輸入 AKS 叢集。 使用下列 kubernetes 命令檢查輸入是否已準備好執行:

    kubectl get services --namespace ingress-nginx 
    

    您應該會看到類似下列訊息的輸出:

    NAME                                 TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)                      AGE
    ingress-nginx-controller             LoadBalancer   10.0.135.51    20.26.154.64   80:32115/TCP,443:32254/TCP   58s
    ingress-nginx-controller-admission   ClusterIP      10.0.137.137   <none>         443/TCP                      58s
    
  5. 使用此指令部署 eShop 應用程式:

    kubectl apply -f deployment.yml
    

    kubectl 套用命令會部署 eShop 應用程式、前端 Blazor Web 應用程式和後端 REST API 產品服務,以及將流量路由傳送至 AKS 叢集之正確服務的輸入規則。 如果您在部署上收到任何錯誤,請重新執行此命令。

    您應該會看到類似下列訊息的輸出:

    deployment.apps/storeimage created
    service/eshop-website created
    deployment.apps/productservice created
    service/eshop-backend created
    ingress.networking.k8s.io/eshop-ingress created
    
  6. 使用此指令檢查兩個微服務是否已部署:

    kubectl get pods -A
    

    您應該會看到類似下列訊息的輸出:

    NAMESPACE       NAME                                        READY   STATUS      RESTARTS   AGE
    default         productservice-7569b8c64-vfbfz              1/1     Running     0          3m56s
    default         storeimage-6c7c999d7c-zsnxd                 1/1     Running     0          3m56s
    ingress-nginx   ingress-nginx-admission-create-szb8l        0/1     Completed   0          4m4s
    ingress-nginx   ingress-nginx-admission-patch-czdbv         0/1     Completed   0          4m4s
    ingress-nginx   ingress-nginx-controller-58bf5bf7dc-nwtsr   1/1     Running     0          4m4s
    
  7. 使用此指令檢視已部署的 eShop:

    echo "http://$(kubectl get services --namespace ingress-nginx ingress-nginx-controller --output jsonpath='{.status.loadBalancer.ingress[0].ip}')"
    

    上述命令會傳回 Web 應用程式的外部 IP 位址。 按住 CTRL 並點擊連結,在新分頁中開啟應用程式。

    eShop Web 應用程式首頁的螢幕快照。

建立服務主體以從 GitHub 部署

GitHub Actions 可以將容器映射發佈至 Azure Container Registry。 因此,GitHub 執行器必須具有連線至 Azure 的許可權。 下列步驟會建立 Azure AD 服務主體,以作為 Azure 內的 GitHub Actions 身分識別。

  1. 若要將訂用帳戶標識碼儲存在環境變數中,請在終端機中執行下列命令:

    export SUBS=$(az account show --query 'id' --output tsv)
    
  2. 若要建立 Azure AD 服務主體以允許從 GitHub 存取,請執行下列命令:

    az ad sp create-for-rbac --name "eShop" --role contributor --scopes /subscriptions/$SUBS/resourceGroups/$RESOURCE_GROUP --json-auth
    

    隨即出現下列輸出的變化:

    Creating 'Contributor' role assignment under scope '/subscriptions/ffffffff-aaaa-bbbb-6666-777777777777'
    
    The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli
     {
      "clientId": "00001111-aaaa-2222-bbbb-3333cccc4444",
      "clientSecret": "abc1A~abc123ABC123abc123ABC123abc123ABC1",
      "subscriptionId": "00000000-0000-0000-0000-000000000000",
      "tenantId": "00000000-0000-0000-0000-000000000000",
      "activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
      "resourceManagerEndpointUrl": "https://management.azure.com/",
      "activeDirectoryGraphResourceId": "https://graph.windows.net/",
      "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
      "galleryEndpointUrl": "https://gallery.azure.com/",
      "managementEndpointUrl": "https://management.core.windows.net/"
    }
    
  3. 複製 JSON 輸出和括弧,以在下一個步驟中使用。

建立 GitHub 秘密

GitHub Actions 執行器會使用認證來與 Container Registry 和 AKS 互動。 容器登錄的服務主體和認證是敏感性資訊。 最好將敏感性資訊以加密的形式儲存在安全的位置作為秘密。 GitHub 提供內建位置來儲存秘密和其他變數。

完成下列步驟,以安全地將敏感性資訊儲存為存放庫中的環境變數。 存放庫管理員應該管理 GitHub Actions 執行器可以存取的秘密。

  1. 在您的分支 GitHub 存放庫中,移至 Settings>Secrets and variables>Actions

  2. Actions secrets and variables 頁面上,選取 New repository secret

  3. New secret 頁面上,於 Name 輸入 AZURE_CREDENTIALS,接著在 Secret 下輸入您從終端機複製的 JSON 輸出。

    這些設定看起來應該類似下列螢幕快照:

    在 GitHub 中設定環境變數秘密之 [新增秘密] 頁面的螢幕快照。

  4. 選取 Add secret

您將在下一節使用此 GitHub 秘密來建立 GitHub 動作來建置容器映像。