在 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 may also 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. 若要手動調整,您可以定義複本或節點計數,Kubernetes API 則會安排建立其他 Pod 或清空節點。To manually scale, you define the replica or node count, and the Kubernetes API schedules creating additional pods or draining nodes.

若要開始手動調整 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 中的自動調整 PodTo 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 has been able to receive application workload and the resource demands to adjust accordingly.

若要盡可能減少這些競爭事件,可以設定冷卻時間或延遲值。To minimize these race events, cooldown or delay values can be 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 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

您可能需要調整這些冷卻時間值。You may need to tune these cooldown values. 預設的冷卻時間值可能會讓您認為水平 Pod 自動調整程式調整複本計數不夠快速。The default cooldown values may give the impression that the horizontal pod autoscaler isn't scaling the replica count quickly enough. 例如,若要更快速地增加使用中的複本數目,當您使用 kubectl 建立水平 Pod 自動調整程式定義時,請減少 --horizontal-pod-autoscaler-upscale-delayFor example, to more quickly increase the number of replicas in use, reduce the --horizontal-pod-autoscaler-upscale-delay when you create your horizontal pod autoscaler definitions using kubectl.

叢集自動調整程式Cluster autoscaler

若要回應變更 pod 的需求,Kubernetes 會有叢集中自動調整程式 (目前在 AKS 中的預覽) 會根據要求的計算節點集區中資源的節點數目進行調整。To respond to changing pod demands, Kubernetes has a cluster autoscaler (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 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 with a single node pool.

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

相應增加事件Scale up events

如果節點沒有足夠計算資源可執行所需的 Pod,則該 Pod 將無法完成排程的程序。If a node does not have sufficient compute resources to run a requested pod, that pod cannot progress through the scheduling process. 除非節點集區內有額外的計算資源可以使用,否則無法啟動 Pod。The pod cannot start unless additional compute resources are available within the node pool.

當叢集自動調整程式注意到因節點集區資源限制而無法排定 Pod 時,會增加節點集區內的節點數目,以提供額外的計算資源。When the cluster autoscaler notices pods that cannot be scheduled due to 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 have not recently received new scheduling requests. 此案例指出節點集區具有高於所需的計算資源,且節點的數目可以降低。This scenario indicates that the node pool has more compute resources than are required, and that 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: