在 Azure Kubernetes Service (AKS) 中調整應用程式的選項Scaling options for applications in Azure Kubernetes Service (AKS)

當您在 Azure Kubernetes Service (AKS) 中執行應用程式時,可能需要增加或減少計算資源的數量。As you run applications in Azure Kubernetes Service (AKS), you may need to increase or decrease the amount of compute resources. 當您需要的應用程式執行個體數目有所變更時,基礎 Kubernetes 節點的數目可能也需要變更。As the number of application instances you need change, the number of underlying Kubernetes nodes may also need to change. 您也可能需要快速布建大量的額外應用程式實例。You also might need to quickly provision a large number of additional application instances.

本文所介紹的核心概念,可協助您在 AKS 中調整應用程式:This article introduces the core concepts that help you scale applications in AKS:

手動調整 Pod 或節點Manually scale pods or nodes

您可以手動調整複本 (Pod) 及節點,來測試您的應用程式如何回應可用資源和狀態中的變更。You can manually scale replicas (pods) and nodes to test how your application responds to a change in available resources and state. 手動調整資源也可讓您定義用來維護固定成本的資源集數量,例如節點的數目。Manually scaling resources also lets you define a set amount of resources to use to maintain a fixed cost, such as the number of nodes. 若要手動調整,您可以定義複本或節點計數。To manually scale, you define the replica or node count. Kubernetes API 接著會根據該複本或節點計數,排程建立額外的 pod 或清空節點。The Kubernetes API then schedules creating additional pods or draining nodes based on that replica or node count.

若要開始手動調整 pod 和節點,請參閱在 AKS 中調整應用程式To get started with manually scaling pods and nodes see Scale applications in AKS.

水平 Pod 自動調整程式Horizontal pod autoscaler

Kubernetes 會使用水平 Pod 自動調整程式 (HPA) 來監視資源需求,並自動調整複本數目。Kubernetes uses the horizontal pod autoscaler (HPA) to monitor the resource demand and automatically scale the number of replicas. 根據預設,水平 Pod 自動調整程式每隔 30 秒會檢查計量 API,找出複本計數中任何必要的變更。By default, the horizontal pod autoscaler checks the Metrics API every 30 seconds for any required changes in replica count. 需要進行變更時,複本的數目會據以增加或減少。When changes are required, the number of replicas is increased or decreased accordingly. 水平 Pod 自動調整程式會搭配使用 AKS 叢集,當中已部署適用於 Kubernetes 1.8 + 的計量伺服器。Horizontal pod autoscaler works with AKS clusters that have deployed the Metrics Server for Kubernetes 1.8+.

Kubernetes 水平 Pod 自動調整

當您針對指定的部署設定水平 Pod 自動調整程式時,需定義可執行複本的最小和最大數目。When you configure the horizontal pod autoscaler for a given deployment, you define the minimum and maximum number of replicas that can run. 您也會根據任何調整決策定義要監視的計量,例如 CPU 使用量。You also define the metric to monitor and base any scaling decisions on, such as CPU usage.

若要開始使用 AKS 中的水準 pod 自動調整程式,請參閱AKS 中的自動調整 pod。To get started with the horizontal pod autoscaler in AKS, see Autoscale pods in AKS.

調整事件中的冷卻時間Cooldown of scaling events

因為水平 Pod 自動調整程式每隔 30 秒會檢查計量 API,因此在進行另一項檢查之前,先前的調整事件可能尚未成功完成。As the horizontal pod autoscaler checks the Metrics API every 30 seconds, previous scale events may not have successfully completed before another check is made. 此行為可能會導致水準 pod 自動調整程式在先前的調整事件可以接收應用程式工作負載之前變更複本數目,並據此調整資源需求。This behavior could cause the horizontal pod autoscaler to change the number of replicas before the previous scale event could receive application workload and the resource demands to adjust accordingly.

若要將這些競爭事件降至最低,請設定 cooldown 或 delay 值。To minimize these race events, cooldown or delay values are set. 這些值會定義水平 Pod 自動調整程式在調整事件之後,必須等待多久時間,才可以觸發另一個調整事件。These values define how long the horizontal pod autoscaler must wait after a scale event before another scale event can be triggered. 此行為可讓新的複本計數生效,而計量 API 會反映分散式工作負載。This behavior allows the new replica count to take effect and the Metrics API to reflect the distributed workload. 根據預設,相應增加事件的延遲為 3 分鐘,而相應減少事件的延遲為 5 分鐘By default, the delay on scale up events is 3 minutes, and the delay on scale down events is 5 minutes

