游乐游手机版
首页/系统平台/文章详情

PrometheusOperator实现自定义指标监控的4个简单步骤

时间:2026-08-21 16:49
本文转载自Rancher Labs在过去的多篇文章中,我们曾用较大篇幅讨论 Kubernetes 监控。这是因为在管理 Kubernetes 集群时,集群状态、资源使用情况以及工作负载变化都非常迅速。因此,借助专业的监控工具持续追踪集群健康状态和资源指标,对于保障稳定运行至关重要。在 Rancher

本文转载自Rancher Labs

在过去的多篇文章中,我们曾用较大篇幅讨论 Kubernetes 监控。这是因为在管理 Kubernetes 集群时,集群状态、资源使用情况以及工作负载变化都非常迅速。因此,借助专业的监控工具持续追踪集群健康状态和资源指标,对于保障稳定运行至关重要。

在 Rancher 2.5 中,全新监控体系基于 Prometheus Operator 引入,可提供 Prometheus 及相关监控组件的原生 Kubernetes 部署与管理能力。借助 Prometheus Operator,你可以监控集群节点、Kubernetes 核心组件以及应用工作负载的运行状态和进程信息。同时,它还能基于 Prometheus 采集到的指标定义告警规则,并创建自定义 Grafana 仪表盘,方便对监控数据进行可视化展示。以下是关于新版监控组件的官方文档链接:

https://rancher.com/docs/rancher/v2.x/en/monitoring-alerting/v2.5/

新版监控方案同样集成了 prometheus-adapter,开发者可以结合自定义指标与 HPA(Horizontal Pod Autoscaler)对应用工作负载进行弹性扩缩容。

在本文中,我们将重点介绍如何使用 Prometheus Operator 抓取自定义指标,并基于这些指标实现更高级的 Kubernetes 工作负载管理。

安装Prometheus

在 Rancher 2.5 中安装 Prometheus 非常简单。你只需要进入 Cluster Explorer -> Apps,然后安装 rancher-monitoring 即可。

安装前,你需要了解以下几个默认设置:

  • prometheus-adapter会作为 chart 安装流程的一部分默认启用

  • ServiceMonitorNamespaceSelector 留空,这意味着 Prometheus 可以在所有命名空间中发现并采集 ServiceMonitors

安装完成后,我们就可以通过 Cluster Explorer 访问这些监控组件。

部署工作负载

接下来,我们部署一个可从应用层暴露自定义指标的示例工作负载。这个示例应用已经通过 Prometheus 的 client_golang 库完成埋点,并在/metric端点上暴露若干自定义监控指标。

它包含以下两个指标:

  • http_requests_total

  • http_request_duration_seconds

下面的 manifest 会部署该工作负载、对应的 Service,以及用于访问该工作负载的 Ingress:

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/name: prometheus-example-app
name: prometheus-example-app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: prometheus-example-app
template:
metadata:
labels:
app.kubernetes.io/name: prometheus-example-app
spec:
containers:
- name: prometheus-example-app
image: gmehta3/demo-app:metrics
ports:
- name: web
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: prometheus-example-app
labels:
app.kubernetes.io/name: prometheus-example-app
spec:
selector:
app.kubernetes.io/name: prometheus-example-app
ports:
- protocol: TCP
port: 8080
targetPort: 8080
name: web
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: prometheus-example-app
spec:
rules:
- host: hpa.demo
https:
paths:
- path: /
backend:
serviceName: prometheus-example-app
servicePort: 8080

部署ServiceMonitor

ServiceMonitor 是一个自定义资源定义(CRD),用于以声明式方式定义 Prometheus 应如何监控一组动态变化的服务。

你可以通过以下链接查看完整的 ServiceMonitor 规范说明:

https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#servicemonitor

现在,我们来部署 ServiceMonitor。Prometheus 将使用它来发现并采集组成 prometheus-example-app 这个 Kubernetes Service 的 Pod 指标。

kind: ServiceMonitor
metadata:
name: prometheus-example-app
spec:
selector:
matchLabels:
app.kubernetes.io/name: prometheus-example-app
endpoints:
- port: web

如你所见,部署完成后,用户已经可以在 Rancher 监控界面中查看这个 ServiceMonitor。

稍等片刻后,这个新的 ServiceMonitor 以及与该服务关联的 Pod,应该就会出现在 Prometheus 的服务发现列表中。

随后,我们也能够在 Prometheus 中查询并看到这些指标数据。

部署Grafana仪表盘

在 Rancher 2.5 中,监控功能支持用户将 Grafana 仪表盘以 ConfigMap 的形式存储在cattle-dashboards命名空间中。

用户或集群管理员可以继续在该命名空间中添加更多仪表盘,从而扩展 Grafana 自定义监控面板的展示能力。

Dashboard ConfigMap Example
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-example-app-dashboard
namespace: cattle-dashboards
labels:
grafana_dashboard: "1"
data:
prometheus-example-app.json: |
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"links": [],
"panels": [
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": null,
"fieldConfig": {
"defaults": {
"custom": {}
},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 0
},
"hiddenSeries": false,
"id": 2,
"legend": {
"a vg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"percentage": false,
"pluginVersion": "7.1.5",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "rate(http_requests_total{code="200",service="prometheus-example-app"}[5m])",
"instant": false,
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "http_requests_total_200",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": null,
"description": "",
"fieldConfig": {
"defaults": {
"custom": {}
},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 9
},
"hiddenSeries": false,
"id": 4,
"legend": {
"a vg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"percentage": false,
"pluginVersion": "7.1.5",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "rate(http_requests_total{code!="200",service="prometheus-example-app"}[5m])",
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "http_requests_total_not_200",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"schemaVersion": 26,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-15m",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
]
},
"timezone": "",
"title": "prometheus example app",
"version": 1
}

完成后,用户应该就可以在 Grafana 中访问 prometheus example app 的监控仪表盘了。

自定义指标的HPA

这一部分默认你已经将prometheus-adapter作为监控组件的一部分安装完成。事实上,在默认配置下,监控安装程序就会自动部署 prometheus-adapter。

现在,用户可以创建如下所示的 HPA 规格:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: prometheus-example-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: prometheus-example-app
minReplicas: 1
maxReplicas: 5
metrics:
- type: Object
object:
describedObject:
kind: Service
name: prometheus-example-app
metric:
name: http_requests
target:
a verageValue: "5"
type: A verageValue

你可以通过以下链接了解更多关于 Kubernetes HPA 自动扩缩容的详细信息:

https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

这里我们将使用自定义的 http_requests_total 指标来实现 Pod 自动伸缩。

接下来,我们可以生成一段样本流量来观察 HPA 的实际运行效果。我这里同样可以使用hey来完成压力请求测试。

hey -c 10 -n 5000 https://hpa.demo

总 结

通过本文,我们了解了 Rancher 2.5 新版监控体系在 Kubernetes 监控、自定义指标采集、Grafana 可视化以及 HPA 自动扩缩容方面的灵活能力。无论是开发人员还是集群管理员,都可以借助这套监控堆栈更高效地监控应用工作负载、部署可视化面板,并充分利用 Kubernetes 提供的高级工作负载管理能力。

来源:https://apiv1.oschina.net/oschinapi/blog/detail?id=4875093
上一篇Kubernetes生产环境部署与运维最佳实践 下一篇Kubernetes探针机制详解与配置实践
本站内容用于信息整理与展示,如有侵权或内容问题请及时联系处理。

相关推荐

补充同频道和同主题内容,方便继续浏览更多相关内容。

同类最新

继续查看同栏目最近更新的文章。

更多
VMware安装Ubuntu完整教程:创建虚拟机与启动验证
系统平台 · 2026-09-01

VMware安装Ubuntu完整教程:创建虚拟机与启动验证

本教程详细演示如何在VMware中创建Ubuntu虚拟机,涵盖ISO挂载、硬件配置、安装向导及启动验证。通过清晰的步骤与验证命令,帮助新手快速搭建可用的Linux学习环境。

Win10专业版U盘安装教程:制作启动盘与完整安装步骤
系统平台 · 2026-09-01

Win10专业版U盘安装教程:制作启动盘与完整安装步骤

本文提供Win10专业版U盘安装完整流程:准备8GB以上U盘与官方镜像,制作启动盘并核对盘符;通过F12 F11 Esc等快捷键或BIOS设置U盘为第一启动项;安装时选择专业版并谨慎分区;完成后在“设置—系统—关于”验证版本与激活状态。操作前务必备份数据。

Windows10系统字体太小怎么调大
系统平台 · 2026-08-27

Windows10系统字体太小怎么调大

Windows10系统字体太小怎么调大?只需两步:首先打开设置中的显示选项,将缩放比例调整为125%或150%;随后运行ClearType文本调谐器优化字体清晰度。此方法适用于高分屏及普通屏幕,无需修改注册表即可解决界面拥挤问题。

Win10磁盘占用100%基础排查:从监控到清理的完整步骤
系统平台 · 2026-08-27

Win10磁盘占用100%基础排查:从监控到清理的完整步骤

Windows 10系统出现磁盘占用100%会导致电脑卡顿、程序响应缓慢。本文提供基础排查方案:首先通过任务管理器确认是否为磁盘高负载,随后进入系统存储页面分析C盘占用类别,最后针对性清理临时文件。遵循此流程可有效缓解磁盘压力,避免盲目重装系统。

Windows10系统怎么显示此电脑和控制面板
系统平台 · 2026-08-27

Windows10系统怎么显示此电脑和控制面板

Windows10默认可能不显示桌面图标,导致找不到“此电脑”和“控制面板”。只需进入个性化设置,在“桌面图标设置”中勾选对应选项即可恢复。本文提供详细图文步骤,帮助快速找回系统入口。