Integrating ACP Monitoring with Prometheus Plugin

本指南介绍如何配置与 ACP Monitoring with Prometheus Plugin 的集成,以实现基于 Prometheus 指标的应用自动伸缩。

Prerequisites

使用此功能前,请确保:

  • 安装 ACP Monitoring with Prometheus Plugin
  • 获取当前 Kubernetes 集群的 Prometheus 端点 URL 和 secretName:
    PrometheusEndpoint=$(kubectl get feature monitoring -o jsonpath='{.spec.accessInfo.database.address}')
  • 获取当前 Kubernetes 集群的 Prometheus secret:
    PrometheusSecret=$(kubectl get feature monitoring -o jsonpath='{.spec.accessInfo.database.basicAuth.secretName}')
  • <your-namespace> 命名空间中创建名为 <your-deployment> 的 Deployment。

Procedure

  • keda 命名空间中配置 Prometheus 认证 Secret。

将 Secret 从 cpaas-system 复制到 keda 命名空间的步骤

# 获取 Prometheus 认证信息
PrometheusUsername=$(kubectl get secret $PrometheusSecret -n cpaas-system -o jsonpath='{.data.username}' | base64 -d)
PrometheusPassword=$(kubectl get secret $PrometheusSecret -n cpaas-system -o jsonpath='{.data.password}' | base64 -d)

# 在 keda 命名空间创建 Secret
kubectl create secret generic $PrometheusSecret \
  -n keda \
  --from-literal=username=$PrometheusUsername \
  --from-literal=password=$PrometheusPassword
  • 使用 ClusterTriggerAuthentication 配置 KEDA 访问 Prometheus 的认证。

要配置 KEDA 访问 Prometheus 的认证凭据,需要定义一个 ClusterTriggerAuthentication 资源,引用包含用户名和密码的 Secret。以下是示例配置:

kubectl apply -f - <<EOF
apiVersion: keda.sh/v1alpha1
kind: ClusterTriggerAuthentication
metadata:
  name: cluster-prometheus-auth
spec:
  secretTargetRef:
    - key: username
      name: $PrometheusSecret
      parameter: username
    - key: password
      name: $PrometheusSecret
      parameter: password
EOF
  • 使用 ScaledObject 配置基于 Prometheus 指标的 Kubernetes Deployment 自动伸缩。

要基于 Prometheus 指标对 Kubernetes Deployment 进行伸缩,定义一个引用已配置 ClusterTriggerAuthentication 的 ScaledObject 资源。以下是示例配置:

kubectl apply -f - <<EOF
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: prometheus-scaledobject
  namespace: <your-namespace>
spec:
  cooldownPeriod: 300          # 缩容前等待时间(秒)
  maxReplicaCount: 5           # 最大副本数
  minReplicaCount: 1           # 最小副本数(注意:HPA 可能强制最小为 1)
  pollingInterval: 30          # 轮询 Prometheus 指标的间隔(秒)
  scaleTargetRef:
    name: <your-deployment>    # 目标 Kubernetes Deployment 名称
  triggers:
    - authenticationRef:
        kind: ClusterTriggerAuthentication
        name: cluster-prometheus-auth  # 引用 ClusterTriggerAuthentication
      metadata:
        authModes: basic       # 认证方式(此处为 basic auth)
        query: sum(container_memory_working_set_bytes{container!="POD",container!="",namespace="<your-namespace>",pod=~"<your-deployment-name>.*"})
        queryParameters: timeout=10s  # 可选查询参数
        serverAddress: $PrometheusEndpoint
        threshold: "1024000"   # 伸缩阈值
        unsafeSsl: "true"      # 跳过 SSL 证书验证(生产环境不推荐)
      type: prometheus         # 触发器类型
EOF

Verification

要验证 ScaledObject 是否已对 Deployment 进行伸缩,可以检查目标 Deployment 的副本数:

kubectl get deployment <your-deployment> -n <your-namespace>

或者使用以下命令查看 Pod 数量:

kubectl get pods -n <your-namespace> -l <your-deployment-label-key>=<your-deployment-label-value>

副本数应根据 ScaledObject 中指定的指标进行增减。 如果部署伸缩正常,您应看到 Pod 数量已变更为 maxReplicaCount 的值。

Other KEDA scalers

KEDA scalers 既可以检测部署是否应激活或停用,也可以为特定事件源提供自定义指标。

KEDA 支持多种额外的 scalers。详情请参见官方文档:KEDA Scalers