[單master節點k8s部署]17.監控系統構建(二)Prometheus安裝

prometheus server安裝

創建sa賬號,對prometheus server進行授權。因為Prometheus是安裝在pod里面,以pod的形式去運行的,因此需要創建sa,并對他做rbac授權。

apiVersion: v1
kind: ServiceAccount
metadata:name: monitornamespace: monitor-sa

然后進行角色綁定 。

kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor-sa --clusterrole=cluster-admin --serviceaccount=monitor-sa:monitor

?再把sa綁定到pod里。這里需要提前設置Prometheus的存儲目錄。在工作節點上進行操作mkdir /data,然后設置目錄權限chmod 777 /data。

Prometheus config

創建一個configMap存儲卷來存儲Prometheus的配置:

[root@master prometheus]# cat prometheus-cfg.yaml 
---
kind: ConfigMap
apiVersion: v1
metadata:labels:app: prometheusname: prometheus-confignamespace: monitor-sa
data:prometheus.yml: |global:scrape_interval: 15sscrape_timeout: 10sevaluation_interval: 1mscrape_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 
服務發現配置node_exporter

scrape_configs:
? ? - job_name: 'kubernetes-node'
? ? ? kubernetes_sd_configs:
? ? ? - role: node?

這一段是使用kubernetes的服務發現,使用node角色,它使用默認的kubelet提供的http端口來發現集群中每個node節點。

然后進行relabel:

relabel_configs:
? ? ? - source_labels: [__address__]
? ? ? ? regex: '(.*):10250'
? ? ? ? replacement: '${1}:9100'
? ? ? ? target_label: __address__
? ? ? ? action: replace
? ? ? - action: labelmap
? ? ? ? regex: __meta_kubernetes_node_label_(.+)

這個正則將前綴匹配,然后labelmap把前綴去掉,只用后面大家不一樣的標簽,這樣就能進行更細致的數據表示和分類

在 Kubernetes 集群中,Prometheus 通過服務發現獲取節點的默認地址和端口(通常是 node-ip:10250,這是 kubelet 的默認 HTTP 端口)。由于 Node Exporter 通常運行在節點的 9100 端口,因此我們使用重標簽配置(relabeling)將 node-ip:10250 替換為 node-ip:9100。這樣,Prometheus 就能從正確的端口(9100)抓取到 Node Exporter 的監控數據。?

cAdvisor配置?

?- 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

?cAdvisor是用來獲取容器內部信息的job,pormetheus通過上面job的服務發現,得到了所有node的cluster IP,但是這里Prometheus想通過kubernetes 的api代理來進行aAdvisor的訪問,kubernetes.default.svc:443 是 Kubernetes 集群內部訪問 Kubernetes API 服務器的服務地址。這個地址使用 Kubernetes 內部 DNS 解析,并且所有集群內部的 Pod 都可以通過這個地址訪問 API 服務器。所以用kubectl的新的url就是 https://kubernetes.default.svc:443/api/v1/nodes/xianchaomaster1/proxy/metrics/cadvisor,這個nodename可以隨著不同的node改變。

下面的代碼可以看到kubernetes的服務的端口就是443

[root@master prometheus]# kubectl get svc
NAME                TYPE           CLUSTER-IP      EXTERNAL-IP                            PORT(S)        AGE
client-svc          ExternalName   <none>          nginx-svc.nginx-ns.svc.cluster.local   80/TCP         6d8h
kubernetes          ClusterIP      10.96.0.1       <none>                                 443/TCP        18d
my-nginx-nodeport   NodePort       10.99.238.240   <none>                                 80:30380/TCP   7d
nginx               ClusterIP      None            <none>                                 80/TCP         3d
服務抓取?

- 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

如果一個service打上了“prometheus.io/scrape: true” 的標簽(source_labels),就是說這個service允許prometheus抓取。如果匹配到就保留,否則就丟棄。

- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
? ? ? ? action: replace
? ? ? ? target_label: __scheme__
? ? ? ? regex: (https?)

