云原生监控:Prometheus Operator,一文带你打通全流程:监控、规则、警报。

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
简介: 云原生监控:Prometheus Operator,一文带你打通全流程:监控、规则、警报。

Prometheus

  1. 安装prometheus-operator
wget https://github.com/prometheus-operator/prometheus-operator/releases/download/v0.64.0/bundle.yaml
kubectl create -f bundle.yaml
  1. 创建示例应用
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-app
        image: fabxc/instrumented_app
        ports:
        - name: web
          containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: example-app
  name: example-app
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: example-app
  type: NodePort
  1. 创建Service和Pod监控对象
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-app
  labels:
    team: frontend
spec:
  selector:
    matchLabels:
      app: example-app
  endpoints:
  - port: 8080-8080
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: example-app
  labels:
    team: frontend
spec:
  selector:
    matchLabels:
      app: example-app
  podMetricsEndpoints:
  - port: web
  1. 部署Prometheus和监控示例应用的Service和Pod

如果在K8S集群上激活了RBAC授权,则必须先创建RBAC规则,并且提前获得Prometheus服务帐户。

接下来创建服务帐户和所需的集群角色和集群角色绑定:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - nodes/metrics
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses
  verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: default
  1. 创建Prometheus对象,并定义选择监控指定标签的ServiceAccount和PodMonitor,最后暴露Prometheus
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
spec:
  serviceAccountName: prometheus
  replicas: 3
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  podMonitorSelector:
    matchLabels:
      team: frontend
  resources:
    requests:
      memory: 400Mi
  enableAdminAPI: false # 如果要开启管理API可设置为true
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
spec:
  type: NodePort
  ports:
  - name: web
    nodePort: 30900
    port: 9090
    protocol: TCP
    targetPort: web
  selector:
    prometheus: prometheus

Alertmanager

对于警报组件,Prometheus Operator引入了2个资源对象:

  • Alertmanager资源对象,它的作用是允许用户以声明的方式描述警报管理器群集。
  • AlertmanagerConfig资源对象,它的作用是允许用户以声明方式描述警报管理器配置。

先准备好警报管理器的配置,也就是创建AlertmanagerConfig资源对象。接着部署有3个副本的警报管理器集群,并使用该警报管理器的配置,最后暴露警报管理器

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: config-example
  labels:
    alertmanagerConfig: example
spec:
  route:
    groupBy: ['...']
    groupWait: 1s
    groupInterval: 1s
    repeatInterval: 1000d
    receiver: 'webhook'
  receivers:
  - name: 'webhook'
    webhookConfigs:
    - url: 'http://192.168.11.254:5001/webhook' # 接收到的警报还要往这个API发送,接收告警的API请见下面的webhook.py代码
      sendResolved: true
---
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
  name: example
spec:
  replicas: 3
  alertmanagerConfiguration: # 此处为全局配置
    name: config-example
---
apiVersion: v1
kind: Service
metadata:
  name: alertmanager-example
spec:
  type: NodePort
  ports:
  - name: web
    nodePort: 30903
    port: 9093
    protocol: TCP
    targetPort: web
  selector:
    alertmanager: example

接收警报消息的py代码:

import json
from flask import Flask, request
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def send():
    try:
        data = json.loads(request.data)
        print(data)
    except Exception as e:
        print(e)
    return 'finish ok ...'
if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=5001)

接收到的警报消息之所以要推给另外的接口,其目的是还可以对警报消息做进一步的处理。然后再往其它地方推送警报,比如钉钉、邮件等等。

Prometheus和Alertmanager整合

在之前创建Prometheus对象的yaml文件的基础上,拿过来改造:

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
spec:
  serviceAccountName: prometheus
  replicas: 3
  alerting: # 主要是改造这里,此处与警报管理器整合
    alertmanagers:
    - namespace: default
      name: alertmanager-example
      port: web
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  podMonitorSelector:
    matchLabels:
      team: frontend
  resources:
    requests:
      memory: 400Mi
  enableAdminAPI: false
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
spec:
  type: NodePort
  ports:
  - name: web
    nodePort: 30900
    port: 9090
    protocol: TCP
    targetPort: web
  selector:
    prometheus: prometheus

整合后,在Prometheus的页面中 Status > Runtime & Build Information 下会看到已经发现到了3个警报管理器实例

如果没有成功发现,请检查配置是否正确。

Rule

