当前位置: 首页 > news >正文

kube-prometheus监控服务发现

首次看到kube-prometheus的manifests可能会被吓到,太多yaml配置了,根本不知道从何处入手

alertmanager-alertmanager.yaml                                   
nodeExporter-daemonset.yaml
alertmanager-networkPolicy.yaml                                  
nodeExporter-networkPolicy.yaml
alertmanager-podDisruptionBudget.yaml                            
nodeExporter-prometheusRule.yaml
alertmanager-prometheusRule.yaml                                 
nodeExporter-serviceAccount.yaml
alertmanager-secret.yaml                                         
nodeExporter-serviceMonitor.yaml
alertmanager-serviceAccount.yaml                                 
nodeExporter-service.yaml
alertmanager-serviceMonitor.yaml                                 prometheusAdapter-apiService.yaml
alertmanager-service.yaml                                        prometheusAdapter-clusterRoleAggregatedMetricsReader.yaml
blackboxExporter-clusterRoleBinding.yaml                         prometheusAdapter-clusterRoleBindingDelegator.yaml
blackboxExporter-clusterRole.yaml                                prometheusAdapter-clusterRoleBinding.yaml
blackboxExporter-configuration.yaml                              prometheusAdapter-clusterRoleServerResources.yaml
blackboxExporter-deployment.yaml                                 prometheusAdapter-clusterRole.yaml
blackboxExporter-networkPolicy.yaml                              prometheusAdapter-configMap.yaml
blackboxExporter-serviceAccount.yaml                             prometheusAdapter-deployment.yaml
blackboxExporter-serviceMonitor.yaml                             prometheusAdapter-networkPolicy.yaml
blackboxExporter-service.yaml                                    prometheusAdapter-podDisruptionBudget.yaml
grafana-config.yaml                                              prometheusAdapter-roleBindingAuthReader.yaml
grafana-dashboardDatasources.yaml                                prometheusAdapter-serviceAccount.yaml
grafana-dashboardDefinitions.yaml                                prometheusAdapter-serviceMonitor.yaml
grafana-dashboardSources.yaml                                    prometheusAdapter-service.yaml
grafana-deployment.yaml                                          
prometheus-clusterRoleBinding.yaml
grafana-networkPolicy.yaml                                       
prometheus-clusterRole.yaml
grafana-prometheusRule.yaml                                      
prometheus-networkPolicy.yaml
grafana-serviceAccount.yaml                                      prometheusOperator-clusterRoleBinding.yaml
grafana-serviceMonitor.yaml                                      prometheusOperator-clusterRole.yaml
grafana-service.yaml                                             prometheusOperator-deployment.yaml
kubePrometheus-prometheusRule.yaml                               prometheusOperator-networkPolicy.yaml
kubernetesControlPlane-prometheusRule.yaml                       prometheusOperator-prometheusRule.yaml
kubernetesControlPlane-serviceMonitorApiserver.yaml              prometheusOperator-serviceAccount.yaml
kubernetesControlPlane-serviceMonitorCoreDNS.yaml                prometheusOperator-serviceMonitor.yaml
kubernetesControlPlane-serviceMonitorKubeControllerManager.yaml  prometheusOperator-service.yaml
kubernetesControlPlane-serviceMonitorKubelet.yaml                
prometheus-podDisruptionBudget.yaml
kubernetesControlPlane-serviceMonitorKubeScheduler.yaml          
prometheus-prometheusRule.yaml
kubeStateMetrics-clusterRoleBinding.yaml                         
prometheus-prometheus.yaml
kubeStateMetrics-clusterRole.yaml                                
prometheus-roleBindingConfig.yaml
kubeStateMetrics-deployment.yaml                                 
prometheus-roleBindingSpecificNamespaces.yaml
kubeStateMetrics-networkPolicy.yaml                              
prometheus-roleConfig.yaml
kubeStateMetrics-prometheusRule.yaml                             
prometheus-roleSpecificNamespaces.yaml
kubeStateMetrics-serviceAccount.yaml                             
prometheus-serviceAccount.yaml
kubeStateMetrics-serviceMonitor.yaml                             
prometheus-serviceMonitor.yaml
kubeStateMetrics-service.yaml                                    
prometheus-service.yaml
nodeExporter-clusterRoleBinding.yaml                             
setup
nodeExporter-clusterRole.yaml

不过经过仔细观察,你会发现这些文件都是不同类型文件的重复,有以下几种类型的资源

  • service/ServiceAccount
  • clusterrole
  • deployment/DaemonSet
  • prometheus
  • prometheusRule
  • ClusterRole/ClusterRoleBinding
  • Role/RoleBinding
  • Secret/ConfigMap
  • ServiceMonitor/PodMonitor

    而服务发现就需要用到了 ServiceMonitor/PodMonitor

服务发现

kube-prometheus 本身不直接进行服务发现,而是通过其核心组件 PrometheusPrometheus Operator,结合 Kubernetes 原生机制,实现自动化的、声明式的服务发现(Service Discovery)

在这里插入图片描述

从上图可以看出,kube-prometheus 使用 Custom Resource Definitions (CRDs) 来定义“监控目标”,并通过 Prometheus Operator 将这些 CR 转换为 Prometheus 的实际抓取配置。

三大发现机制

CRD作用发现对象
ServiceMonitor基于 Kubernetes Service 发现目标Service 后端的 Pod
PodMonitor直接基于 Pod 发现目标特定标签的 Pod
Probe基于静态列表或 Blackbox 探测外部 HTTP/HTTPS/TCP 端点

1. ServiceMonitor:最常用的服务发现方式

原理
  1. 定义一个 ServiceMonitor,指定要监控的 Service 的标签选择器(selector
  2. Prometheus Operator 监听 ServiceMonitor 资源
  3. Operator 查询 Kubernetes API,找到所有匹配标签的 Service
  4. 对每个 Service,获取其后端 Pods 的 IP 和端口
  5. 生成 Prometheus 的 scrape_configs,自动添加这些 Pod 为监控目标
以grafana的 ServiceMonitor为例
# 指定api组 monitoring.coreos.com 以及版本 v1
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:labels:# 使用 Kubernetes 应用推荐标签,便于组织和筛选资源app.kubernetes.io/component: grafana  # 标识资源名称app.kubernetes.io/name: grafana # 标识组件类型app.kubernetes.io/part-of: kube-prometheus  # 表明它是 `kube-prometheus` 项目的一部分app.kubernetes.io/version: 11.6.0  # 指定 Grafana 版本(用于版本追踪)name: grafana   # 该 ServiceMonitor 的名称# 它位于 `monitoring` 命名空间中(与 Prometheus 同处)namespace: monitoring
spec:endpoints:- interval: 15s  # 抓取频率:每 15 秒从目标拉取一次指标port: http # 指定 Service 中名为 `http` 的端口作为抓取目标selector:matchLabels:# 服务选择器:表示要监控所有带有此标签的 Kubernetes Serviceapp.kubernetes.io/name: grafana

grafana service

apiVersion: v1
kind: Service
metadata:labels:app.kubernetes.io/component: grafanaapp.kubernetes.io/name: grafana  # 这个标签必须有,否则ServiceMonitor将绑定失败app.kubernetes.io/part-of: kube-prometheusapp.kubernetes.io/version: 11.6.0name: grafananamespace: monitoring
spec:ports:- name: httpport: 3000targetPort: httpselector:app.kubernetes.io/component: grafanaapp.kubernetes.io/name: grafanaapp.kubernetes.io/part-of: kube-prometheus

发现流程

ServiceMonitor
Operator 查询
Kubernetes API
查找 label=app.kubernetes.io/name: grafana 的 Service
获取 Service 后端的 Pod 列表
提取 Pod IP:Port
生成 Prometheus 抓取配置
Prometheus 开始采集 /metrics

✅ 只要 monitoring 命名空间下有 Service 带有 app.kubernetes.io/name: grafana 标签,其后端 Pod 就会被自动监控。


2. PodMonitor:直接监控 Pod

适用场景

  • 没有 Service 的 Pod(如 DaemonSet)
  • 需要更细粒度控制抓取配置
  • 多个端口暴露不同指标

示例

apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:name: node-exporternamespace: monitoring
spec:selector:matchLabels:app: node-exporterpodMetricsEndpoints:- port: metricsinterval: 10spath: /metricsnamespaceSelector:any: true

发现流程

  1. Operator 查找所有带有 app: node-exporter 标签的 Pod
  2. 获取其 IP 和 metrics 端口
  3. 配置 Prometheus 抓取 /metrics

✅ 常用于 node-exporterkube-state-metrics 等系统组件。


🔍 3. Probe:外部服务探测

适用场景

  • 监控集群外部的 HTTP 服务
  • Blackbox 监控(连通性、延迟)
  • 静态 IP 列表

示例

apiVersion: monitoring.coreos.com/v1
kind: Probe
metadata:name: external-https
spec:prober:url: blackbox-exporter.example.comtargets:staticConfig:static:- https://google.com- https://github.commodule: http_2xx

🔄 自动更新机制

  1. 当你创建/更新 ServiceMonitor
  2. Prometheus Operator 检测到变化
  3. Operator 调用 Prometheus 的 Reload API(或挂载 ConfigMap)
  4. Prometheus 重新加载配置,无需重启

⚡ 实现了动态服务发现,新增服务自动被监控。


📊 kube-prometheus 中预置的发现配置

kube-prometheus 默认包含多个 ServiceMonitorPodMonitor,用于监控:

组件使用的 CRD
kube-apiserverServiceMonitor
kube-schedulerPodMonitor
kube-controller-managerPodMonitor
kubeletPodMonitor
corednsServiceMonitor
node-exporterPodMonitor
kube-state-metricsServiceMonitor
Prometheus 本身PodMonitor

kube-prometheus 如何做服务发现?

机制工具说明
声明式配置ServiceMonitor / PodMonitor用户通过 YAML 定义“想监控谁”
控制器监听Prometheus Operator监听 CRD 变化,生成 Prometheus 配置
Kubernetes API 查询Operator + Prometheus查询 Service/Pod 列表
动态配置更新Prometheus Reload无需重启,自动生效
底层支持Prometheus 内置 kubernetes_sd_configs实现基于角色的服务发现

kube-prometheus 通过 “CRD + Operator + Kubernetes API + Prometheus SD” 四层机制,实现了对 Kubernetes 环境的全自动、动态、声明式服务发现。

http://www.dtcms.com/a/426846.html

相关文章:

  • 攻防世界-Web-Web_python_template_injection
  • seo站内优化公司河北邯郸seo网站建设网站优化
  • wordpress网站插件优秀校园网站
  • Hibernate批量查询方法全面解析
  • 深度解析 ChatGPT 和 Claude 的记忆机制
  • 994. 腐烂的橘子,207. 课程表, 208.实现 Trie (前缀树)
  • 有趣的化学元素
  • 深圳网站建设者西安广告公司
  • READ_ONCE、smp_store_release在io_uring中实例分析
  • C/C++数据结构之用数组实现栈
  • Linux timekeeping
  • macOS 下安装 zsh、zsh-syntax-highlighting、powerlevel9k、nerd-font
  • CarveMe:代谢模型构建
  • windows显示驱动开发-调试间接显示驱动程序(二)
  • 企业平台网站建设制作一个网站平台
  • LinuxC++——etcd分布式键值存储系统入门
  • 使用arcgis提取评价指标时,导出数据是负数-9999
  • VUE3+element plus 实现表格行合并
  • LinuxC++——etcd分布式键值存储系统API(libetcd-cpp-api3)下载与二次封装
  • Electron vue项目 打包 exe文件2
  • 【开题答辩全过程】以 springboot高校创新创业课程体系的设计与实现为例,包含答辩的问题和答案
  • package.json详解
  • iOS 应用上架全流程解析,苹果应用发布步骤、ipa 上传工具、TestFlight 测试与 App Store 审核经验
  • QGIS + ArcGIS Pro 下载常见卫星影像及 ESRI Wayback 历史影像
  • Hexo搭建/部署个人博客教程
  • 中山 网站建设发布平台是什么
  • Qt操作Windows平板上摄像头
  • 外贸建站哪好asp网站打开很慢的原因
  • rknn yolo11 推理
  • 虚幻基础:容器