k8s監控方案實踐(一):部署Prometheus與Node Exporter

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-exporternamespace: prometheuslabels:name: node-exporter
spec:selector:matchLabels:name: node-exportertemplate:metadata:labels:name: node-exporterspec:hostPID: truehostIPC: truehostNetwork: true  #使用宿主機網絡,便于 Prometheus 拉取指標containers:- name: node-exporterimage: harbor.local/k8s/node-exporter:v1.7.0imagePullPolicy: IfNotPresentports:- containerPort: 9100securityContext:privileged: trueargs:- --path.procfs- /host/proc- --path.sysfs- /host/sys- --collector.filesystem.ignored-mount-points- '"^/(sys|proc|dev|host|etc)($|/)"'volumeMounts:- name: devmountPath: /host/dev- name: procmountPath: /host/proc- name: sysmountPath: /host/sys- name: rootfsmountPath: /rootfstolerations:- key: "node-role.kubernetes.io/control-plane"operator: "Exists"effect: "NoSchedule"volumes:- name: prochostPath:path: /proc- name: devhostPath:path: /dev- name: syshostPath:path: /sys- name: rootfshostPath:path: /root

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

為 Prometheus 創建專屬的ServiceAccount,用于后續綁定權限

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

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

將集群管理員權限綁定到 Prometheus 的ServiceAccount,以便其能訪問 Kubernetes API 拉取監控目標

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: prometheus-clusterrolebinding
subjects:- kind: ServiceAccountname: prometheusnamespace: prometheus
roleRef:kind: ClusterRolename: cluster-adminapiGroup: rbac.authorization.k8s.io

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

定義 Prometheus 的主配置文件prometheus.yml,配置抓取規則、服務發現方式、靜態目標與黑盒探測等

---
kind: ConfigMap
apiVersion: v1
metadata:labels:app: prometheusname: prometheus-confignamespace: prometheus
data:prometheus.yml: |global:scrape_interval: 15sscrape_timeout: 10sevaluation_interval: 1malerting:alertmanagers:- static_configs:- targets: ['alertmanager.prometheus.svc.cluster.local:9093']rule_files:- "rules/*.yml"scrape_configs:- job_name: 'kubernetes-node'kubernetes_sd_configs:- role: noderelabel_configs:- source_labels: [__address__]regex: '(.*):10250'replacement: '${1}:9100'target_label: __address__action: replace- action: labelmapregex: __meta_kubernetes_node_label_(.+)- job_name: 'kubernetes-node-cadvisor'kubernetes_sd_configs:- role:  nodescheme: httpstls_config:ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crtbearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenrelabel_configs:- action: labelmapregex: __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: endpointsscheme: httpstls_config:ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crtbearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenrelabel_configs:- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]action: keepregex: default;kubernetes;https- job_name: 'kubernetes-service-endpoints'kubernetes_sd_configs:- role: endpointsrelabel_configs:- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]action: keepregex: true- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]action: replacetarget_label: __scheme__regex: (https?)- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]action: replacetarget_label: __metrics_path__regex: (.+)- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]action: replacetarget_label: __address__regex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2- action: labelmapregex: __meta_kubernetes_service_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace- source_labels: [__meta_kubernetes_service_name]action: replacetarget_label: kubernetes_name- job_name: kubernetes-etcdkubernetes_sd_configs:- role: endpointsrelabel_configs:- action: keepregex: kube-system;etcdsource_labels:- __meta_kubernetes_namespace- __meta_kubernetes_service_name      - job_name: 'k8s-cluster'scrape_interval: 15sstatic_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: 15sstatic_configs:- targets: ['alertmanager.prometheus.svc.cluster.local:9093']- job_name: "blackbox_http"metrics_path: /probeparams:module: [http_2xx]static_configs:- targets:- https://www.baidu.comrelabel_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: /probeparams:module: [tcp_connect]static_configs:- targets: - 192.168.100.101:3306relabel_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: prometheusnamespace: prometheuslabels:app: prometheus
spec:type: NodePortports:- port: 9090targetPort: 9090nodePort: 30001protocol: TCPselector:app: prometheuscomponent: server

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

部署Prometheus Server,指定使用的配置文件和掛載方式,綁定上一步的ServiceAccount,支持高可用部署(當前為單實例)

