k8s监控方案实践(一):部署Prometheus与Node Exporter

news2025/5/10 0:43:21

k8s监控方案实践(一):部署Prometheus与Node Exporter

文章目录

  • k8s监控方案实践(一):部署Prometheus与Node Exporter
  • 一、Prometheus简介
  • 二、Prometheus+Node Exporter实战部署
    • 1. 创建Namespace(prometheus-namespace.yaml)
    • 2. 创建Node Exporter DaemonSet(node-exporter.yaml)
    • 3. 创建ServiceAccount(prometheus-serviceaccount.yaml)
    • 4. 创建ClusterRoleBinding(prometheus-clusterrolebinding.yaml)
    • 5. 创建ConfigMap(prometheus-config.yaml)
    • 5. 创建Service(prometheus-svc.yaml)
    • 7. 创建Deployment(prometheus-deploy.yaml)
    • 8. 部署所有资源
  • 三、验证部署
    • 1. 验证Pod状态
    • 2. 访问Prometheus Web UI
  • 总结


随着容器化和微服务架构的普及,系统架构日益复杂,传统监控工具难以胜任对多服务、多节点环境的全面可观测性需求。Prometheus 作为 Cloud Native Computing Foundation(CNCF) 的核心项目之一,因其灵活的数据模型、强大的查询语言(PromQL)以及对 Kubernetes 的天然支持,逐渐成为容器环境下主流的监控方案。

在本系列中,我们将围绕 Prometheus 在 Kubernetes 中的部署与实践展开介绍。本篇作为第一篇,将聚焦于 Prometheus 的基础部署,并集成 Node Exporter 实现对集群节点资源(如 CPU、内存、磁盘、网络等)的实时监控,为后续服务指标采集与告警体系搭建打下基础。

一、Prometheus简介

Prometheus 采用拉取模型(Pull-based)来定期采集被监控目标暴露的指标数据,所有数据以时间序列的形式存储在本地时间序列数据库中,支持高效压缩和快速查询。其核心组件包括:

  • Prometheus Server:主服务组件,负责服务发现、数据抓取、存储和 PromQL 查询处理
  • Exporter:用于暴露指标的采集器,常见如 Node Exporter(主机指标)、Kube-State-Metrics(K8s 状态)、Blackbox Exporter(探测)等
  • Alertmanager:负责接收和管理 Prometheus 的告警信息,并支持邮件、Slack、WebHook 等通知方式
  • Pushgateway(可选):用于支持短生命周期任务的指标上报,如定时任务脚本
  • Web UI 与 HTTP API:提供基础的可视化查询界面与外部系统接入能力

在 Kubernetes 环境下,Prometheus 可结合服务发现机制动态发现集群中的 Pod、Service、Node 等资源,从而自动完成监控目标的注册与指标抓取,非常适合用于监控容器化和微服务系统。

g.cn/direct/31da7451a2e34431b7ce7606e6722ebf.png)

二、Prometheus+Node Exporter实战部署

1. 创建Namespace(prometheus-namespace.yaml)

创建名为prometheus的命名空间,用于隔离部署监控相关资源

apiVersion: v1
kind: Namespace
metadata:
  name: prometheus

2. 创建Node Exporter DaemonSet(node-exporter.yaml)

以守护进程形式在所有节点部署Node Exporter,用于采集节点 CPU、内存、磁盘、网络等基础指标

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: prometheus
  labels:
    name: node-exporter
spec:
  selector:
    matchLabels:
     name: node-exporter
  template:
    metadata:
      labels:
        name: node-exporter
    spec:
      hostPID: true
      hostIPC: true
      hostNetwork: true  #使用宿主机网络,便于 Prometheus 拉取指标
      containers:
      - name: node-exporter
        image: harbor.local/k8s/node-exporter:v1.7.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9100
        securityContext:
          privileged: true
        args:
        - --path.procfs
        - /host/proc
        - --path.sysfs
        - /host/sys
        - --collector.filesystem.ignored-mount-points
        - '"^/(sys|proc|dev|host|etc)($|/)"'
        volumeMounts:
        - name: dev
          mountPath: /host/dev
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: rootfs
          mountPath: /rootfs
      tolerations:
      - key: "node-role.kubernetes.io/control-plane"
        operator: "Exists"
        effect: "NoSchedule"
      volumes:
        - name: proc
          hostPath:
            path: /proc
        - name: dev
          hostPath:
            path: /dev
        - name: sys
          hostPath:
            path: /sys
        - name: rootfs
          hostPath:
            path: /root

3. 创建ServiceAccount(prometheus-serviceaccount.yaml)

