diff --git a/content/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough.md b/content/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough.md index d11843826c..2dd70c6a2f 100644 --- a/content/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough.md +++ b/content/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough.md @@ -1,7 +1,8 @@ --- -title: Horizontal Pod Autoscaler 演练 +title: HorizontalPodAutoscaler 演练 content_type: task weight: 100 +min-kubernetes-server-version: 1.23 --- -Horizontal Pod Autoscaler 可以根据 CPU 利用率自动扩缩 ReplicationController、 -Deployment、ReplicaSet 或 StatefulSet 中的 Pod 数量 -(也可以基于其他应用程序提供的度量指标,目前这一功能处于 beta 版本)。 +[HorizontalPodAutoscaler](/zh/docs/tasks/run-application/horizontal-pod-autoscale/) (简称 HPA ) +自动更新工作负载资源(例如 {{< glossary_tooltip text="Deployment" term_id="deployment" >}} 或者 +{{< glossary_tooltip text="StatefulSet" term_id="statefulset" >}}), +目的是自动扩缩工作负载以满足需求。 + +水平扩缩意味着对增加的负载的响应是部署更多的 {{< glossary_tooltip text="Pods" term_id="pod" >}}。 +这与 “垂直(Vertical)” 扩缩不同,对于 Kubernetes, +垂直扩缩意味着将更多资源(例如:内存或 CPU)分配给已经为工作负载运行的 Pod。 -本文将引领你了解如何为 php-apache 服务器配置和使用 Horizontal Pod Autoscaler。 -与 Horizontal Pod Autoscaler 相关的更多信息请参阅 -[Horizontal Pod Autoscaler 用户指南](/zh/docs/tasks/run-application/horizontal-pod-autoscale/)。 +如果负载减少,并且 Pod 的数量高于配置的最小值, +HorizontalPodAutoscaler 会指示工作负载资源( Deployment、StatefulSet 或其他类似资源)缩减。 + +本文档将引导你完成启用 HorizontalPodAutoscaler 以自动管理示例 Web 应用程序的扩缩的示例。 +此示例工作负载是运行一些 PHP 代码的 Apache httpd。 ## {{% heading "prerequisites" %}} - -本文示例需要一个运行中的 Kubernetes 集群以及 kubectl,版本为 1.2 或更高。 -[Metrics 服务器](https://github.com/kubernetes-incubator/metrics-server/) -需要被部署到集群中,以便通过 [Metrics API](https://github.com/kubernetes/metrics) -提供度量数据。 -Horizontal Pod Autoscaler 根据此 API 来获取度量数据。 -要了解如何部署 metrics-server,请参考 -[metrics-server 文档](https://github.com/kubernetes-incubator/metrics-server/) 。 +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} -如果需要为 Horizontal Pod Autoscaler 指定多种资源度量指标,你的 Kubernetes -集群以及 kubectl 至少需要达到 1.6 版本。 -此外,如果要使用自定义度量指标,你的 Kubernetes 集群还必须能够与提供这些自定义指标 -的 API 服务器通信。 -最后,如果要使用与 Kubernetes 对象无关的度量指标,则 Kubernetes 集群版本至少需要 -达到 1.10 版本,同样,需要保证集群能够与提供这些外部指标的 API 服务器通信。 -更多详细信息,请参阅 -[Horizontal Pod Autoscaler 用户指南](/zh/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics)。 +如果你运行的是旧版本的 Kubernetes,请参阅该版本的文档版本 +([可用的文档版本](/zh/docs/home/supported-doc-versions/))。 + + +按照本演练进行操作,你需要一个部署并配置了 +[Metrics Server](https://github.com/kubernetes-sigs/metrics-server#readme) 的集群。 +Kubernetes Metrics Server 从集群中的 {{}} 收集资源指标, +并通过 [Kubernetes API](/zh/docs/concepts/overview/kubernetes-api/) 公开这些指标, +使用 [APIService](/zh/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/) 添加代表指标读数的新资源。 + +要了解如何部署 Metrics Server,请参阅 +[metrics-server 文档](https://github.com/kubernetes-sigs/metrics-server#deployment)。 -## 运行 php-apache 服务器并暴露服务 +## 运行 php-apache 服务器并暴露服务 {#run-and-expose-php-apache-server} -为了演示 Horizontal Pod Autoscaler,我们将使用一个基于 php-apache 镜像的 -定制 Docker 镜像。Dockerfile 内容如下: +为了演示 HorizontalPodAutoscaler,你将首先制作一个自定义容器镜像, +该镜像使用来自 Docker Hub 的 `php-apache` 镜像作为其起点。 +`Dockerfile` 已经为你准备好了,内容如下: -``` +```dockerfile FROM php:5-apache COPY index.php /var/www/html/index.php RUN chmod a+rx index.php ``` -该文件定义了一个 index.php 页面来执行一些 CPU 密集型计算: +代码定义了一个简单的 `index.php` 页面,该页面执行一些 CPU 密集型计算, +以模拟集群中的负载。 -``` +```php }} +using the following manifest: --> -首先,我们使用下面的配置启动一个 Deployment 来运行这个镜像并暴露一个服务: +制作完该容器镜像后,使用你制作的镜像启动运行一个容器的 Deployment, +并使用以下清单将其公开为{{< glossary_tooltip term_id="service" text="服务" >}}: {{< codenew file="application/php-apache.yaml" >}} -运行下面的命令: +为此,运行下面的命令: ```shell kubectl apply -f https://k8s.io/examples/application/php-apache.yaml @@ -128,28 +153,46 @@ service/php-apache created ``` -## 创建 Horizontal Pod Autoscaler {#create-horizontal-pod-autoscaler} +## 创建 HorizontalPodAutoscaler {#create-horizontal-pod-autoscaler} -现在,php-apache 服务器已经运行,我们将通过 -[kubectl autoscale](/docs/reference/generated/kubectl/kubectl-commands#autoscale) -命令创建 Horizontal Pod Autoscaler。 -以下命令将创建一个 Horizontal Pod Autoscaler 用于控制我们上一步骤中创建的 -Deployment,使 Pod 的副本数量维持在 1 到 10 之间。 -大致来说,HPA 将(通过 Deployment)增加或者减少 Pod 副本的数量以保持所有 Pod -的平均 CPU 利用率在 50% 左右。由于每个 Pod 请求 200 毫核的 CPU,这意味着平均 -CPU 用量为 100 毫核。 -算法的详情请参阅[相关文档](/zh/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details)。 +现在服务器正在运行,使用 `kubectl` 创建自动扩缩器。 +[`kubectl autoscale`](/zh/docs/reference/generated/kubectl/kubectl-commands#autoscale) 子命令是 `kubectl` 的一部分, +可以帮助你执行此操作。 + +你将很快运行一个创建 HorizontalPodAutoscaler 的命令, +该 HorizontalPodAutoscaler 维护由你在这些说明的第一步中创建的 php-apache Deployment 控制的 Pod 存在 1 到 10 个副本。 + +粗略地说,HPA {{}}将增加和减少副本的数量 +(通过更新 Deployment)以保持所有 Pod 的平均 CPU 利用率为 50%。 +Deployment 然后更新 ReplicaSet —— 这是所有 Deployment 在 Kubernetes 中工作方式的一部分 —— +然后 ReplicaSet 根据其 `.spec` 的更改添加或删除 Pod。 + +由于每个 Pod 通过 `kubectl run` 请求 200 milli-cores,这意味着平均 CPU 使用率为 100 milli-cores。 +有关算法的更多详细信息, +请参阅[算法详细信息](/zh/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details)。 + + + +创建 HorizontalPodAutoscaler: ```shell kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 @@ -160,14 +203,19 @@ horizontalpodautoscaler.autoscaling/php-apache autoscaled ``` -我们可以通过以下命令查看 Autoscaler 的状态: +你可以通过运行以下命令检查新制作的 HorizontalPodAutoscaler 的当前状态: + ```shell +# 你可以使用 “hap” 或 “horizontalpodautoscaler”;任何一个名字都可以。 kubectl get hpa ``` + +输出类似于: + ``` NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache/scale 0% / 50% 1 10 1 18s @@ -175,42 +223,71 @@ php-apache Deployment/php-apache/scale 0% / 50% 1 10 1 ``` -请注意当前的 CPU 利用率是 0%,这是由于我们尚未发送任何请求到服务器 -(``CURRENT`` 列显示了相应 Deployment 所控制的所有 Pod 的平均 CPU 利用率)。 +(如果你看到其他具有不同名称的 HorizontalPodAutoscalers,这意味着它们已经存在,这通常不是问题)。 +请注意当前的 CPU 利用率是 0%,这是由于我们尚未发送任何请求到服务器 +(``TARGET`` 列显示了相应 Deployment 所控制的所有 Pod 的平均 CPU 利用率)。 -Now, we will see how the autoscaler reacts to increased load. -We will start a container, and send an infinite loop of queries to the php-apache service (please run it in a different terminal): + ## 增加负载 {#increase-load} -现在,我们将看到 Autoscaler 如何对增加负载作出反应。 -我们将启动一个容器,并通过一个循环向 php-apache 服务器发送无限的查询请求 -(请在另一个终端中运行以下命令): +接下来,看看自动扩缩器如何对增加的负载做出反应。 +为此,你将启动一个不同的 Pod 作为客户端。 +客户端 Pod 中的容器在无限循环中运行,向 php-apache 服务发送查询。 + ```shell -kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done" +# 在单独的终端中运行它 +# 以便负载生成继续,你可以继续执行其余步骤 +kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done" ``` -一分钟时间左右之后,通过以下命令,我们可以看到 CPU 负载升高了: +Now run: +Within a minute or so, you should see the higher CPU load; for example: +--> +现在执行: + + ```shell -kubectl get hpa +# 准备好后按 Ctrl+C 结束观察 +kubectl get hpa php-apache --watch ``` +一分钟时间左右之后,通过以下命令,我们可以看到 CPU 负载升高了;例如: + ``` NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache/scale 305% / 50% 1 10 1 3m ``` + +然后,更多的副本被创建。例如: + +``` +NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE +php-apache Deployment/php-apache/scale 305% / 50% 1 10 7 3m +``` + +你应该会看到与 HorizontalPodAutoscaler 中的数字与副本数匹配 + ``` NAME READY UP-TO-DATE AVAILABLE AGE php-apache 7/7 7 7 19m @@ -238,32 +320,40 @@ will differ from this example. {{< /note >}} -## 停止负载 +## 停止产生负载 {#stop-load} -我们将通过停止负载来结束我们的示例。 +要完成该示例,请停止发送负载。 -在我们创建 busybox 容器的终端中,输入` + C` 来终止负载的产生。 +在我们创建 `busybox` 容器的终端中,输入 ` + C` 来终止负载的产生。 -然后我们可以再次检查负载状态(等待几分钟时间): +然后验证结果状态(大约一分钟后): + ```shell -kubectl get hpa +# 准备好后按 Ctrl+C 结束观察 +kubectl get hpa php-apache --watch ``` + +输出类似于: + ``` NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache/scale 0% / 50% 1 10 1 11m ``` + +Deployment 也显示它已经缩小了: + ```shell kubectl get deployment php-apache ``` @@ -274,16 +364,14 @@ php-apache 1/1 1 1 27m ``` -这时,CPU 利用率已经降到 0,所以 HPA 将自动缩减副本数量至 1。 +一旦 CPU 利用率降至 0,HPA 会自动将副本数缩减为 1。 -{{< note >}} 自动扩缩完成副本数量的改变可能需要几分钟的时间。 -{{< /note >}} @@ -291,17 +379,17 @@ Autoscaling the replicas may take a few minutes. ## Autoscaling on multiple metrics and custom metrics You can introduce additional metrics to use when autoscaling the `php-apache` Deployment -by making use of the `autoscaling/v2beta2` API version. +by making use of the `autoscaling/v2` API version. --> ## 基于多项度量指标和自定义度量指标自动扩缩 {#autoscaling-on-multiple-metrics-and-custom-metrics} -利用 `autoscaling/v2beta2` API 版本,你可以在自动扩缩 php-apache 这个 +利用 `autoscaling/v2` API 版本,你可以在自动扩缩 php-apache 这个 Deployment 时使用其他度量指标。 -首先,将 HorizontalPodAutoscaler 的 YAML 文件改为 `autoscaling/v2beta2` 格式: +首先,将 HorizontalPodAutoscaler 的 YAML 文件改为 `autoscaling/v2` 格式: ```shell kubectl get hpa php-apache -o yaml > /tmp/hpa-v2.yaml @@ -313,7 +401,7 @@ Open the `/tmp/hpa-v2.yaml` file in an editor, and you should see YAML which loo 在编辑器中打开 `/tmp/hpa-v2.yaml`: ```yaml -apiVersion: autoscaling/v2beta2 +apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: php-apache @@ -376,13 +464,13 @@ advanced cluster monitoring setup. 这些度量指标可能具有特定于集群的名称,并且需要更高级的集群监控设置。 -第一种可选的度量指标类型是 Pod 度量指标。这些指标从某一方面描述了 Pod, +第一种可选的度量指标类型是 **Pod 度量指标**。这些指标从某一方面描述了 Pod, 在不同 Pod 之间进行平均,并通过与一个目标值比对来确定副本的数量。 -它们的工作方式与资源度量指标非常相像,只是它们仅支持 `target` 类型为 `AverageValue`。 +它们的工作方式与资源度量指标非常相像,只是它们 **仅** 支持 `target` 类型为 `AverageValue`。 -第二种可选的度量指标类型是对象(Object)度量指标。这些度量指标用于描述 +第二种可选的度量指标类型是对象 **(Object)度量指标**。这些度量指标用于描述 在相同名字空间中的别的对象,而非 Pods。 请注意这些度量指标不一定来自某对象,它们仅用于描述这些对象。 对象度量指标支持的 `target` 类型包括 `Value` 和 `AverageValue`。 @@ -447,7 +535,7 @@ you could update the definition above using `kubectl edit` to look like this: 将上述 Horizontal Pod Autoscaler 的定义更改为: ```yaml -apiVersion: autoscaling/v2beta1 +apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: php-apache @@ -463,7 +551,7 @@ spec: resource: name: cpu target: - type: AverageUtilization + type: Utilization averageUtilization: 50 - type: Pods pods: @@ -477,11 +565,11 @@ spec: metric: name: requests-per-second describedObject: - apiVersion: networking.k8s.io/v1beta1 + apiVersion: networking.k8s.io/v1 kind: Ingress name: main-route target: - kind: Value + type: Value value: 10k status: observedGeneration: 1 @@ -500,7 +588,7 @@ status: metric: name: requests-per-second describedObject: - apiVersion: networking.k8s.io/v1beta1 + apiVersion: networking.k8s.io/v1 kind: Ingress name: main-route current: @@ -537,8 +625,8 @@ GET 请求执行: type: Object object: metric: - name: `http_requests` - selector: `verb=GET` + name: http_requests + selector: {matchLabels: {verb: GET}} ``` ## 附录:Horizontal Pod Autoscaler 状态条件 -使用 `autoscaling/v2beta2` 格式的 HorizontalPodAutoscaler 时,你将可以看到 +使用 `autoscaling/v2` 格式的 HorizontalPodAutoscaler 时,你将可以看到 Kubernetes 为 HorizongtalPodAutoscaler 设置的状态条件(Status Conditions)。 这些状态条件可以显示当前 HorizontalPodAutoscaler 是否能够执行扩缩以及是否受到一定的限制。 @@ -662,7 +750,7 @@ Events: ``` -## 附录:量纲 {#appendix-quantities} +## 量纲 {#quantities} HorizontalPodAutoscaler 和 度量指标 API 中的所有的度量指标使用 Kubernetes 中称为 {{< glossary_tooltip term_id="quantity" text="量纲(Quantity)">}} @@ -701,24 +789,24 @@ HorizontalPodAutoscaler 和 度量指标 API 中的所有的度量指标使用 K 这意味着你可能会看到你的度量指标在 `1` 和 `1500m` (也就是在十进制记数法中的 `1` 和 `1.5`)之间波动。 -## 附录:其他可能的情况 {#appendix-other-possible-scenarios} +## 其他可能的情况 {#other-possible-scenarios} ### 以声明式方式创建 Autoscaler {#creating-the-autoscaler-declaratively} -除了使用 `kubectl autoscale` 命令,也可以文件创建 HorizontalPodAutoscaler: +除了使用 `kubectl autoscale` 命令,也可以使用以下清单以声明方式创建 HorizontalPodAutoscaler: {{< codenew file="application/hpa/php-apache.yaml" >}} 使用如下命令创建 autoscaler: