外部 prometheus監控k8s集群資源(pod、CPU、service、namespace、deployment等)

prometheus監控k8s集群資源

  • 一,通過CADvisior 監控pod的資源狀態
    • 1.1 授權外邊用戶可以訪問prometheus接口。
    • 1.2 獲取token保存
    • 1.3 配置prometheus.yml 啟動并查看狀態
    • 1.4 Grafana 導入儀表盤
  • 二,通過kube-state-metrics 監控k8s資源狀態
    • 2.1 部署 kube-state-metrics
    • 2.2 配置prometheus.yml
    • 2.3 Grafana 導入儀表盤
    • 2.4 Grafana沒有數據,添加路由轉發

二進制安裝的prometheus,監控k8s集群信息。

監控指標實現方式舉例
Pod資源利用率cAdvisor容器CPU、內存利用率
K8s資源狀態kube-state-metricscontroller控制器、Node、Namespace、Pod、ReplicaSet、service等

一,通過CADvisior 監控pod的資源狀態

1.1 授權外邊用戶可以訪問prometheus接口。

apiVersion: v1
kind: ServiceAccount
metadata:name: prometheusnamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:name: prometheus
rules:
- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch
- apiGroups:- "extensions"resources:- ingressesverbs:- get- list- watch
- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get
- nonResourceURLs:- /metricsverbs:- get
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:name: prometheus
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheus
subjects:
- kind: ServiceAccountname: prometheusnamespace: kube-system
kubectl apply -f rbac.yaml

1.2 獲取token保存

kubectl get secrets -n kube-system |grep prometheus #查看toekn name
name:prometheus-token-vgxhckubectl describe secret prometheus-token-vgxhc -n kube-system > token.k8s
#kubectl get secrets -n kube-system -o yaml prometheus-token-vgxhc |grep token
scp token.k8s prometheus #拷貝到prometheus服務器prometheus的目錄下

我的token放在 /opt/monitor/prometheus/token.k8s

1.3 配置prometheus.yml 啟動并查看狀態

vim prometheus.yml

  - job_name: kubernetes-nodes-cadvisormetrics_path: /metricsscheme: httpskubernetes_sd_configs:- role: nodeapi_server: https://172.18.0.0:6443bearer_token_file: /opt/monitor/prometheus/token.k8s tls_config:insecure_skip_verify: truebearer_token_file: /opt/monitor/prometheus/token.k8s tls_config:insecure_skip_verify: truerelabel_configs:# 將標簽(.*)作為新標簽名,原有值不變- action: labelmapregex: __meta_kubernetes_node_label_(.*)# 修改NodeIP:10250為APIServerIP:6443- action: replaceregex: (.*)source_labels: ["__address__"]target_label: __address__replacement: 172.18.0.0:6443# 實際訪問指標接口 https://NodeIP:10250/metrics/cadvisor 這個接口只能APISERVER訪問,故此重新標記標簽使用APISERVER代理訪問- action: replacesource_labels: [__meta_kubernetes_node_name]target_label: __metrics_path__regex: (.*)replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor 
./promtool check config prometheus.yml 
重啟prometheus 或 kill -HUP PrometheusPid

在prometheus的target頁面查看
http://172.18.0.0:9090
在這里插入圖片描述

1.4 Grafana 導入儀表盤

導入3119 儀表盤
在這里插入圖片描述
在這里插入圖片描述完成pod資源監控

二,通過kube-state-metrics 監控k8s資源狀態

2.1 部署 kube-state-metrics

apiVersion: v1
kind: ServiceAccount
metadata:name: kube-state-metricsnamespace: kube-systemlabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: kube-state-metricslabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
rules:
- apiGroups: [""]resources:- configmaps- secrets- nodes- pods- services- resourcequotas- replicationcontrollers- limitranges- persistentvolumeclaims- persistentvolumes- namespaces- endpointsverbs: ["list", "watch"]
- apiGroups: ["apps"]resources:- statefulsets- daemonsets- deployments- replicasetsverbs: ["list", "watch"]
- apiGroups: ["batch"]resources:- cronjobs- jobsverbs: ["list", "watch"]
- apiGroups: ["autoscaling"]resources:- horizontalpodautoscalersverbs: ["list", "watch"]
- apiGroups: ["networking.k8s.io", "extensions"]resources:- ingresses verbs: ["list", "watch"]
- apiGroups: ["storage.k8s.io"]resources:- storageclasses verbs: ["list", "watch"]
- apiGroups: ["certificates.k8s.io"]resources:- certificatesigningrequestsverbs: ["list", "watch"]
- apiGroups: ["policy"]resources:- poddisruptionbudgets verbs: ["list", "watch"]---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:name: kube-state-metrics-resizernamespace: kube-systemlabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
rules:
- apiGroups: [""]resources:- podsverbs: ["get"]
- apiGroups: ["extensions","apps"]resources:- deploymentsresourceNames: ["kube-state-metrics"]verbs: ["get", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1 
kind: ClusterRoleBinding
metadata:name: kube-state-metricslabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: kube-state-metrics
subjects:
- kind: ServiceAccountname: kube-state-metricsnamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: kube-state-metricsnamespace: kube-systemlabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: kube-state-metrics-resizer
subjects:
- kind: ServiceAccountname: kube-state-metricsnamespace: kube-system---apiVersion: apps/v1
kind: Deployment
metadata:name: kube-state-metricsnamespace: kube-systemlabels:k8s-app: kube-state-metricskubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcileversion: v1.3.0
spec:selector:matchLabels:k8s-app: kube-state-metricsversion: v1.3.0replicas: 1template:metadata:labels:k8s-app: kube-state-metricsversion: v1.3.0annotations:scheduler.alpha.kubernetes.io/critical-pod: ''spec:priorityClassName: system-cluster-criticalserviceAccountName: kube-state-metricscontainers:- name: kube-state-metricsimage: harbor.cpit.com.cn/monitor/kube-state-metrics:v1.8.0ports:- name: http-metricscontainerPort: 8080- name: telemetrycontainerPort: 8081readinessProbe:httpGet:path: /healthzport: 8080initialDelaySeconds: 5timeoutSeconds: 5- name: addon-resizerimage: harbor.cpit.com.cn/monitor/addon-resizer:1.8.6resources:limits:cpu: 1000mmemory: 500Mirequests:cpu: 1000mmemory: 500Mienv:- name: MY_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: MY_POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespacevolumeMounts:- name: config-volumemountPath: /etc/configcommand:- /pod_nanny- --config-dir=/etc/config- --container=kube-state-metrics- --cpu=100m- --extra-cpu=1m- --memory=100Mi- --extra-memory=2Mi- --threshold=5- --deployment=kube-state-metricsvolumes:- name: config-volumeconfigMap:name: kube-state-metrics-config
---
# Config map for resource configuration.
apiVersion: v1
kind: ConfigMap
metadata:name: kube-state-metrics-confignamespace: kube-systemlabels:k8s-app: kube-state-metricskubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
data:NannyConfiguration: |-apiVersion: nannyconfig/v1alpha1kind: NannyConfiguration---apiVersion: v1
kind: Service
metadata:name: kube-state-metricsnamespace: kube-systemlabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcilekubernetes.io/name: "kube-state-metrics"annotations:prometheus.io/scrape: 'true'
spec:ports:- name: http-metricsport: 8080targetPort: http-metricsprotocol: TCP- name: telemetryport: 8081targetPort: telemetryprotocol: TCPselector:k8s-app: kube-state-metrics

部署

kubectl apply -f kube-state-metrics.yaml
kubectl get pods -n kube-system

在這里插入圖片描述
pod的正常運行

2.2 配置prometheus.yml

- job_name: kubernetes-service-endpointskubernetes_sd_configs:- role: endpointsapi_server: https://192.168.0.0:6443bearer_token_file: /opt/monitor/prometheus/token.k8stls_config:insecure_skip_verify: truebearer_token_file: /opt/monitor/prometheus/token.k8stls_config:insecure_skip_verify: trueService沒配置注解prometheus.io/scrape的不采集relabel_configs:- action: keepregex: truesource_labels:- __meta_kubernetes_service_annotation_prometheus_io_scrape重命名采集目標協議- action: replaceregex: (https?)source_labels:- __meta_kubernetes_service_annotation_prometheus_io_schemetarget_label: __scheme__重命名采集目標指標URL路徑- action: replaceregex: (.+)source_labels:- __meta_kubernetes_service_annotation_prometheus_io_pathtarget_label: __metrics_path__重命名采集目標地址- action: replaceregex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2source_labels:- __address__- __meta_kubernetes_service_annotation_prometheus_io_porttarget_label: __address__將K8s標簽(.*)作為新標簽名,原有值不變- action: labelmapregex: __meta_kubernetes_service_label_(.+)生成命名空間標簽- action: replacesource_labels:- __meta_kubernetes_namespacetarget_label: kubernetes_namespace生成Service名稱標簽- action: replacesource_labels:- __meta_kubernetes_service_nametarget_label: kubernetes_service_name
./promtool check config prometheus.yml 
重啟prometheus 或 kill -HUP PrometheusPid

在prometheus的target頁面查看
http://172.18.0.0:9090
在這里插入圖片描述

2.3 Grafana 導入儀表盤

Grafana導入k8s集群資源對象監控儀表盤 6417

在這里插入圖片描述
完成k8s集群資源對象監控儀表盤監控

2.4 Grafana沒有數據,添加路由轉發

ip route
ip route add 172.40.0.0/16 via 172.18.2.30 dev eth0
ip route

#172.40.1.208:kube-state-metrics pod 集群內部ip
#172.18.2.30:k8s master 節點ip

然后在查看Grafana儀表盤。

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

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

相關文章

手把手教你編寫LoadRunner腳本

編寫 LoadRunner 腳本需要熟悉腳本語言、業務場景、參數化技術、斷言和事務等基礎知識。 在實際編寫時,可以根據具體測試需求,結合實際情況進行合理的配置和調整。 基本步驟 創建腳本 在 LoadRunner 的 Controller 模塊中,創建一個新的測…

linux centos上安裝python3.11.x詳細完整教程

一. 安裝步驟 注意: 1、安裝python3.11的其他版本替換下面的版本信息即可。(如想安裝3.11.5將案例中的3.11.0替換成3.11.5即可) #下載最新的軟件安裝包 wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz#解壓縮安裝包 tar -xzf Python-3.11.0.tg…

gRPC之gRPC負載均衡(客戶端負載均衡)(etcd)

1、gRPC負載均衡(客戶端負載均衡)(etcd) 本篇將基于etcd的服務發現前提下,介紹如何實現gRPC客戶端負載均衡。 1.1 gRPC負載均衡 gRPC官方文檔提供了關于gRPC負載均衡方案Load Balancing in gRPC https://github.com/grpc/grpc/blob/master/doc/load-balancing.m…

Jackson無縫替換Fastjson

目錄 文章目錄 一,Fastjson到Jackson的替換方案方案代碼序列化反序列化通過key獲取某種類型的值類型替換 二,Springboot工程中序列化的使用場景三,SpringMVC框架中的Http消息轉換器1,原理:2,自定義消息轉換…

什么是mvc原理是什么

MVC是一種設計模式,它將應用程序分為三個部分:模型(Model)、視圖(View)和控制器(Controller)。 模型(Model)表示應用程序的數據結構,包括與數據相…

常用腳本-持續更新(文件重命名、視頻抽幀、拆幀)

所有代碼位置:Learning-Notebook-Codes/Python/常用腳本 1. 文件重命名 1.1 說明 腳本路徑:codes/files_rename.py腳本說明:可以自動重命名某個文件夾下指定類型的文件。 修改前文件名稱: img1.jpg修改后文件名稱: Le0v1n-20231123-X-0001…

python-opencv在圖片中繪制各種圖形

python-opencv在圖片中繪制各種圖形 1.繪制直線 2.繪制矩形 3.繪制圓 4.繪制橢圓 5.繪制多邊形 6.嵌入文字 實現代碼都在下面了,代碼中參數做了簡單注釋 import copy import math import matplotlib.pyplot as plt import matplotlib as mpl import numpy a…

window非gui形式運行jmeter腳本

配置jmeter環境 新增1個環境變量: JMETER_HOMED:\Tools\apache-jmeter-5.0 【jmeter文件夾】 編輯CLASSPATH: CLASSPATH后面加上 %JMETER_HOME%\lib\ext\ApacheJMeter_core.jar; %JMETER_HOME%\lib\jorphan.jar; 編輯path: path后面加上 %JM…

二次開發問題匯總【C#】

1未將對象引用到實例。 接口函數的參數不對。解決辦法【用fixed去限制數組長度】 unsafe public struct VCI_BOARD_INFO {public UInt16 hw_Version;public UInt16 fw_Version;public UInt16 dr_Version;public UInt16 in_Version;public UInt16 irq_Num;public byte can_Num;…

C語言眾數問題(ZZULIOJ1201:眾數問題)

題目描述 給定含有n個元素的多重集合S,每個元素在S中出現的次數稱為該元素的重數。多重集S中重數最大的元素稱為眾數。 例如,S{1,2,2,2,3,5}。多重集S的眾數是2,其重數為3。 編程任務…

前端學習--React(3)

一、Redux 集中狀態管理工具,不需要react即可使用,每個store的數據都是獨立于組件之外的 vue小鏈接:vuex/pinia 基本使用 Redux將數據修改流程分成三個概念,state、action和reducer state - 一個對象 存放我們管理的數據狀態 a…

1688API如何獲取商品詳情信息(關鍵詞搜索商品列表),1688API接口開發系列

1688商品詳情接口是指1688平臺提供的API接口,用于獲取商品詳情信息。通過該接口,您可以獲取到商品的詳細信息,包括商品標題、價格、庫存、描述、圖片等。 要使用1688商品詳情接口,您需要先申請1688的API權限,并獲取ac…

UML建模圖文詳解教程01——Enterprise Architect安裝與使用

版權聲明 本文原創作者:谷哥的小弟作者博客地址:http://blog.csdn.net/lfdfhl Enterprise Architect概述 官方網站:https://www.sparxsystems.cn/products/ea/;圖示如下: Enterprise Architect是一個全功能的、基于…

Python入門02 算術運算符及優先級

目錄 1 REPL2 啟動3 算術運算符4 算術運算符的優先級5 清除屏幕總結 上一節我們安裝了Python的開發環境,本節我們介紹一下REPL的概念 1 REPL 首先解釋一下python執行代碼的一個交互環境的定義: Python REPL(Read-Eval-Print Loop&#xff0c…

靠這份求職指南找工作,穩了!

大家好,我是魚皮。為了幫助朋友們更好的準備秋招,我們精心匯總整理了 編程導航星球 內魚友反饋的 200 多個高頻求職問題和 150 多篇面經、以及最新秋招企業投遞信息表,解答大家的求職困惑。 一、最新秋招投遞信息表 目前已匯總整理了 600 多家…

C百題--3.求未知數

1.問題描述 一個正整數&#xff0c;它加上100后是一個完全平方數&#xff0c;再加上168又是一個完全平方數&#xff0c;請問該數是多少&#xff1f; 2.解決思路 遍歷這個數&#xff0c;讓其從1開始&#xff0c;到100000結束 3.代碼實現 #include<stdio.h> #include&…

2.C語言--鏈表-頭插、頭刪、尾插、尾刪、查找、插入和刪除

文章目錄 簡介動態順序表結構體1.頭插功能2.頭刪功能3.尾插功能4.尾刪功能5.查找功能6.插入功能6.1 指定位置之&#xff08;前&#xff09;去插入一個節點6.2 指定位置之&#xff08;后&#xff09;去插入一個節點 7.刪除功能7.1 刪除指定位置的數據-時間復雜度O(N)7.2 刪除指定…

配置hikari數據庫連接池時多數據源不生效

1.原始配置&#xff0c;改造前&#xff1a; spring:# 配置數據源信息datasource:dynamic:#設置默認的數據源或者數據源組,默認值即為masterprimary: masterstrict: truedatasource:#這里采用了配置文件取值的方式&#xff0c;可以直接替換為數據庫連接master:url: jdbc:postgr…

【LLS-Player】音視頻幀的回調過程

RtdSinkInterface 實現者用于從SDK獲取音視頻幀 class RtdSinkInterface {public:virtual ~RtdSinkInterface() = default;virtual void OnAudioFrame(const RtdAudioFrame& fra