为 Prometheus 创建专属的ServiceAccount,用于后续绑定权限

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: prometheus

4. 创建ClusterRoleBinding(prometheus-clusterrolebinding.yaml)

将集群管理员权限绑定到 Prometheus 的ServiceAccount,以便其能访问 Kubernetes API 拉取监控目标

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-clusterrolebinding
subjects:
  - kind: ServiceAccount
    name: prometheus
    namespace: prometheus
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

5. 创建ConfigMap(prometheus-config.yaml)

定义 Prometheus 的主配置文件prometheus.yml,配置抓取规则、服务发现方式、静态目标与黑盒探测等

---
kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    app: prometheus
  name: prometheus-config
  namespace: prometheus
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      scrape_timeout: 10s
      evaluation_interval: 1m
    alerting:
      alertmanagers:
      - static_configs:
        - targets: ['alertmanager.prometheus.svc.cluster.local:9093']
    rule_files:
      - "rules/*.yml"
    scrape_configs:
    - job_name: 'kubernetes-node'
      kubernetes_sd_configs:
      - role: node
      relabel_configs:
      - source_labels: [__address__]
        regex: '(.*):10250'
        replacement: '${1}:9100'
        target_label: __address__
        action: replace
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
    - job_name: 'kubernetes-node-cadvisor'
      kubernetes_sd_configs:
      - role:  node
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - target_label: __address__
        replacement: kubernetes.default.svc:443
      - source_labels: [__meta_kubernetes_node_name]
        regex: (.+)
        target_label: __metrics_path__
        replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
    - job_name: 'kubernetes-apiserver'
      kubernetes_sd_configs:
      - role: endpoints
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      relabel_configs:
      - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
        action: keep
        regex: default;kubernetes;https
    - job_name: 'kubernetes-service-endpoints'
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
        action: replace
        target_label: __scheme__
        regex: (https?)
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
        action: replace
        target_label: __address__
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
      - action: labelmap
        regex: __meta_kubernetes_service_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_service_name]
        action: replace
        target_label: kubernetes_name
    - job_name: kubernetes-etcd
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      - action: keep
        regex: kube-system;etcd
        source_labels:
        - __meta_kubernetes_namespace
        - __meta_kubernetes_service_name      

    - job_name: 'k8s-cluster'
      scrape_interval: 15s
      static_configs:
        - targets: ['192.168.100.101:9100']
          labels:
            instance: 'master-100.101'
        - targets: ['192.168.100.102:9100']
          labels:
            instance: 'node1-100.102'
        - targets: ['192.168.100.103:9100']
          labels:
            instance: 'node2-100.103'

    - job_name: 'alertmanager'
      scrape_interval: 15s
      static_configs:
      - targets: ['alertmanager.prometheus.svc.cluster.local:9093']
 
    - job_name: "blackbox_http"
      metrics_path: /probe
      params:
        module: [http_2xx]
      static_configs:
        - targets:
          - https://www.baidu.com
      relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 192.168.100.104:9115

    - job_name: "blackbox_tcp"
      metrics_path: /probe
      params:
        module: [tcp_connect]
      static_configs:
        - targets: 
          - 192.168.100.101:3306
      relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 192.168.100.104:9115

5. 创建Service(prometheus-svc.yaml)

暴露 Prometheus Web UI 服务端口(9090),通过NodePort方式允许外部访问

apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: prometheus
  labels:
    app: prometheus
spec:
  type: NodePort
  ports:
    - port: 9090
      targetPort: 9090
      nodePort: 30001
      protocol: TCP
  selector:
    app: prometheus
    component: server

7. 创建Deployment(prometheus-deploy.yaml)

部署Prometheus Server,指定使用的配置文件和挂载方式,绑定上一步的ServiceAccount,支持高可用部署(当前为单实例)

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-server
  namespace: prometheus
  labels:
    app: prometheus
spec:
  replicas: 1  #单实例部署,可根据需要扩展为高可用
  selector:
    matchLabels:
      app: prometheus
      component: server
    #matchExpressions:
    #- {key: app, operator: In, values: [prometheus]}
    #- {key: component, operator: In, values: [server]}
  template:
    metadata:
      labels:
        app: prometheus
        component: server
      annotations:
        prometheus.io/scrape: 'false'
    spec:
      nodeName: node2    #固定部署在node2节点上(也可不指定)
      serviceAccountName: prometheus
      containers:
      - name: prometheus
        image: harbor.local/k8s/prometheus:v2.37.6
        imagePullPolicy: IfNotPresent
        command:
          - prometheus
          - --config.file=/etc/prometheus/prometheus.yml
          - --storage.tsdb.path=/prometheus  #设置本地数据存储路径
          - --storage.tsdb.retention=720h
          - --web.enable-lifecycle    #支持热更新配置
          - --web.enable-admin-api 
          - --storage.tsdb.retention.time=30d
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/prometheus/
          name: prometheus-config
        - mountPath: /prometheus/
          name: prometheus-storage-volume
        - mountPath: /etc/prometheus/rules
          name: prometheus-rules-volume
      volumes:
        - name: prometheus-config
          configMap:
            name: prometheus-config
        - name: prometheus-storage-volume
          hostPath:
          #需要去node2节点创建这两个目录并使用chmod -R 777授权
           path: /kubernetes/prometheus/data/  
           type: Directory
        - name: prometheus-rules-volume
          hostPath:
           path: /kubernetes/prometheus/rules  
           type: Directory

8. 部署所有资源

kubectl apply -f prometheus-namespace.yaml
kubectl apply -f node-exporter.yaml
kubectl apply -f prometheus-serviceaccount.yaml
kubectl apply -f prometheus-clusterrolebinding.yaml
kubectl apply -f prometheus-config.yaml
kubectl apply -f prometheus-svc.yaml
kubectl apply -f prometheus-deploy.yaml

三、验证部署

1. 验证Pod状态

kubectl get pod -n prometheus

在这里插入图片描述

2. 访问Prometheus Web UI

通过浏览器访问 http://ip:30001,进入 Prometheus Web UI

在这里插入图片描述


总结

🚀 本文介绍了如何在 Kubernetes 集群中部署 Prometheus,并结合 Node Exporter 实现对节点资源的基础监控。通过配置 ServiceAccount、RBAC 权限、Prometheus 配置文件以及 DaemonSet 部署 Node Exporter,完成了从数据采集到可视化的基本监控体系搭建。
✅ 在下一篇文章中,我们将继续完善监控体系,重点介绍如何在 Prometheus 中集成 Alertmanager,实现基于指标的自动化告警机制,包括告警规则配置、通知渠道设置(如邮件、Webhook 等)以及与 Prometheus 的联动流程,为运维体系提供实时的预警能力。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2371908.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

具身系列——比较3种vpg算法方式玩CartPole游戏(强化学习)

文档1方式参考:https://gitee.com/chencib/ailib/blob/master/rl/vpg_baseline_cartpole.py 文档2方式参考:https://gitee.com/chencib/ailib/blob/master/rl/vpg_batchupdate_cartpole.py 文档3方式参考:https://gitee.com/chencib/ailib/bl…

面向未来的 TCP 协议设计:可扩展与兼容并存

