Kubernetes Horizontal Pod Autoscaler 进阶:自定义指标伸缩

在 Kubernetes 中,Horizontal Pod Autoscaler (HPA) 允许自动调整 Pod 副本数以适应负载变化。默认情况下,HPA 基于 CPU 或内存使用率伸缩,但在实际应用中,可能需要基于自定义指标(如请求速率、队列长度或应用特定指标)进行伸缩。本指南将逐步解释如何配置 HPA 使用自定义指标,确保高效可靠的自动伸缩。以下内容基于 Kubernetes 1.18+ 版本,假设您已安装 Metrics Server 并熟悉基本 HPA 操作。

步骤 1: 理解自定义指标原理

HPA 通过 Kubernetes Metrics API 获取指标数据。自定义指标需要外部监控系统(如 Prometheus)提供数据,并通过适配器(如 Prometheus Adapter)暴露给 Metrics API。伸缩决策基于目标值与当前值的比例计算目标副本数: $$ \text{desiredReplicas} = \lceil \text{currentReplicas} \times \frac{\text{currentMetricValue}}{\text{targetMetricValue}} \rceil $$ 其中:

  • $\text{currentMetricValue}$ 是当前指标值(如每秒请求数)。
  • $\text{targetMetricValue}$ 是您设定的目标值(如 100 RPS)。
  • $\text{desiredReplicas}$ 必须介于 minReplicasmaxReplicas 之间。
步骤 2: 设置监控系统和适配器

首先,部署监控工具和适配器,将自定义指标暴露给 Kubernetes API。

  • 安装 Prometheus:用于收集应用指标(如通过应用暴露的 /metrics 端点)。
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm install prometheus prometheus-community/prometheus
    

  • 安装 Prometheus Adapter:将 Prometheus 指标转换为 Kubernetes Metrics API 格式。
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm install prometheus-adapter prometheus-community/prometheus-adapter
    

  • 配置适配器规则:创建 adapter-config.yaml 文件,定义如何映射 Prometheus 指标到 Kubernetes。例如,映射一个名为 http_requests_per_second 的指标:
    rules:
      - seriesQuery: 'http_requests_total{namespace!="",pod!=""}'
        resources:
          overrides:
            namespace: {resource: "namespace"}
            pod: {resource: "pod"}
        name:
          matches: "^(.*)_total"
          as: "${1}_per_second"
        metricsQuery: 'sum(rate(http_requests_total{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)'
    

    应用配置:
    kubectl apply -f adapter-config.yaml
    

步骤 3: 创建 HPA 资源使用自定义指标

定义 HPA 资源,指定自定义指标作为伸缩依据。以下是一个完整 YAML 示例(custom-hpa.yaml),假设您的应用名为 my-app,目标为每秒 100 个请求。

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Pods
      pods:
        metric:
          name: http_requests_per_second  # 自定义指标名称,需与适配器配置匹配
        target:
          type: AverageValue
          averageValue: 100  # 目标值,表示每个 Pod 平均处理 100 RPS

应用 HPA:

kubectl apply -f custom-hpa.yaml

步骤 4: 验证和监控伸缩行为
  • 检查指标可用性:确保 Metrics API 暴露了自定义指标。
    kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests_per_second"
    

  • 测试伸缩:模拟负载增加(如使用 kubectl run 或压测工具),观察 HPA 调整副本数。
    kubectl get hpa my-app-hpa -w  # 实时监控 HPA 状态
    

  • 监控日志:查看 Prometheus 和适配器日志,确保指标数据正确传输。
    kubectl logs -l app=prometheus-adapter -f
    

注意事项和最佳实践
  • 指标选择:自定义指标应稳定且可预测(如 $ \text{RPS} $ 或队列长度)。避免高频波动指标,以免导致频繁伸缩。
  • 安全边界:设置合理的 minReplicasmaxReplicas(例如,$ \text{min} \geq 2 $ 以确保高可用)。
  • 性能影响:监控伸缩延迟;HPA 默认每 15 秒评估一次,可通过 --horizontal-pod-autoscaler-sync-period 调整。
  • 故障排查
    • 如果指标未显示,检查 Prometheus 查询和适配器配置。
    • 使用 kubectl describe hpa my-app-hpa 查看事件日志。
  • 进阶优化:结合多个指标(如 CPU + 自定义指标),在 metrics 字段添加多个条目。

通过以上步骤,您可以实现基于业务需求的弹性伸缩。确保在生产环境测试,并根据应用特性调整目标值。更多细节参考 Kubernetes 官方文档

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