apiVersion: v1
kind: Service
metadata:name: my-service-httpannotations:prometheus.io/scheme: "http"
spec:ports:
...

抓取service使用的協議,并放在__scheme__這個標簽中保存,要么是http,要么是https。

- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
? ? ? ? action: replace
? ? ? ? target_label: __address__
? ? ? ? regex: ([^:]+)(?::\d+)?;(\d+)
? ? ? ? replacement: $1:$2

指定要使用的源標簽,分別是 __address____meta_kubernetes_service_annotation_prometheus_io_port。Prometheus 會將這兩個標簽的值用分號 ; 連接成一個字符串。例如,如果 __address__ 的值是 my-service:8080__meta_kubernetes_service_annotation_prometheus_io_port 的值是 9090,那么組合后的字符串就是 my-service:8080;9090。舉例如下:

apiVersion: v1
kind: Service
metadata:name: my-serviceannotations:prometheus.io/port: "8080"

然后使用正則來處理? my-service:8080;9090。第一個括號([^:]+)選擇冒號之前的部分為$0,這里為my-service,(?::\d+)?匹配冒號之后的數字,(?:)表示這是一個非捕獲組,+表示可以有多個數字,?表示這個組合整體是可選的,可以匹配 0 次或 1 次。;匹配一個封號,然后(\d+)匹配封號之后的數字,也就是9090。最后把target_label的地址變為my-service:9090

使用注釋和 relabel_configs 來調整端口的目的是為了確保 Prometheus 能夠靈活地適應不同服務和環境中暴露指標的端口變化。這種配置方式提高了 Prometheus 配置的靈活性和適應性,確保它能夠正確地抓取到所需的指標數據。?

同理,也可以抓取metrics的標簽,然后放入標簽_metrics_path__中,抓取namespace標簽,放入kubernetes_namespace標簽中。

配置deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheus-servernamespace: monitor-salabels:app: prometheusspec:replicas: 1selector:matchLabels:app: prometheuscomponent: servertemplate:metadata: labels:app: prometheuscomponent: serverannotations:prometheus.io/scrape: 'false'spec:nodeName: node1serviceAccountName: monitorcontainers:- name: prometheusimage: prom/prometheus:v2.2.1imagePullPolicy: IfNotPresentcommand:- prometheus- --config.file=/etc/prometheus/prometheus.yml- --storage.tsdb.path=/prometheus- --storage.tsdb.retention=720h- --web.enable-lifecycleports:- containerPort: 9090protocol: TCPvolumeMounts:- name: prometheus-configmountPath: /etc/prometheus/prometheus.ymlsubPath: prometheus.yml- name: prometheus-storage-volumemountPath: /prometheus/volumes:- name: prometheus-configconfigMap:name: prometheus-configitems:- key: prometheus.ymlpath: prometheus.ymlmode: 0644- name: prometheus-storage-volumehostPath:path: /datatype: Directory

?command:
? ? ? ? - prometheus
? ? ? ? - --config.file=/etc/prometheus/prometheus.yml
? ? ? ? - --storage.tsdb.path=/prometheus
? ? ? ? - --storage.tsdb.retention=720h
? ? ? ? - --web.enable-lifecycle

這些命令是容器啟動時執行的,其中第一行Prometheus并不是一個命令,而是一個可執行文件的名稱,首先檢查這個文件是否存在在鏡像中,然后執行的過程中傳遞參數。

?volumes:
? ? ? - name: prometheus-config
? ? ? ? configMap:
? ? ? ? ? name: prometheus-config
? ? ? ? ? items:
? ? ? ? ? - key: prometheus.yml
? ? ? ? ? ? path: prometheus.yml
? ? ? ? ? ? mode: 0644

這個代表將prometheus config面的一個配置文件(有可能會包含多個配置文件)掛載起來

權限模式由四個數字組成:

  • 第一個數字(通常省略):設置特殊權限(Setuid、Setgid、Sticky 位)。
  • 第二個數字:所有者權限。
  • 第三個數字:組權限。
  • 第四個數字:其他用戶權限。

每個數字代表三個權限位(讀、寫、執行):

  • 4:讀權限(r)
  • 2:寫權限(w)
  • 1:執行權限(x)

例如,0644 分解為:

  • 0:沒有設置特殊權限。
  • 6:所有者有讀寫權限(4+2)。
  • 4:組有讀取權限。
  • 4:其他用戶有讀取權限。
/data目錄權限錯誤

目錄權限錯誤:第一次運行的時候,忘記給/data更改權限,導致container嘗試在 /prometheus 目錄中打開數據庫文件時遇到了權限拒絕錯誤。

[root@master prometheus]# kubectl logs prometheus-server-777db64bdb-cth6b -n monitor-sa
level=info ts=2024-07-04T02:45:46.985182Z caller=main.go:220 msg="Starting Prometheus" version="(version=2.2.1, branch=HEAD, revision=bc6058c81272a8d938c05e75607371284236aadc)"
level=info ts=2024-07-04T02:45:46.985233192Z caller=main.go:221 build_context="(go=go1.10, user=root@149e5b3f0829, date=20180314-14:15:45)"
level=info ts=2024-07-04T02:45:46.985248736Z caller=main.go:222 host_details="(Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 prometheus-server-777db64bdb-cth6b (none))"
level=info ts=2024-07-04T02:45:46.985260941Z caller=main.go:223 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2024-07-04T02:45:46.986875199Z caller=main.go:504 msg="Starting TSDB ..."
level=info ts=2024-07-04T02:45:46.98689516Z caller=web.go:382 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2024-07-04T02:45:46.986988203Z caller=main.go:398 msg="Stopping scrape discovery manager..."
level=info ts=2024-07-04T02:45:46.987001517Z caller=main.go:411 msg="Stopping notify discovery manager..."
level=info ts=2024-07-04T02:45:46.987008714Z caller=main.go:432 msg="Stopping scrape manager..."
level=info ts=2024-07-04T02:45:46.987018357Z caller=manager.go:460 component="rule manager" msg="Stopping rule manager..."
level=info ts=2024-07-04T02:45:46.987029598Z caller=manager.go:466 component="rule manager" msg="Rule manager stopped"
level=info ts=2024-07-04T02:45:46.987037473Z caller=notifier.go:512 component=notifier msg="Stopping notification manager..."
level=info ts=2024-07-04T02:45:46.987047143Z caller=main.go:573 msg="Notifier manager stopped"
level=info ts=2024-07-04T02:45:46.98703874Z caller=main.go:407 msg="Notify discovery manager stopped"
level=info ts=2024-07-04T02:45:46.98702453Z caller=main.go:394 msg="Scrape discovery manager stopped"
level=info ts=2024-07-04T02:45:46.98703596Z caller=main.go:426 msg="Scrape manager stopped"
level=error ts=2024-07-04T02:45:46.987764472Z caller=main.go:582 err="Opening storage failed open DB in /prometheus: open /prometheus/243953157: permission denied"
level=info ts=2024-07-04T02:45:46.987791139Z caller=main.go:584 msg="See you next time!"
創建service

創建Prometheus的service,從而可以從集群外訪問

[root@master prometheus]# cat prometheus-svc.yaml 
apiVersion: v1
kind: Service
metadata:name: prometheusnamespace: monitor-salabels:app: prometheus
spec:type: NodePortports:- port: 9090targetPort: 9090protocol: TCPselector:app: prometheuscomponent: server

隨后查看創建的svc,發現匹配了端口

[root@master prometheus]# kubectl get svc -n monitor-sa
NAME         TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
prometheus   NodePort   10.101.118.2   <none>        9090:32331/TCP   24s

隨后在瀏覽器上打開,訪問成功

查看target里面的內容:

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/39926.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/39926.shtml
英文地址,請注明出處:http://en.pswp.cn/web/39926.shtml

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

相關文章

k8s-第九節-命名空間

命名空間 如果一個集群中部署了多個應用&#xff0c;所有應用都在一起&#xff0c;就不太好管理&#xff0c;也可以導致名字沖突等。 我們可以使用 namespace 把應用劃分到不同的命名空間&#xff0c;跟代碼里的 namespace 是一個概念&#xff0c;只是為了劃分空間。 # 創建命…

