1

我在 GKE 上使用自动驾驶仪。我创建了一些基于日志的指标,我想用它们来扩展 pod。

首先 - 我不确定这是否是个好主意 - 指标只是数据库中要处理的记录数......我感觉使用日志来扩展应用程序可能会带来一些奇怪的无限循环或其他东西......

无论如何 - 我已经尝试logging.googleapis.com|user|celery-person-count作为外部指标输入并得到HPA cannot read metric value. 已安装 Stackdriver 适配器,但也不太清楚如何使用它。

4

3 回答 3

2

GKE Autopilot 集群启用了Workload Identity以使用其他 GCP 服务,包括 Cloud Monitoring。

您需要按照此处的步骤在 Autopilot 集群上部署自定义指标适配器。

kubectl create clusterrolebinding cluster-admin-binding \
    --clusterrole cluster-admin --user "$(gcloud config get-value account)"

kubectl create namespace custom-metrics

kubectl create serviceaccount --namespace custom-metrics \
custom-metrics-stackdriver-adapter

gcloud iam service-accounts create GSA_NAME

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member "serviceAccount:GSA_NAME@PROJECT_ID.iam.gserviceaccount.com" \
    --role "roles/monitoring.viewer"

gcloud iam service-accounts add-iam-policy-binding \
  --role roles/iam.workloadIdentityUser \
  --member "serviceAccount:PROJECT_ID.svc.id.goog[custom-metrics/custom-metrics-stackdriver-adapter]" \
  GSA_NAME@PROJECT_ID.iam.gserviceaccount.com

kubectl annotate serviceaccount \
  --namespace custom-metrics custom-metrics-stackdriver-adapter \
  iam.gke.io/gcp-service-account=GSA_NAME@PROJECT_ID.iam.gserviceaccount.com

kubectl apply -f manifests/adapter_new_resource_model.yaml

鉴于您已经部署了适配器,您需要先删除部署,尽管您可能只能从gcloud iam ...

您需要将 GSA_NAME 替换为您选择的名称,并将 PROJECT_ID 替换为您的 Google Cloud 项目 ID。

于 2021-10-19T10:16:26.730 回答
0

自定义指标

GitHub链接

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

使用指标部署应用程序

git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples.git
cd kubernetes-engine-samples/custom-metrics-autoscaling/direct-to-sd

HPA 示例

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: custom-metric-sd
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: custom-metric-sd
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Pods
    pods:
      metric:
        name: custom-metric
      target:
        type: AverageValue
        averageValue: 20

您可以查看此链接以获取更多信息。

如果您的工作负载在 VM 上运行或不在 K8s 上运行,则使用外部指标而不是自定义指标。

请参阅有关自动缩放工作负载的自定义和外部指标的文档

于 2021-10-19T04:15:32.303 回答
0

是的,水平 Pod 自动缩放可以使用度量值来完成。如果您的应用程序在 Kubernetes 中运行,那么您必须使用自定义指标进行自动缩放,如果您的应用程序未在 Kubernetes 中运行,那么您必须使用外部指标进行自动缩放。

可以为以下任何一项选择自定义指标:

  • 特定节点、Pod 或任何类型的任何 Kubernetes 对象,包括 CustomResourceDefinition (CRD)。

  • Deployment 中所有 Pod 报告的指标的平均值。

    有关使用自定义指标进行水平 Pod 自动缩放的详细说明,请参阅此文档

于 2021-10-19T04:16:26.460 回答