教學課程:在 Azure Kubernetes Service (AKS) 中縮放應用程式

如果您上過先前的教學課程,就會有一個可運作的 Kube 叢集和 Azure Store Front 應用程式。

在本教學課程中 (第六部分,共七個部分),您要擴增應用程式中的 Pod、嘗試 Pod 自動縮放,以及縮放 Azure VM 節點數量,以變更叢集裝載工作負載的容量。 您將學習如何:

  • 縮放 Kube 節點。
  • 手動縮放執行應用程式的 Kube Pod。
  • 設定執行應用程式前端的自動縮放 Pod。

開始之前

在先前的教學課程中,您將應用程式封裝成容器映像、將這些映像上傳至 Azure Container Registry、建立 AKS 叢集、部署應用程式,以及使用 Azure 服務匯流排重新部署更新後的應用程式。 如果您尚未完成這些步驟,並且想要跟著做,請從教學課程 1:準備 AKS 的應用程式開始。

本教學課程需要 Azure CLI 2.34.1 版或更新版本。 執行 az --version 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI

手動調整 Pod

  1. 使用 kubectl get 命令檢視叢集中的 Pod。

    kubectl get pods
    

    下列輸出範例會顯示正在執行 Azure Store Front 應用程式的 Pod:

    NAME                               READY     STATUS     RESTARTS   AGE
    order-service-848767080-tf34m      1/1       Running    0          31m
    product-service-4019737227-2q2qz   1/1       Running    0          31m
    store-front-2606967446-2q2qz       1/1       Running    0          31m
    
  2. 使用 kubectl scale 命令變更 store-front 部署中的 Pod 數量。

    kubectl scale --replicas=5 deployment.apps/store-front
    
  3. 使用 kubectl get pods 命令驗證已建立其他 Pod。

    kubectl get pods
    

    下列輸出範例會顯示正在執行 Azure Store Front 應用程式的其他 Pod:

                                      READY     STATUS    RESTARTS   AGE
    store-front-2606967446-2q2qzc     1/1       Running   0          15m
    store-front-3309479140-2hfh0      1/1       Running   0          3m
    store-front-3309479140-bzt05      1/1       Running   0          3m
    store-front-3309479140-fvcvm      1/1       Running   0          3m
    store-front-3309479140-hrbf2      1/1       Running   0          15m
    store-front-3309479140-qphz8      1/1       Running   0          3m
    

自動調整 Pod

若要使用水平 Pod 自動縮放程式,所有容器都必須已定義 CPU 要求和限制,而且 Pod 必須具有指定的要求。 在 aks-store-quickstart 部署中,前端容器會要求 1 百萬個 CPU,限制為 1,000 萬個 CPU。

每個容器都定義這些資源要求和限制,如下列壓縮範例 YAML 所示:

...
  containers:
  - name: store-front
    image: ghcr.io/azure-samples/aks-store-demo/store-front:latest
    ports:
    - containerPort: 8080
      name: store-front
...
    resources:
      requests:
        cpu: 1m
...
      limits:
        cpu: 1000m
...

使用資訊清單檔自動縮放 Pod

  1. 建立資訊清單檔來定義自動縮放程式行為和資源限制,如下列壓縮範例資訊清單檔 aks-store-quickstart-hpa.yaml 所示:

    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: store-front-hpa
    spec:
      maxReplicas: 10 # define max replica count
      minReplicas: 3  # define min replica count
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: store-front
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 50
    
  2. 使用 kubectl apply 命令套用自動縮放程式資訊清單檔。

    kubectl apply -f aks-store-quickstart-hpa.yaml
    
  3. 使用 kubectl get hpa 命令檢查自動縮放程式的狀態。

    kubectl get hpa
    

    幾分鐘之後,如果 Azure Store Front 應用程式上的負載降到最低,Pod 複本數就會降低成 3 個。 您可以再次使用 kubectl get pods,以查看移除的非必要 Pod。

注意

您可以啟用以 Kube 為基礎的 Event-Driven Autoscaling (KEDA) AKS 附加元件到叢集,以根據需要處理的事件數目來驅動縮放。 如需詳細資訊,請參閱使用 Kubernetes Event-Driven Autoscaling (KEDA) 附加元件 (預覽版) 簡化應用程式自動縮放

手動調整 AKS 節點

如果您在上一個教學課程中使用命令建立了 Kube 叢集,其中會有兩個節點。 如果您想要增加或減少此數量,可以手動調整節點數目。

下列範例會在名為 myAKSCluster 的 Kubernetes 叢集中,將節點的數目增加到三個。 此命令需要幾分鐘的時間來完成。

  • 使用 az aks scale 命令縮放叢集節點。

    az aks scale --resource-group myResourceGroup --name myAKSCluster --node-count 3
    

    叢集成功縮放之後,您的輸出將會類似下列輸出範例:

    "agentPoolProfiles": [
      {
        "count": 3,
        "dnsPrefix": null,
        "fqdn": null,
        "name": "myAKSCluster",
        "osDiskSizeGb": null,
        "osType": "Linux",
        "ports": null,
        "vmSize": "Standard_D2_v2",
        "vnetSubnetId": null
      }
    

您也可以自動縮放叢集中的節點。 如需詳細資訊,請參閱使用叢集自動縮放程式搭配節點集區

下一步

在本教學課程中,您已在 Kubernetes 叢集中使用不同的調整功能。 您已了解如何︰

  • 手動縮放執行應用程式的 Kube Pod。
  • 設定執行應用程式前端的自動縮放 Pod。
  • 手動縮放 Kube 節點。

在下一個教學課程中,您會了解如何在 AKS 叢集中升級 Kube。