目前,您無法從預設值微調這些 cooldown 值。Currently, you can't tune these cooldown values from the default.

叢集自動調整程式Cluster autoscaler

為了回應變更的 pod 需求,Kubernetes 有一個叢集自動調整程式(目前在 AKS 中為預覽狀態),它會根據節點集區中要求的計算資源來調整節點數目。To respond to changing pod demands, Kubernetes has a cluster autoscaler, which is currently in preview in AKS, that adjusts the number of nodes based on the requested compute resources in the node pool. 根據預設,叢集自動調整程式會每隔10秒檢查一次計量 API 伺服器,以取得節點計數中的任何必要變更。By default, the cluster autoscaler checks the Metrics API server every 10 seconds for any required changes in node count. 如果叢集自動調整判斷需要變更,則您 AKS 叢集中的節點數目會據以增加或減少。If the cluster autoscale determines that a change is required, the number of nodes in your AKS cluster is increased or decreased accordingly. 叢集自動調整程式會使用已啟用 RBAC 功能且執行 Kubernetes 1.10.x 或更高版本的 AKS 叢集。The cluster autoscaler works with RBAC-enabled AKS clusters that run Kubernetes 1.10.x or higher.

Kubernetes 叢集自動調整程式

叢集自動調整程式通常會與水平 Pod 自動調整程式一起使用。Cluster autoscaler is typically used alongside the horizontal pod autoscaler. 加以結合時,水平 Pod 自動調整程式會根據應用程式需求來增加或減少 Pod 的數目,而叢集自動調整程式會據此來調整所需的節點數目,以執行這些額外的 Pod。When combined, the horizontal pod autoscaler increases or decreases the number of pods based on application demand, and the cluster autoscaler adjusts the number of nodes as needed to run those additional pods accordingly.

叢集自動調整程式只能在 AKS 叢集的預覽中進行測試。Cluster autoscaler should only be tested in preview on AKS clusters.

若要開始在 AKS 中使用叢集自動調整程式,請參閱AKS 上的叢集自動調整程式。To get started with the cluster autoscaler in AKS, see Cluster Autoscaler on AKS.

相應增加事件Scale up events

如果節點沒有足夠的計算資源可執行要求的 pod,該 pod 就無法透過排程程式進行。If a node doesn't have sufficient compute resources to run a requested pod, that pod can't progress through the scheduling process. 除非節點集區中有額外的計算資源可供使用,否則 pod 無法啟動。The pod can't start unless additional compute resources are available within the node pool.

當叢集自動調整程式注意到因為節點集區資源條件約束而無法排程的 pod 時,節點集區中的節點數目會增加,以提供額外的計算資源。When the cluster autoscaler notices pods that can't be scheduled because of node pool resource constraints, the number of nodes within the node pool is increased to provide the additional compute resources. 當這些額外的節點已成功部署且可供在節點集區內使用時,就會將 Pod 排程在節點上執行。When those additional nodes are successfully deployed and available for use within the node pool, the pods are then scheduled to run on them.

如果您必須快速調整應用程式,某些 Pod 會維持在等候排程的狀態,直到由叢集自動調整程式部署的其他節點可以接受排程的 Pod 為止。If your application needs to scale rapidly, some pods may remain in a state waiting to be scheduled until the additional nodes deployed by the cluster autoscaler can accept the scheduled pods. 對於具有高載需求的應用程式,您可以使用虛擬節點與 Azure 容器執行個體進行調整。For applications that have high burst demands, you can scale with virtual nodes and Azure Container Instances.

相應減少事件Scale down events

叢集自動調整程式也會監視最近尚未收到新排程要求之節點的 pod 排程狀態。The cluster autoscaler also monitors the pod scheduling status for nodes that haven't recently received new scheduling requests. 此案例指出節點集區具有比所需更多的計算資源,而且可以減少節點數目。This scenario indicates the node pool has more compute resources than are required, and the number of nodes can be decreased.