规则发现机制:

  • 默认情况下,Prometheus资源仅发现在同一命名空间中的规则
  • 默认情况下,如果spec.ruleSelector字段为nil,则表示不匹配任何规则
  • 要发现所有命名空间中的规则,可以给ruleNamespaceSelector字段传空字典,例如ruleNamespaceSelector: {}
  • 若要从与特定标签匹配的所有命名空间中发现规则,可以使用matchLabels
  1. 创建警报规则
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  creationTimestamp: null
  labels:
    prometheus: example
    role: alert-rules
  name: prometheus-example-rules
spec:
  groups:
  - name: ./example.rules
    rules:
    - alert: ExampleAlert
      expr: vector(1)

出于演示的目的,这条规则始终会触发警报。方便验证是否正常工作。

  1. 开始部署Prometheus规则

之前已经将Prometheus和Alertmanageryaml进行整合,接下来,继续在这个yaml的基础上改造。

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
spec:
  serviceAccountName: prometheus
  replicas: 3
  alerting:
    alertmanagers:
    - namespace: default
      name: alertmanager-example
      port: web
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  podMonitorSelector:
    matchLabels:
      team: frontend
  resources:
    requests:
      memory: 400Mi
  enableAdminAPI: false
  ruleSelector:
    matchLabels:
      role: alert-rules
      prometheus: example
  ruleNamespaceSelector: {} # 要为PrometheusRules发现选择的命名空间。如果未指定,则仅使用与普罗米修斯对象所在的命名空间相同的命名空间。
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
spec:
  type: NodePort
  ports:
  - name: web
    nodePort: 30900
    port: 9090
    protocol: TCP
    targetPort: web
  selector:
    prometheus: prometheus

在Prometheus的页面中 Status > Rules 下会看到这条示例规则

在Alertmanager的页面中,也有了警报消息,说明警报组件已经接收到了Prometheus发送过来的警报,不过貌似时区有点不对。

综合测试

测试步骤:

  1. 修改或添加警报规则
  2. 观察Prometheus是否能发现到新的规则
  3. 警报触发后观察Alertmanager能否正常接收到警报
  4. 警报触发后观察Alertmanager能否正常推送到其它接口

测试结果截图:

相关文章
|
3月前
|
Prometheus 监控 Cloud Native
云原生监控实战:Prometheus+Grafana快速搭建指南
云原生监控实战:Prometheus+Grafana快速搭建指南
|
3月前
|
存储 Prometheus 监控
OSS监控体系搭建:Prometheus+Grafana实时监控流量、错误码、存储量(开源方案替代云监控自定义视图)
本方案基于Prometheus构建OSS监控系统,涵盖架构设计、指标采集、可视化、告警及性能优化,助力企业实现高可用、低成本的自建监控体系。
393 1
|
2月前
|
Cloud Native 中间件 调度
云原生信息提取系统:容器化流程与CI/CD集成实践
本文介绍如何通过工程化手段解决数据提取任务中的稳定性与部署难题。结合 Scrapy、Docker、代理中间件与 CI/CD 工具,构建可自动运行、持续迭代的云原生信息提取系统,实现结构化数据采集与标准化交付。
云原生信息提取系统:容器化流程与CI/CD集成实践
|
4月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
384 79
|
3月前
|
存储 监控 Cloud Native
云原生监控实战:Prometheus+Grafana打造RDS多维度预警体系
本方案构建了基于Prometheus与Thanos的云原生RDS监控体系,涵盖数据采集、存储、可视化与告警全流程。支持10万+QPS采集、90%存储压缩,具备<30秒告警延迟能力。通过自定义指标与智能预警策略,显著提升故障发现效率,实现分钟级响应。
305 5
|
3月前
|
Prometheus 监控 Cloud Native
|
2月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
|
6月前
|
Prometheus Kubernetes 监控
Kubernetes监控:Prometheus与AlertManager结合,配置邮件告警。
完成这些步骤之后,您就拥有了一个可以用邮件通知你的Kubernetes监控解决方案了。当然,所有的这些配置都需要相互照应,还要对你的Kubernetes集群状况有深入的了解。希望这份指南能帮助你创建出适合自己场景的监控系统,让你在首次发现问题时就能做出响应。
301 22
|
9月前
|
存储 数据采集 Prometheus
Grafana Prometheus Altermanager 监控系统
Grafana、Prometheus 和 Alertmanager 是一套强大的开源监控系统组合。Prometheus 负责数据采集与存储,Alertmanager 处理告警通知,Grafana 提供可视化界面。本文简要介绍了这套系统的安装配置流程,包括各组件的下载、安装、服务配置及开机自启设置,并提供了访问地址和重启命令。适用于希望快速搭建高效监控平台的用户。
490 20
|
9月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
778 7

热门文章

最新文章