---
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheus-servernamespace: prometheuslabels:app: prometheus
spec:replicas: 1  #單實例部署,可根據需要擴展為高可用selector:matchLabels:app: prometheuscomponent: server#matchExpressions:#- {key: app, operator: In, values: [prometheus]}#- {key: component, operator: In, values: [server]}template:metadata:labels:app: prometheuscomponent: serverannotations:prometheus.io/scrape: 'false'spec:nodeName: node2    #固定部署在node2節點上(也可不指定)serviceAccountName: prometheuscontainers:- name: prometheusimage: harbor.local/k8s/prometheus:v2.37.6imagePullPolicy: IfNotPresentcommand:- 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=30dports:- containerPort: 9090protocol: TCPvolumeMounts:- mountPath: /etc/prometheus/name: prometheus-config- mountPath: /prometheus/name: prometheus-storage-volume- mountPath: /etc/prometheus/rulesname: prometheus-rules-volumevolumes:- name: prometheus-configconfigMap:name: prometheus-config- name: prometheus-storage-volumehostPath:#需要去node2節點創建這兩個目錄并使用chmod -R 777授權path: /kubernetes/prometheus/data/  type: Directory- name: prometheus-rules-volumehostPath: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.pswp.cn/pingmian/80152.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/80152.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/80152.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

谷歌最新推出的Gemini 2.5 Flash人工智能模型因其安全性能相較前代產品出現下滑

每周跟蹤AI熱點新聞動向和震撼發展 想要探索生成式人工智能的前沿進展嗎?訂閱我們的簡報,深入解析最新的技術突破、實際應用案例和未來的趨勢。與全球數同行一同,從行業內部的深度分析和實用指南中受益。不要錯過這個機會,成為AI領…

【Python】PDF文件處理(PyPDF2、borb、fitz)

Python提供了多種方法和庫用于處理PDF文件,這些工具可以幫助開發者實現諸如讀取、寫入、合并、拆分以及壓縮等功能。以下是幾個常用的Python PDF操作庫及其基本用法(PyPDF2、borb、fitz)。 1. PyPDF2 PyPDF2 是一個功能強大的庫&#xff0…

websocketd 10秒教程

websocketd 參考地址:joewalnes/websocketd 官網地址:websocketd websocketd簡述 websocketd是一個簡單的websocket服務Server,運行在命令行方式下,可以通過websocketd和已經有程序進行交互。 現在,可以非常容易地構…

Spring Boot 基于 Cookie 實現單點登錄:原理、實踐與優化詳解

前言 在多系統交互的應用場景中,單點登錄(SSO)能夠顯著提升用戶體驗,減少重復登錄的繁瑣操作。基于 Cookie 的單點登錄方案,憑借其簡單直觀、瀏覽器原生支持的特性,成為快速實現單點登錄的有效方式。本文將…

ModBus協議詳解:從基礎概念到C#實現RTU與TCP通訊

ModBus協議是莫迪康公司為了讓PLC之間進行數據通信而設計出來的協議。它是一種總線協議,是一種一對多,上下級的關系。 它的應用廣泛,具有免費開源,操作簡單的有點,并且可以兼容串口和網絡通訊,兼容也不錯。…

PHP數組排序深度解析:sort()、rsort()、asort()、arsort()、ksort()、krsort() 的適用場景與性能對比

在PHP開發中,數組排序是日常操作的核心技能之一。無論是處理用戶數據、產品列表,還是分析日志信息,合理的排序方法能顯著提升代碼的效率和可維護性。PHP提供了多種數組排序函數(如 sort()、rsort()、asort() 等)&#…

RabittMQ-高級特性2-應用問題