依預設,節點若超過 10 分鐘未使用的閾值,則會安排將此節點刪除。A node that passes a threshold for no longer being needed for 10 minutes by default is scheduled for deletion. 當這種情況發生時,Pod 會排程在節點集區內的其他節點上執行,且叢集自動調整程式會減少節點的數目。When this situation occurs, pods are scheduled to run on other nodes within the node pool, and the cluster autoscaler decreases the number of nodes.

因為叢集自動調整程式在減少節點的數目時,Pod 會排程在不同節點上,所以您的應用程式可能會遇到一些中斷情況。Your applications may experience some disruption as pods are scheduled on different nodes when the cluster autoscaler decreases the number of nodes. 若要盡可能不中斷,請避免使用單一 Pod 執行個體的應用程式。To minimize disruption, avoid applications that use a single pod instance.

高載至 Azure 容器執行個體Burst to Azure Container Instances

若要快速調整您的 AKS 叢集,您可以與 Azure 容器執行個體 (ACI) 整合。To rapidly scale your AKS cluster, you can integrate with Azure Container Instances (ACI). Kubernetes 有內建的元件,可調整複本和節點計數。Kubernetes has built-in components to scale the replica and node count. 不過,如果您的應用程式需要快速調整,水平 Pod 自動調整程式可能排程更多 Pod,比節點集區中現有計算資源所能提供的還要多。However, if your application needs to rapidly scale, the horizontal pod autoscaler may schedule more pods than can be provided by the existing compute resources in the node pool. 如果設定完成,則這種情況可能會觸發叢集自動調整程式在節點集區中部署其他節點,但是這些節點可能需要幾分鐘的時間才能成功佈建,並允許 Kubernetes 排程器在節點上執行 Pod。If configured, this scenario would then trigger the cluster autoscaler to deploy additional nodes in the node pool, but it may take a few minutes for those nodes to successfully provision and allow the Kubernetes scheduler to run pods on them.

Kubernetes 高載調整至 ACI

ACI 可讓您快速部署容器執行個體,不需要額外的基礎結構成本。ACI lets you quickly deploy container instances without additional infrastructure overhead. 當您與 AKS 連線時,ACI 會變成您 AKS 叢集的安全邏輯擴充功能。When you connect with AKS, ACI becomes a secured, logical extension of your AKS cluster. Virtual Kubelet 元件會安裝在您的 AKS 叢集,使 ACI 作為虛擬的 Kubernetes 節點。The Virtual Kubelet component is installed in your AKS cluster that presents ACI as a virtual Kubernetes node. 接著,Kubernetes 可以排程透過虛擬節點以 ACI 執行個體身分執行的 Pod,而非直接在 AKS 叢集中,以 VM 節點上的 Pod 身分執行。Kubernetes can then schedule pods that run as ACI instances through virtual nodes, not as pods on VM nodes directly in your AKS cluster. 虛擬節點目前在 AKS 中處於預覽狀態。Virtual nodes are currently in preview in AKS.

您的應用程式不需要進行任何修改即可使用虛擬節點。Your application requires no modification to use virtual nodes. 部署可以跨 AKS 和 ACI 調整,且當叢集自動調整程式在 AKS 叢集中部署新的節點時沒有延遲。Deployments can scale across AKS and ACI and with no delay as cluster autoscaler deploys new nodes in your AKS cluster.

虛擬節點會部署到與您 AKS 叢集相同虛擬網路中的其他子網路。Virtual nodes are deployed to an additional subnet in the same virtual network as your AKS cluster. 此虛擬網路設定可維護 ACI 與 AKS 之間流量的安全。This virtual network configuration allows the traffic between ACI and AKS to be secured. 如同 AKS 叢集,ACI 執行個體是安全的邏輯計算資源,會與其他使用者隔離。Like an AKS cluster, an ACI instance is a secure, logical compute resource that is isolated from other users.

後續步驟Next steps

若要開始調整應用程式,請先遵循快速入門,使用 Azure CLI 建立 AKS叢集。To get started with scaling applications, first follow the quickstart to create an AKS cluster with the Azure CLI. 接著,您可以在 AKS 叢集中開始手動或自動調整應用程式:You can then start to manually or automatically scale applications in your AKS cluster:

如需有關 Kubernetes 及 AKS 核心概念的詳細資訊,請參閱下列文章:For more information on core Kubernetes and AKS concepts, see the following articles: