您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:在 Azure Kubernetes 服务 (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. 在本教程第 5 部分(共 7 部分)中,会在应用中扩大 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容器注册表,同时,你创建了 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 投票前端和 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 自动缩放以根据 CPU 利用率或其他选择指标调整部署中的 Pod 数。Kubernetes supports horizontal pod autoscaling to adjust the number of pods in a deployment depending on CPU utilization or other select metrics. 指标服务器用来将资源利用率提供给 Kubernetes,可自动部署在 AKS 群集 1.10 及更高版本中。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, the Metrics Server is not automatically installed. 若要进行安装,请克隆 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. 如果所有 Pod 的平均 CPU 利用率超过其请求使用率的 50%,则自动缩放程序会将 Pod 增加到最多 10 个实例。If average CPU utilization across all pods exceeds 50% of their requested usage, the autoscaler increases the pods up to a maximum of 10 instances. 为部署定义的最小实例数为 3A 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 投票应用处于最小负荷状态几分钟之后,Pod 副本数会自动减少到 3 个。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 two nodes. 如果计划在群集上有更多或更少的容器工作负荷,则可以手动调整节点数。You can adjust the number of nodes manually if you plan more or fewer container workloads on your cluster.

下面的示例将名为 myAKSCluster 的 Kubernetes 群集中的节点数增加到 3 个。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.