教學課程:調整 Azure Kubernetes Service (AKS) 中的應用程式Tutorial: Scale applications in Azure Kubernetes Service (AKS)

如果您已依照教學課程操作,就會在 AKS 中有一個正常運作的 Kubernetes 叢集,並已部署範例 Azure 投票應用程式。If you've followed the tutorials, you have a working Kubernetes cluster in AKS and you deployed the sample Azure Voting app. 在本教學課程中 (七個章節的第五部分),您會將應用程式中的 Pod 相應放大,然後嘗試進行 Pod 自動調整。In this tutorial, part five of seven, you scale out the pods in the app and try pod autoscaling. 您也會了解如何調整 Azure VM 節點的數目,以變更叢集裝載工作負載的容量。You also learn how to scale the number of Azure VM nodes to change the cluster's capacity for hosting workloads. 您會了解如何:You learn how to:

  • 調整 Kubernetes 節點Scale the Kubernetes nodes
  • 手動調整執行應用程式的 Kubernetes PodManually scale Kubernetes pods that run your application
  • 設定執行應用程式前端的自動調整 PodConfigure autoscaling pods that run the app front-end

在其他教學課程中,Azure 投票應用程式會更新為新版本。In additional tutorials, the Azure Vote application is updated to a new version.

開始之前Before you begin

在先前的教學課程中,已將應用程式封裝為容器映像。In previous tutorials, an application was packaged into a container image. 此映像已上傳至 Azure Container Registry,而您已建立 AKS 叢集。This image was uploaded to Azure Container Registry, and you created an AKS cluster. 接著已將應用程式部署至 AKS 叢集。The application was then deployed to the AKS cluster. 如果您尚未完成這些步驟,而且想要跟著做,請從教學課程 1 – 建立容器映像開始。If you haven't done these steps, and would like to follow along, start with Tutorial 1 – Create container images.

在本教學課程中,您必須執行 Azure CLI 2.0.53 版或更新版本。This tutorial requires that you're running the Azure CLI version 2.0.53 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

手動調整 PodManually scale pods

在先前的教學課程中部署 Azure Vote 前端與 Redis 執行個體時,已建立單一複本。When the Azure Vote front-end and Redis instance were deployed in previous tutorials, a single replica was created. 若要查看叢集中的 Pod 數目和狀態,請使用 kubectl get 命令,如下所示:To see the number and state of pods in your cluster, use the kubectl get command as follows:

kubectl get pods

下列範例輸出會顯示一個前端 Pod 和一個後端 Pod:The following example output shows one front-end pod and one back-end pod:

NAME                               READY     STATUS    RESTARTS   AGE
azure-vote-back-2549686872-4d2r5   1/1       Running   0          31m
azure-vote-front-848767080-tf34m   1/1       Running   0          31m

若要手動變更 azure-vote-front 部署中的 Pod 數目,請使用 kubectl scale 命令。To manually change the number of pods in the azure-vote-front deployment, use the kubectl scale command. 下列範例會將前端 Pod 的數目增加為 5The following example increases the number of front-end pods to 5:

kubectl scale --replicas=5 deployment/azure-vote-front

再次執行 kubectl get pods,以確認 AKS 是否建立其他 Pod。Run kubectl get pods again to verify that AKS creates the additional pods. 大約一分鐘後,其他 Pod 就會成為叢集中的可用項目:After a minute or so, the additional pods are available in your cluster:

$ kubectl get pods

                                    READY     STATUS    RESTARTS   AGE
azure-vote-back-2606967446-nmpcf    1/1       Running   0          15m
azure-vote-front-3309479140-2hfh0   1/1       Running   0          3m
azure-vote-front-3309479140-bzt05   1/1       Running   0          3m
azure-vote-front-3309479140-fvcvm   1/1       Running   0          3m
azure-vote-front-3309479140-hrbf2   1/1       Running   0          15m
azure-vote-front-3309479140-qphz8   1/1       Running   0          3m

自動調整 PodAutoscale pods

Kubernetes 支援水平 Pod 自動調整 to adjust the number of pods in a deployment depending on CPU utilization or other select metrics. The Metrics Server 可用來將資源使用率提供給 Kubernetes,並且會自動部署在 AKS 叢集 1.10 版和更新版本中。Kubernetes supports horizontal pod autoscaling to adjust the number of pods in a deployment depending on CPU utilization or other select metrics. The Metrics Server is used to provide resource utilization to Kubernetes, and is automatically deployed in AKS clusters versions 1.10 and higher. 若要查看您的 AKS 叢集版本,請使用 az aks show 命令,如下列範例所示:To see the version of your AKS cluster, use the az aks show command, as shown in the following example:

az aks show --resource-group myResourceGroup --name myAKSCluster --query kubernetesVersion

如果您的 AKS 叢集低於 1.10 版,請安裝計量伺服器,否則請略過此步驟。If your AKS cluster is less than 1.10, install the Metrics Server, otherwise skip this step. 若要安裝,請複製 metrics-server GitHub 存放庫並安裝範例資源定義。To install, clone the metrics-server GitHub repo and install the example resource definitions. 若要檢視這些 YAML 定義的內容,請參閱Kuberenetes 1.8+ 的計量伺服器To view the contents of these YAML definitions, see Metrics Server for Kuberenetes 1.8+.

git clone https://github.com/kubernetes-incubator/metrics-server.git
kubectl create -f metrics-server/deploy/1.8+/

若要使用自動調整程式,必須為您 Pod 中所有容器和 Pod 定義 CPU 要求和限制。To use the autoscaler, all containers in your pods and your pods must have CPU requests and limits defined. azure-vote-front 部署中,前端容器已經要求 0.25 個 CPU,限制為 0.5 個 CPU。In the azure-vote-front deployment, the front-end container already requests 0.25 CPU, with a limit of 0.5 CPU. 這些資源要求和限制定義如下列範例程式碼片段所示:These resource requests and limits are defined as shown in the following example snippet:

resources:
  requests:
     cpu: 250m
  limits:
     cpu: 500m

下列範例會使用 kubectl autoscale 命令自動調整 azure-vote-front 部署中的 Pod 數目。The following example uses the kubectl autoscale command to autoscale the number of pods in the azure-vote-front deployment. 如果 CPU 使用率超過 50%,自動調整程式就會增加 Pod,最多可達 10 個執行個體。If CPU utilization exceeds 50%, the autoscaler increases the pods up to a maximum of 10 instances. 然後,為此部署定義最少 3 個執行個體:A minimum of 3 instances is then defined for the deployment:

kubectl autoscale deployment azure-vote-front --cpu-percent=50 --min=3 --max=10

若要查看自動調整程式的狀態,請使用 kubectl get hpa 命令,如下所示:To see the status of the autoscaler, use the kubectl get hpa command as follows:

$ kubectl get hpa

NAME               REFERENCE                     TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
azure-vote-front   Deployment/azure-vote-front   0% / 50%   3         10        3          2m

幾分鐘之後,如果 Azure Vote 應用程式上的負載降到最低,Pod 複本數目就會自動降低成三個。After a few minutes, with minimal load on the Azure Vote app, the number of pod replicas decreases automatically to three. 您可以再次使用 kubectl get pods,以查看移除的非必要 Pod。You can use kubectl get pods again to see the unneeded pods being removed.

手動調整 AKS 節點Manually scale AKS nodes

如果您在上一個教學課程中使用命令建立了 Kubernetes 叢集,它會有一個節點。If you created your Kubernetes cluster using the commands in the previous tutorial, it has one node. 如果您打算在叢集上增加或減少容器工作負載,可以手動調整節點的數目。You can adjust the number of nodes manually if you plan more or fewer container workloads on your cluster.

下列範例會在名為 myAKSCluster 的 Kubernetes 叢集中,將節點的數目增加到三個。The following example increases the number of nodes to three in the Kubernetes cluster named myAKSCluster. 此命令需要幾分鐘的時間來完成。The command takes a couple of minutes to complete.

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

成功調整叢集後,輸出會類似下列範例:When the cluster has successfully scaled, the output is similar to following example:

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

後續步驟Next steps

在本教學課程中,您已在 Kubernetes 叢集中使用不同的調整功能。In this tutorial, you used different scaling features in your Kubernetes cluster. 您已了解如何︰You learned how to:

  • 手動調整執行應用程式的 Kubernetes PodManually scale Kubernetes pods that run your application
  • 設定執行應用程式前端的自動調整 PodConfigure autoscaling pods that run the app front-end
  • 手動調整 Kubernetes 節點Manually scale the Kubernetes nodes

請繼續進行下一個教學課程,以了解如何更新 Kubernetes 中的應用程式。Advance to the next tutorial to learn how to update application in Kubernetes.