LeetCode熱題100刷題4:76. 最小覆蓋子串、239. 滑動窗口最大值、53. 最大子數組和、56. 合并區間

76. 最小覆蓋子串 滑動窗口解決字串問題。 labuladong的算法小抄中關于滑動窗口的算法總結&#xff1a; class Solution { public:string minWindow(string s, string t) {unordered_map<char,int> need,window;for(char c : t) {need[c];}int left 0, right 0;int …

2.8億東亞五國建筑數據分享

數據是GIS的血液&#xff01; 我們現在為你分享東亞5國的2.8億條建筑輪廓數據&#xff0c;該數據包括中國、日本、朝鮮、韓國和蒙古5個東亞國家完整、高質量的建筑物輪廓數據&#xff0c;你可以在文末查看領取方法。 數據介紹 雖然開源的全球的建筑數據已經有微軟的建筑數據…

elementUI中table組件固定列時會渲染兩次模板內容問題

今天在使用elementUI的table組件時&#xff0c;由于業務需要固定表格的前幾項列&#xff0c;然后獲取表格對象時發現竟然有兩個對象。 查閱資料發現&#xff0c;elementUI的固定列的實現原理是將兩個表格拼裝而成&#xff0c;因此獲取的對象也是兩個。對于需要使用對象的方法的…

vxe-table的序號一樣

使用vxe-table的時候&#xff0c;有的時候會出現序號相同的現象&#xff0c;這種現象一般出現在我們后面自己添加的行中&#xff0c;就像這種 此時的這三個序號是相同的&#xff0c;我來說一下原因&#xff0c;這是在添加新的一行的時候&#xff0c;有的時候數據很多&#xff0…

Mac 運行 Windows 軟件,Parallels Desktop 19和 CrossOver 24全面對比

Parallels Desktop 和 CrossOver 都是能滿足你「在 Mac 上運行 Windows 軟件」需求的工具。可能很多人都已經知道 Parallels Desktop 是「虛擬機」&#xff0c;但 CrossOver 其實并不是「虛擬機」。這兩款軟件有相同的作用&#xff0c;但由于實現原理的不同&#xff0c;兩者也有…

系統提示我未定義與 ‘double‘ 類型的輸入參數相對應的函數 ‘finverse‘,如何解決?

&#x1f3c6;本文收錄于「Bug調優」專欄&#xff0c;主要記錄項目實戰過程中的Bug之前因后果及提供真實有效的解決方案&#xff0c;希望能夠助你一臂之力&#xff0c;幫你早日登頂實現財富自由&#x1f680;&#xff1b;同時&#xff0c;歡迎大家關注&&收藏&&…

Kubernetes 部署簡單的應用

Kubernetes 部署簡單的應用 Kubernetes 是一個強大的容器編排平臺&#xff0c;它可以幫助我們自動化應用程序的部署、擴展和管理。在本期文章中&#xff0c;我們將學習如何使用 Kubernetes 部署一個簡單的應用程序。 1. 環境準備 確保你已經安裝了 Kubernetes 集群&#xff…

【python模塊】argparse

文章目錄 argparse模塊介紹基本用法add_argument() argparse模塊介紹 argparse 模塊是 Python 標準庫中的一個用于編寫用戶友好的命令行接口&#xff08;CLI&#xff09;的模塊。它允許程序定義它所需要的命令行參數&#xff0c;然后 argparse 會自動從 sys.argv 解析出那些參…

TCP粘包解決方法

一. 產生原因及解決方法 產生原因&#xff1a;TCP是面向連接、基于字節流的協議&#xff0c;其無邊界標記。當服務端處理速度比不其接收速度時&#xff0c;就很容易產生粘包現象。 解決方法&#xff1a;目前主要有兩種解決方法&#xff0c;一個是在內容中添加分割標識&#xf…

人臉識別考勤系統

