[英]Google cloud GKE horizontal pod autoscaling based on Kubernetes metrics
I want to use the pod network received bytes count standard kubernetes metrics on HPA .我想在 HPA 上使用 pod 网络收到的字节数标准 kubernetes 指标。 Using following yaml to accomplish that but getting errors like unable to fetch metrics from custom metrics API: no custom metrics API (custom.metrics.k8s.io) registered使用以下 yaml 来完成此操作,但出现无法从自定义指标 API 获取指标之类的错误:未注册自定义指标 API (custom.metrics.k8s.io)
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: xxxx-hoa
namespace: xxxxx
spec:
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: xxxx-xxx
minReplicas: 2
maxReplicas: 6
metrics:
- type: Pods
pods:
metricName: received_bytes_count
targetAverageValue: 20k
If anybody had an experience with same kind of metrics usage that would be greatly helpful如果有人对相同类型的指标使用有经验,那将非常有帮助
autoscaling/v1 is an API in order to autoscale based only on CPU utilization. autoscaling/v1 是一个 API,用于仅根据 CPU 利用率进行自动缩放。 So, in order to autoscale based on other metrics you should use autoscaling/v2beta2.因此,为了根据其他指标进行自动缩放,您应该使用 autoscaling/v2beta2。 I recommend you to read this doc to check API versions.我建议您阅读此文档以检查 API 版本。
Solution解决方案
To make it work, you need to deploy Stackdriver Custom Metrics Adapter .要使其工作,您需要部署Stackdriver Custom Metrics Adapter 。 Below commands to deploy it.下面的命令来部署它。
$ kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole cluster-admin --user "$(gcloud config get-value account)"
$ kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/production/adapter_new_resource_model.yaml
Later you need to use proper Custom Metric
, in your case it should be kubernetes.io|pod|network|received_bytes_count
稍后您需要使用适当的Custom Metric
,在您的情况下它应该是kubernetes.io|pod|network|received_bytes_count
Description描述
In Custom and external metrics for autoscaling workloads documentation you have information that you need to deploy StackDriver Adapter
before you will be able to get custom metrics.在用于自动缩放工作负载的自定义和外部指标文档中,您提供了部署StackDriver Adapter
所需的信息,然后才能获得自定义指标。
Before you can use custom metrics, you must enable Monitoring in your Google Cloud project and install the Stackdriver adapter on your cluster.在您可以使用自定义指标之前,您必须在您的 GCP 项目中启用监控并在您的集群上安装 Stackdriver 适配器。
Next step is to deploy your application (I've used Nginx deployment for test purpose) and create proper HPA.下一步是部署您的应用程序(我使用 Nginx 部署进行测试)并创建适当的 HPA。
In your HPA example, you have a few issues在您的 HPA 示例中,您遇到了一些问题
apiVersion: autoscaling/v2beta1 ## you can also use autoscaling/v2beta2 if you need more features, however for this scenario is ok
kind: HorizontalPodAutoscaler
metadata:
name: xxxx-hoa
namespace: xxxxx # HPA have namespace specified, deployment doesnt have
spec:
scaleTargetRef:
apiVersion: apps/v1beta1 # apiVersion: apps/v1beta1 is quite old. In Kubernetes 1.16+ it was changed to apps/v1
kind: Deployment
name: xxxx-xxx
minReplicas: 2
maxReplicas: 6
metrics:
- type: Pods
pods:
metricName: received_bytes_count # this metrics should be replaced with kubernetes.io|pod|network|received_bytes_count
targetAverageValue: 20k
In GKE you can choose between autoscaling/v2beta1
and autoscaling/v2beta2
.在 GKE 中,您可以在autoscaling/v2beta1
和autoscaling/v2beta2
autoscaling/v2beta1
之间进行选择。 Your case will work with both apiVersions
, however if you would decide to use autoscaling/v2beta2
you will need to change manifest syntax.您的案例适用于两个apiVersions
,但是如果您决定使用autoscaling/v2beta2
您将需要更改清单语法。
Why kubernetes.io/pod/network/received_bytes_count
?为什么是kubernetes.io/pod/network/received_bytes_count
? You are refering to Kubernetes metrics, and /pod/network/received_bytes_count
is provided in this docs .您指的是 Kubernetes 指标,此文档中提供了/pod/network/received_bytes_count
。
Why |
为什么|
instead of /
?而不是/
? If you will check Stackdriver documentation on Github you will find information.如果您在 Github 上查看Stackdriver 文档,您会找到相关信息。
Stackdriver metrics have a form of paths separated by "/" character, but Custom Metrics API forbids using "/" character. Stackdriver 指标具有以“/”字符分隔的路径形式,但自定义指标 API 禁止使用“/”字符。 When using Custom Metrics - Stackdriver Adapter either directly via Custom Metrics API or by specifying a custom metric in HPA, replace "/" character with "|".直接通过自定义指标 API 或通过在 HPA 中指定自定义指标使用自定义指标 - Stackdriver Adapter 时,请将“/”字符替换为“|”。 For example, to use custom.googleapis.com/my/custom/metric, specify custom.googleapis.com|my|custom|metric.例如,要使用 custom.googleapis.com/my/custom/metric,请指定 custom.googleapis.com|my|custom|metric。
Proper Configuration正确配置
for v2beta1对于 v2beta1
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: xxxx-hoa
spec:
scaleTargetRef:
apiVersion: apps/v1 # In your case should be apps/v1beta1 but my deployment was created with apps/v1 apiVersion
kind: Deployment
name: nginx
minReplicas: 2
maxReplicas: 6
metrics:
- type: Pods
pods:
metricName: "kubernetes.io|pod|network|received_bytes_count"
targetAverageValue: 20k
for v2beta2对于 v2beta2
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: xxxx-hoa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 2
maxReplicas: 6
metrics:
- type: Pods
pods:
metric:
name: "kubernetes.io|pod|network|received_bytes_count"
target:
type: AverageValue
averageValue: 20k
Tests Outputs测试输出
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True SucceededRescale the HPA controller was able to update the target scale to 2
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from pods metric kubernetes.io|pod|network|received_bytes_count
ScalingLimited True TooFewReplicas the desired replica count is more than the maximum replica count
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulRescale 8m18s horizontal-pod-autoscaler New size: 4; reason: pods metric kubernetes.io|pod|network|received_bytes_count above target
Normal SuccessfulRescale 8m9s horizontal-pod-autoscaler New size: 6; reason: pods metric kubernetes.io|pod|network|received_bytes_count above target
Normal SuccessfulRescale 17s horizontal-pod-autoscaler New size: 5; reason: All metrics below target
Normal SuccessfulRescale 9s (x2 over 8m55s) horizontal-pod-autoscaler New size: 2; reason: All metrics below target
Possible Issues with your current configuration您当前的配置可能存在的问题
In your HPA you have specified namespace but not in your target Deployment.在您的 HPA 中,您已指定命名空间,但未在目标部署中。 Both, HPA and deployment should have the same namespace. HPA 和部署都应该具有相同的命名空间。 With this mismatch configuration you can encounter issue below:使用这种不匹配的配置,您可能会遇到以下问题:
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale False FailedGetScale the HPA controller was unable to get the target's current scale: deployments/scale.apps "nginx" not found
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedGetScale 94s (x264 over 76m) horizontal-pod-autoscaler deployments/scale.apps "nginx" not found
In Kubernetes 1.16+, deployments are using apiVersion: apps/v1
, you won't be able to create deployment with apiVersion: apps/v1beta1
in Kubernets 1.16+在 Kubernetes 1.16+ 中,部署使用apiVersion: apps/v1
,您将无法在 Kubernets 1.16+ 中使用apiVersion: apps/v1
apiVersion: apps/v1beta1
创建部署
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.