Prometheus Adapter完全指南:如何让Kubernetes HPA基于应用指标自动扩缩容
Prometheus Adapter完全指南如何让Kubernetes HPA基于应用指标自动扩缩容【免费下载链接】prometheus-adapterAn implementation of the custom.metrics.k8s.io API using Prometheus项目地址: https://gitcode.com/gh_mirrors/pr/prometheus-adapterPrometheus Adapter是实现custom.metrics.k8s.io API的关键组件它能帮助Kubernetes集群中的Horizontal Pod AutoscalerHPA基于Prometheus收集的应用指标实现自动扩缩容。本文将详细介绍如何部署、配置Prometheus Adapter并实现基于自定义指标的HPA自动扩缩容。为什么需要Prometheus Adapter在Kubernetes中默认的HPA仅支持CPU和内存等基础资源指标。但现代应用通常需要基于业务指标如每秒请求数、队列深度、错误率等进行扩缩容。Prometheus Adapter作为自定义指标API的实现能够将Prometheus收集的丰富指标暴露给Kubernetes API从而让HPA实现基于任意应用指标的自动扩缩容。快速部署Prometheus Adapter的步骤前提条件Kubernetes集群版本1.8推荐1.9已部署Prometheus建议使用Prometheus Operator已启用Kubernetes API聚合层已部署metrics-server克隆代码仓库git clone https://gitcode.com/gh_mirrors/pr/prometheus-adapter cd prometheus-adapter部署Prometheus Adapter项目提供了完整的部署清单位于deploy/manifests/目录下包含了部署所需的所有Kubernetes资源kubectl apply -f deploy/manifests/注意如果您的集群不是x86_64架构需要修改Deployment中的镜像字段选择对应架构的镜像。官方多架构镜像地址为registry.k8s.io/prometheus-adapter/prometheus-adapter:${VERSION}。配置Prometheus Adapter核心参数Prometheus Adapter的配置通过ConfigMap进行管理默认配置文件为deploy/manifests/config-map.yaml。关键配置项包括规则配置rules规则定义了如何从Prometheus指标转换为Kubernetes自定义指标以下是一个基础配置示例rules: - seriesQuery: {namespace!, __name__!~^container_.*} resources: template: .Resource name: matches: ^(.*)_total as: metricsQuery: | sum by (.GroupBy) ( irate( .Series{.LabelMatchers}[1m] ) )这个配置将所有非容器指标__name__!~^container_.*中以_total结尾的指标转换为速率指标使用irate函数计算1分钟内的速率。与Prometheus通信配置确保Adapter能够正确访问Prometheus配置项包括prometheus: url: http://prometheus-server.monitoring.svc port: 80 path: 创建基于自定义指标的HPA准备示例应用首先部署一个暴露指标的示例应用部署清单如下# sample-app.deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: sample-app labels: app: sample-app spec: replicas: 1 selector: matchLabels: app: sample-app template: metadata: labels: app: sample-app spec: containers: - image: luxas/autoscale-demo:v0.1.2 name: metrics-provider ports: - name: http containerPort: 8080# sample-app.service.yaml apiVersion: v1 kind: Service metadata: labels: app: sample-app name: sample-app spec: ports: - name: http port: 80 protocol: TCP targetPort: 8080 selector: app: sample-app type: ClusterIP部署应用kubectl apply -f sample-app.deploy.yaml kubectl apply -f sample-app.service.yaml创建ServiceMonitor监控应用使用Prometheus Operator的ServiceMonitor资源来监控示例应用# sample-app.monitor.yaml kind: ServiceMonitor apiVersion: monitoring.coreos.com/v1 metadata: name: sample-app labels: app: sample-app spec: selector: matchLabels: app: sample-app endpoints: - port: http部署ServiceMonitorkubectl apply -f sample-app.monitor.yaml创建基于自定义指标的HPA创建HPA资源基于http_requests指标进行扩缩容# sample-app.hpa.yaml kind: HorizontalPodAutoscaler apiVersion: autoscaling/v2 metadata: name: sample-app spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: sample-app minReplicas: 1 maxReplicas: 10 metrics: - type: Pods pods: metric: name: http_requests target: type: Value averageValue: 500m部署HPAkubectl apply -f sample-app.hpa.yaml这个HPA配置表示当平均每个Pod的http_requests指标达到500m即0.5个请求/秒时开始自动扩容最多扩容到10个副本。验证与测试HPA自动扩缩容检查HPA状态部署HPA后可以通过以下命令检查其状态kubectl describe hpa sample-app正常情况下应该能看到HPA成功获取到指标类似如下输出Metrics: ( current / target ) http_requests on pods: 0 / 500m Min replicas: 1 Max replicas: 10 Deployment pods: 1 current / 1 desired生成流量测试扩容通过curl命令向示例应用发送请求生成流量while true; do curl http://$(kubectl get service sample-app -o jsonpath{ .spec.clusterIP })/metrics; sleep 0.5; done持续发送请求一段时间后再次检查HPA状态应该能看到Pod数量开始增加Metrics: ( current / target ) http_requests on pods: 1200m / 500m Min replicas: 1 Max replicas: 10 Deployment pods: 3 current / 3 desired停止流量测试缩容停止发送请求后等待几分钟HPA会自动将Pod数量缩容到初始值Metrics: ( current / target ) http_requests on pods: 0 / 500m Min replicas: 1 Max replicas: 10 Deployment pods: 1 current / 1 desired高级配置与最佳实践多指标扩缩容HPA支持同时基于多个指标进行扩缩容例如同时考虑CPU使用率和自定义指标metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 80 - type: Pods pods: metric: name: http_requests target: type: Value averageValue: 500m外部指标配置Prometheus Adapter还支持外部指标External Metrics配置方法可参考docs/externalmetrics.md。外部指标适用于需要基于集群外系统指标进行扩缩容的场景。配置优化建议指标查询优化确保Prometheus查询语句高效避免使用过于复杂的聚合操作。合理设置指标窗口使用irate函数时窗口大小建议设置为Prometheus scrape interval的2-3倍。避免抖动通过设置stabilizationWindowSeconds参数避免HPA频繁扩缩容。定期检查配置随着应用变化定期检查Prometheus Adapter配置确保指标转换规则仍然适用。常见问题解决自定义指标不显示如果通过kubectl get --raw /apis/custom.metrics.k8s.io/v1beta2命令看不到预期的指标可能原因Prometheus未正确收集到指标检查Prometheus UI确认指标存在。Adapter配置错误检查config-map.yaml中的规则配置确保指标匹配正确。标签不匹配确保Prometheus指标中的标签如namespace、pod与Adapter配置中的标签匹配。HPA无法获取指标如果HPA事件中显示无法获取指标可能原因API服务未注册检查APIService资源是否正确部署可通过kubectl get apiservice v1beta2.custom.metrics.k8s.io命令查看。Adapter未正常运行检查Prometheus Adapter的Pod日志查看是否有错误信息。权限问题确保Adapter的Service Account具有足够的权限访问Prometheus和Kubernetes API。总结通过Prometheus AdapterKubernetes HPA可以突破基础资源指标的限制基于任意Prometheus收集的应用指标实现自动扩缩容。本文详细介绍了Prometheus Adapter的部署、配置和使用方法包括如何创建基于自定义指标的HPA并提供了最佳实践和常见问题解决方法。要深入了解Prometheus Adapter的配置选项可以参考docs/config.md和docs/config-walkthrough.md。通过合理配置和使用Prometheus Adapter可以让Kubernetes应用更加智能、高效地应对流量变化。【免费下载链接】prometheus-adapterAn implementation of the custom.metrics.k8s.io API using Prometheus项目地址: https://gitcode.com/gh_mirrors/pr/prometheus-adapter创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考