人臉識別考勤系統是一種利用生物識別技術進行自動身份驗證的現代解決方案&#xff0c;它通過分析和比對人臉特征來進行員工的出勤記錄。這種系統不僅提升了工作效率&#xff0c;還大大減少了人為錯誤和欺詐行為的可能性。 一、工作原理 人臉識別考勤系統的核心在于其生物識別…

深入剖析Python中的Pandas庫:通過實戰案例全方位解讀數據清洗與預處理藝術

引言 隨著大數據時代的到來&#xff0c;數據的質量直接影響到最終分析結果的可靠性和有效性。在這個背景下&#xff0c;Python憑借其靈活強大且易于上手的特點&#xff0c;在全球范圍內被廣泛應用于數據科學領域。而在Python的數據處理生態中&#xff0c;Pandas庫無疑是最耀眼…

高級策略:解讀 SQL 中的復雜連接

了解基本連接 在深入研究復雜連接之前&#xff0c;讓我們先回顧一下基本連接的基礎知識。 INNER JOIN&#xff1a;根據指定的連接條件檢索兩個表中具有匹配值的記錄。LEFT JOIN&#xff1a;從左表檢索所有記錄&#xff0c;并從右表中檢索匹配的記錄&#xff08;如果有&#x…

管道支架安裝

工程結構施工完畢后&#xff0c;系統管道安裝完畢后的第一步任務就是管道支架的制作安裝&#xff0c;作為對管道固定和承重作用至關重要的支、托、吊架&#xff0c;有些項目部在施工中卻往往因為對它們的重要性認識不足&#xff0c;因存在僥幸心里或經驗主義&#xff0c;導致支…

NIO為什么會導致CPU100%?

1. Java IO 類型概覽 BIO&#xff1a;阻塞I/O&#xff0c;每個連接一個線程&#xff0c;簡單但遇到高并發時性能瓶頸明顯。NIO&#xff1a;非阻塞I/O&#xff0c;JDK 1.4引入&#xff0c;一個線程處理多個IO操作&#xff0c;提高資源利用率和系統吞吐量。AIO&#xff1a;異步I…

技術探索:利用Python庫wxauto實現Windows微信客戶端的全面自動化管理

項目地址&#xff1a;github-wxauto 點擊即可訪問 項目官網&#xff1a;wxauto 點擊即可訪問 &#x1f602;什么是wxauto? wxauto 是作者在2020年開發的一個基于 UIAutomation 的開源 Python 微信自動化庫&#xff0c;最初只是一個簡單的腳本&#xff0c;只能獲取消息和發送…

kpatch Patch Author Guide

kpatch Patch Author Guide Because kpatch-build is relatively easy to use, it can be easy to assume that a successful patch module build means that the patch is safe to apply. But in fact that’s a very dangerous assumption. 由于 kpatch-build 比較容易使用…

精通Spring Cloud: Spring Cloud Config面試題詳解及參考答案(3萬字長文)

解釋Spring Cloud Config的基本功能和它在微服務架構中的作用 Spring Cloud Config是一個用于集中管理和外部化配置的工具。其核心功能在于允許開發者將配置從代碼中分離出來,放置于一個中央存儲庫中,從而簡化了配置管理,提高了應用程序的可維護性和靈活性。在微服務架構中…

論文的3個創新點方向

1、數據分析創新 通過對現有數據的分析&#xff0c;發現新的模式或趨勢&#xff0c;提出新的假設或理論的方法。隨著大數據和人工智能技術的發展&#xff0c;數據分析在科學研究中也有很多的創新。 可以通過實驗、調查、模擬、現場等方式收集相關數據。數據的質量和數量是數據…

掌握MySQL基礎命令:數據更新操作詳細操作(數據的增刪改)

MySQL數據修改是指使用SQL語句&#xff08;如UPDATE、INSERT、DELETE&#xff09;對數據庫表中的數據進行更改、添加或刪除的操作&#xff0c;常見的操作包括更新表中的記錄、插入新記錄以及刪除現有記錄 。 一、數據插入 1插入完整的數據記錄 2插入非完整的數據記錄 3插入多…