本文转载自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 ExampleapiVersion: 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 提供的高级工作负载管理能力。