文章目錄 前言延遲隊列介紹ttl死信隊列存在問題延遲隊列插件安裝延遲插件使用事務消息分發概念介紹限流非公平分發(負載均衡) 限流負載均衡RabbitMQ應用問題-冪等性保障順序性保障介紹1順序性保障介紹2消息積壓總結 前言 延遲隊列介紹 延遲隊列(Delaye…

HOW - 在 Mac 上的 Chrome 瀏覽器中調試 Windows 場景下的前端頁面

文章目錄 為什么需要模擬 Windows 環境?一、修改 User-Agent 模擬 Windows 瀏覽器方法 1:通過 Chrome 開發者工具修改 UA方法 2:使用瀏覽器插件 二、模擬 Windows 的字體和滾動條樣式1. 模擬 Windows 字體2. 強制顯示滾動條(模擬 …

如何刪除豆包本地大模型

由于無法選擇大模型的安裝位置,因此會占用C盤大量空間,然后又找到不卸載的地方,經排查豆包大模型安裝位為:C:\Users\[當前電腦用戶]\AppData\Local\Doubao\User Data,只能進行手動卸載。

Linux C語言線程編程入門筆記

目錄 開發環境準備 線程基礎概念 進程與線程的關系 線程生命周期 創建線程 等待線程結束 線程函數和參數 互斥鎖與共享資源保護 總結 開發環境準備 操作系統:以 Linux 為例(Ubuntu/CentOS 等主流發行版)。請確保系統已安裝 GNU C 編…

levelDB的數據查看(非常詳細)

起因:.net大作業天氣預報程序(WPF)答辯時,老師問怎么維持數據持久性的,啟動時加載的數據存在哪里,我明白老師想考的應該是json文件的解析(正反),半天沒答上來存那個文件了(老師默認這個文件是自…

數據分析怎么做?高效的數據分析方法有哪些?

目錄 一、數據分析的對象和目的 (一)數據分析的常見對象 (二)數據分析的目的 二、數據分析怎么做? (一)明確問題 (二)收集數據 (三)清洗和…

手寫 Vue 源碼 === 完善依賴追蹤與觸發更新

目錄 依賴收集的完整實現 trackEffects:建立雙向依賴關系 觸發更新的完整實現 完整的響應式流程 為什么使用 Map 而不是 Set? 總結 在上一篇文章中,我們介紹了 Vue3 響應式系統的基本原理和 activeEffect 的作用。現在,我們將深入探討完善后的依賴追蹤和觸發更新機制…

從代碼學習深度學習 - 區域卷積神經網絡(R-CNN)系列 PyTorch版

文章目錄 前言R-CNNFast R-CNN興趣區域匯聚層 (RoI Pooling)代碼示例:興趣區域匯聚層 (RoI Pooling) 的計算方法Faster R-CNNMask R-CNN雙線性插值 (Bilinear Interpolation) 與興趣區域對齊 (RoI Align)興趣區域對齊層的輸入輸出全卷積網絡 (FCN) 的作用掩碼輸出形狀總結前言…

18個國內wordpress主題推薦

工廠wordpress中文主題 紅藍色搭配的工廠wordpress中文主題,適合從事生產、加工的工廠官方網站使用。 https://www.jianzhanpress.com/?p8533 Pithy設計師wordpress網站模板 精練簡潔的wordpress模板,設計師或設計工作室展示型網站模板。 https://w…

低成本自動化改造技術錨點深度解析

執行摘要 本文旨在深入剖析四項關鍵的低成本自動化技術,這些技術為工業轉型提供了顯著的運營和經濟效益。文章將提供實用且深入的指導,涵蓋老舊設備聯網、AGV車隊優化、空壓機系統智能能耗管控以及此類項目投資回報率(ROI)的嚴謹…

Oracle — 數據管理

介紹 Oracle數據庫作為全球領先的關系型數據庫管理系統,其數據管理能力以高效性、安全性和智能化為核心。系統通過多維度技術實現海量數據的存儲與實時處理,支持高并發事務操作與復雜分析查詢,滿足企業關鍵業務需求。在安全領域,O…

【PhysUnits】3.3 SI 基礎量綱單位(units/base.rs)

一、源碼 這段代碼定義了一系列基礎物理量綱的類型別名,并使用標記 trait Canonical 來表示它們是國際單位制(SI)中的基本單位。 use crate::Dimension; use typenum::{P1, Z0};/// 標記特質,表示基礎量綱單位 pub trait Canoni…

硬件實操技巧記錄

本篇自用,防止自己忘記 焊接技巧 一般都是隨機電烙鐵錫膏組合。 拆電阻時,電烙鐵放在電阻上,加錫膏,這個時候熔點會降低,電阻更容易掉下來,用電烙鐵帶走;焊電阻時,一端點錫膏&…

13.thinkphp的Session和cookie

一.Session 1. 在使用Session之前,需要開啟初始化,在中間件文件middleware.php; // Session 初始化 \think\middleware\SessionInit::class 2. TP6.0不支持原生$_SESSION的獲取方式,也不支持session_開頭的函數&…