目录 1.设计思路 (1)完整数据结构(字节布局) 1)字段解释: 2)Flags字段设计(1字节位图) (2)进阶版 Java 解码器实现(示例&#xf…

LSB图像信息隐藏系统(MATLAB)

图像信息隐藏系统 系统概述 图像信息隐藏系统是一个基于MATLAB开发的图像隐写工具,采用自适应LSB(最低有效位)隐写算法,实现了信息在图像中的隐藏与提取功能。系统配备了直观的图形用户界面,支持图像分析、信息隐藏、…

C++GO语言微服务项目之 go语言基础语法

目录 01 变量定义 02 自增语法 03 指针 04 go不支持的语法 05 string 06 定长数组-forrange 07 动态数组追加元素 08 切片截取-copy-make介绍 09 map介绍 10 函数 11 内存逃逸 12 import 13 命令行参数-switch 14 标签与continue-goto-break配合使用 15 枚举cons…

最新字节跳动运维云原生面经分享

继续分享最新的go面经。 今天分享的是组织内部的朋友在字节的go运维工程师岗位的云原生方向的面经,涉及Prometheus、Kubernetes、CI/CD、网络代理、MySQL主从、Redis哨兵、系统调优及基础命令行工具等知识点,问题我都整理在下面了 面经详解 Prometheus …

理解 Elasticsearch 的评分机制和 Explain API

作者:来自 Elastic Kofi Bartlett 深入了解 Elasticsearch 的评分机制并探索 Explain API。 想获得 Elastic 认证吗?查看下一期 Elasticsearch Engineer 培训的时间! Elasticsearch 拥有大量新功能,帮助你为你的使用场景构建最佳…

视频编解码学习三之显示器

整理自:显示器_百度百科,触摸屏_百度百科,百度安全验证 分为阴极射线管显示器(CRT),等离子显示器PDP,液晶显示器LCD 液晶显示器的组成。一般来说,液晶显示器由以下几个部分组成: […

K8s网络从0到1

K8s网络从0到1 前言 K8s是一个强大的平台,但它的网络比较复杂,涉及很多概念,例如Pod网络,Service网络,Cluster IPs,NodePort,LoadBalancer和Ingress等等。为了帮助大家理解,模仿TC…

13.Excel:分列

一 分列的作用 将一个单元格中的内容拆分到两个或多个单元格当中。 二 如何使用 1.常规分列使用 注意:分列功能一次只能拆分一列。 长度一致或者数据间有分隔符。 补充:快速选择一列。 CTRL shift 向下箭头。 补充:中英文逗号不同。 可以先通…

计算机网络应用层(5)-- P2P文件分发视频流和内容分发网

💓个人主页:mooridy 💓专栏地址:《计算机网络:自顶向下方法》 大纲式阅读笔记_mooridy的博客-CSDN博客 💓本博客内容为《计算机网络:自顶向下方法》第二章应用层第五、六节知识梳理 关注我&…

Gin优雅关闭 graceful-shutdown

文章目录 优雅关闭示例 - Close 方法项目结构使用方法代码如下代码说明如果去掉代码中的数字1,会发生什么 优雅关闭示例项目结构使用方法使用上下文通知不使用上下文通知 代码 notify-without-context-server.go代码说明 代码 notify-with-context-server.go代码说明…

五子棋html

<!DOCTYPE html> <html lang"zh-CN"> <head> <meta charset"UTF-8" /> <meta name"viewport" content"widthdevice-width, initial-scale1" /> <title>五子棋游戏</title> <style>bo…

JavaWeb:后端web基础(TomcatServletHTTP)

一、今日内容 二、Tomcat 介绍与使用 介绍 基本使用 小结 配置 配置 查找进程 三、Servlet 什么是Servlet 快速入门 需求 步骤 1.新建工程-模块&#xff08;Maven&#xff09; 2.修改打包方式-war 3.编写代码 /*** 可以选择继承HttpServlet*/ WebServlet("/hello&q…

缓存(1):三级缓存

三级缓存是指什么 我们常说的三级缓存如下&#xff1a; CPU三级缓存Spring三级缓存应用架构&#xff08;JVM、分布式缓存、db&#xff09;三级缓存 CPU 基本概念 CPU 的访问速度每 18 个月就会翻 倍&#xff0c;相当于每年增⻓ 60% 左右&#xff0c;内存的速度当然也会不断…

Cursor —— AI编辑器 使用详解

Cursor - The AI Code Editor 一、Cursor 是什么&#xff1f; Cursor 是一款优秀的AI代码编辑器&#xff0c;它内置了 Deepseek-R1、GPT-4、Claude等 AI 模型。 简单说&#xff0c;就是&#xff1a;Cursor VS Code 编辑器 AI 大模型 Cursor 功能特性&#xff08;代码补全、…

Pytorch-CUDA版本环境配置

Pytorch-CUDA版本环境配置 电脑如果是Windows平台下的Nvidia GPU的用户&#xff0c;需配置Pytorch的CUDA版本&#xff0c;分为三步&#xff1a; 1. 安装或更新NVIDA显卡驱动 官方驱动下载地址&#xff1a; https://www.nvidia.cn/Download/index.aspx?langcn 2. 安装CUDA Too…

OpenCV 图形API(77)图像与通道拼接函数-----对图像进行几何变换函数remap()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 对图像应用一个通用的几何变换。 函数 remap 使用指定的映射对源图像进行变换&#xff1a; dst ( x , y ) src ( m a p x ( x , y ) , m a p y…

Spring AI 入门(持续更新)

介绍 Spring AI 是 Spring 项目中一个面向 AI 应用的模块&#xff0c;旨在通过集成开源框架、提供标准化的工具和便捷的开发体验&#xff0c;加速 AI 应用程序的构建和部署。 依赖 <!-- 基于 WebFlux 的响应式 SSE 传输 --> <dependency><groupId>org.spr…

QUIC协议优化:HTTP_3环境下的超高速异步抓取方案

摘要 随着 QUIC 和 HTTP/3 的普及&#xff0c;基于 UDP 的连接复用与内置加密带来了远超 HTTP/2 的性能提升&#xff0c;可显著降低连接握手与拥塞恢复的开销。本文以爬取知乎热榜数据为目标&#xff0c;提出一种基于 HTTPX aioquic 的异步抓取方案&#xff0c;并结合代理 IP设…

uni-app实现完成任务解锁拼图功能

界面如下 代码如下 <template><view class"puzzle-container"><view class"puzzle-title">任务进度 {{completedCount}}/{{totalPieces}}</view><view class"puzzle-grid"><viewv-for"(piece, index) in…