k8s學習筆記- 部署prometheus


1.Prometheus概述

Prometheus是一個開源監控系統,它前身是SoundCloud的警告工具包。從2012年開始,許多公司和組織開始使用Prometheus。

該項目的開發人員和用戶社區非常活躍,越來越多的開發人員和用戶參與到該項目中。

目前它是一個獨立的開源項目,且不依賴與任何公司。 為了強調這點和明確該項目治理結構,Prometheus在2016年繼Kurberntes之后,加入了Cloud Native Computing Foundation。

特征:

Prometheus的主要特征有:

  1. 多維度數據模型
  2. 靈活的查詢語言
  3. 不依賴分布式存儲,單個服務器節點是自主的
  4. 以HTTP方式,通過pull模型拉去時間序列數據
  5. 也通過中間網關支持push模型
  6. 通過服務發現或者靜態配置,來發現目標服務對象
  7. 支持多種多樣的圖表和界面展示,grafana也支持它

組件

Prometheus生態包括了很多組件,它們中的一些是可選的:

  1. 主服務Prometheus Server負責抓取和存儲時間序列數據
  2. 客戶庫負責檢測應用程序代碼
  3. 支持短生命周期的PUSH網關
  4. 基於Rails/SQL儀表盤構建器的GUI
  5. 多種導出工具,可以支持Prometheus存儲數據轉化為HAProxy、StatsD、Graphite等工具所需要的數據存儲格式
  6. 警告管理器
  7. 命令行查詢工具
  8. 其他各種支撐工具

多數Prometheus組件是Go語言寫的,這使得這些組件很容易編譯和部署。

如上圖,每個被監控的主機都可以通過專用的exporter程序提供輸出監控數據的接口,並等待Prometheus服務器周期性的進行數據抓取。如果存在告警規則,則抓取到數據之后會根據規則進行計算,滿足告警條件則會生成告警,並發送到Alertmanager完成告警的匯總和分發。當被監控的目標有主動推送數據的需求時,可以以Pushgateway組件進行接收並臨時存儲數據,然后等待Prometheus服務器完成數據的采集。

任何被監控的目標都需要事先納入到監控系統中才能進行時序數據采集、存儲、告警和展示,監控目標可以通過配置信息以靜態形式指定,也可以讓Prometheus通過服務發現的機制進行動態管理。下面是組件的一些解析:

  • 監控代理程序:如node_exporter:收集主機的指標數據,如平均負載、CPU、內存、磁盤、網絡等等多個維度的指標數據。
  • kubelet(cAdvisor):收集容器指標數據,也是K8S的核心指標收集,每個容器的相關指標數據包括:CPU使用率、限額、文件系統讀寫限額、內存使用率和限額、網絡報文發送、接收、丟棄速率等等。
  • API Server:收集API Server的性能指標數據,包括控制隊列的性能、請求速率和延遲時長等等
  • etcd:收集etcd存儲集群的相關指標數據
  • kube-state-metrics:該組件可以派生出k8s相關的多個指標數據,主要是資源類型相關的計數器和元數據信息,包括制定類型的對象總數、資源限額、容器狀態以及Pod資源標簽系列等。

Prometheus 能夠 直接 把 Kubernetes API Server 作為 服務 發現 系統 使用 進而 動態 發現 和 監控 集群 中的 所有 可被 監控 的 對象。

這里 需要 特別 說明 的 是, Pod 資源 需要 添加 下列 注解 信息 才 能被 Prometheus 系統 自動 發現 並 抓取 其 內建 的 指標 數據。

  • 1) prometheus. io/ scrape: 用於 標識 是否 需要 被 采集 指標 數據, 布爾 型 值, true 或 false。
  • 2) prometheus. io/ path: 抓取 指標 數據 時 使用 的 URL 路徑, 一般 為/ metrics。
  • 3) prometheus. io/ port: 抓取 指標 數據 時 使 用的 套 接 字 端口, 如 8080。

另外, 僅 期望 Prometheus 為 后端 生成 自定義 指標 時 僅 部署 Prometheus 服務器 即可, 它 甚至 也不 需要 數據 持久 功能。 但 若要 配置 完整 功能 的 監控 系統, 管理員 還需 要在 每個 主機 上 部署 node_ exporter、 按 需 部署 其他 特有 類型 的 exporter 以及 Alertmanager。

Prometheus服務,可以直接通過目標拉取數據,或者間接地通過中間網關拉取數據。它在本地存儲抓取的所有數據,並通過一定規則進行清理和整理數據,並把得到的結果存儲到新的時間序列中,PromQL和其他API可視化地展示收集的數據

適用場景

Prometheus在記錄純數字時間序列方面表現非常好。它既適用於面向服務器等硬件指標的監控,也適用於高動態的面向服務架構的監控。對於現在流行的微服務,Prometheus的多維度數據收集和數據篩選查詢語言也是非常的強大。

Prometheus是為服務的可靠性而設計的,當服務出現故障時,它可以使你快速定位和診斷問題。它的搭建過程對硬件和服務沒有很強的依賴關系。

不適用場景

Prometheus,它的價值在於可靠性,甚至在很惡劣的環境下,你都可以隨時訪問它和查看系統服務各種指標的統計信息。 如果你對統計數據需要100%的精確,它並不適用,例如:它不適用於實時計費系統

更對詳細介紹參考文章

https://www.kancloud.cn/cdh0805010118/prometheus/719339

https://github.com/prometheus

https://github.com/yunlzheng/prometheus-book

2.在K8S 是部署Prometheus

 

Prometheus可以采集其它各種指標,但是prometheus采集到的metrics並不能直接給k8s用,因為兩者數據格式不兼容,因此還需要另外一個組件(kube-state-metrics),

將prometheus的metrics數據格式轉換成k8s API接口能識別的格式,轉換以后,因為是自定義API,所以還需要用Kubernetes aggregator在主API服務器中注冊,以便直接通過/apis/來訪問。

 

文件清單:

 

node-exporter:prometheus的export,收集Node級別的監控數據
prometheus:監控服務端,從node-exporter拉數據並存儲為時序數據。
kube-state-metrics:將prometheus中可以用PromQL查詢到的指標數據轉換成k8s對應的數據
k8s-prometheus-adpater:聚合進apiserver,即一種custom-metrics-apiserver實現
開啟Kubernetes aggregator功能(參考上文metric-server)

 

這個適用於PrometheusKubernetes Customm Metrics Adapter是屬於Github上的k8s-prometheus-adapter項目提供的。其原理圖如下:

 

總結:

 prometheus本身就是一監控系統,也分為server端和agent端,server端從被監控主機獲取數據,而agent端需要部署一個node_exporter,主要用於數據采集和暴露節點的數據,那么 在獲取Pod級別或者是mysql等多種應用的數據,也是需要部署相關的exporter

我們可以通過PromQL的方式對數據進行查詢,但是由於本身prometheus屬於第三方的 解決方案,原生的k8s系統並不能對Prometheus的自定義指標進行解析,就需要借助於k8s-prometheus-adapter將這些指標數據查詢接口轉換為標准的Kubernetes自定義指標。

參考文章

https://yasongxu.gitbook.io/container-monitor/yi-.-kai-yuan-fang-an/di-1-zhang-cai-ji/cadvisor

由於官方的YAML部署方式需要使用到PVC,如果只是為了簡單的演示可以通過馬哥的方式部署

注意一點,安裝這個之前,必須已經安裝好metrics-server (詳細參考上一篇文章)

查考文檔:

https://www.cnblogs.com/linuxk/p/10582534.html

我這邊按照官方文檔安裝:

https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/prometheus 

下載下來相應的文檔

cd /data/k8s/prometheus

mkdir promtheus 

mkdir kube-state-metrics 

mkdir node-exporter

cd promtheus

for file in  prometheus-configmap.yaml prometheus-rbac.yaml prometheus-service.yaml prometheus-statefulset.yaml ;do wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/$file;done

cd  ../node-exporter

for file in  node-exporter-ds.yml node-exporter-service.yaml ;do wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/$file;done

cd ../kube-state-metrics

for file in  kube-state-metrics-deployment.yaml kube-state-metrics-rbac.yaml kube-state-metrics-service.yaml ;do wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/prometheus/$file;done

整個prometheus 我安裝在一個名稱空間,先創建個名稱空間prom 

 kubectl create ns prom 

部署node-export

最開始安裝的是node-export,它的作用是收集節點的數據,被prometheus采集的。

官方提供的node-export的yaml文件都是安裝在kube-system的名稱空間,所以需要修改下名稱空間為prom

還有一點注意,因為我們還要監控master相關的節點,所以最好在主節點安裝一個ds,可以給pod增加toleration(容忍主節點污點)

具體修改如圖

node-exporter-ds.yml 

1.#priorityClassName: system-node-critical #這行注釋,否則創建會報錯,具體的原因我還沒找到

2.master節點的污點容忍度,否則不會再master節點創建pod

tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master

3.備注:
1.為了讓容器里的node-exporter獲取到主機上的網絡、PID、IPC指標,這里設置了hostNetwork: true、hostPID: true、hostIPC: true,來與主機共用網絡、PID、IPC這三個namespace。
2.此處在Service的annotations中定義標注prometheus.io/scrape: 'true',表明該Service需要被Promethues發現並采集數據。

cd  node-exporter

kubectl apply -f .

[root@k8s-master k8s]# kubectl get pods -n prom -o wide |egrep node-expor
node-exporter-5hwcg 1/1 Running 0 19h 10.211.55.11 k8s-master <none> <none>
node-exporter-92rt2 1/1 Running 0 19h 10.211.55.12 k8s-node1 <none> <none>
node-exporter-ls72w 1/1 Running 0 19h 10.211.55.13 k8s-node2 <none> <none>

可以看到Master 也部署了Pod

部署prometheus

通過查看prometheus-statefulset.yaml文件內容可知 需要持久存儲數據的,官方給的yaml文件中需要設置一個16G的大小的pv

 

這里面涉及到存儲類名所以先注釋掉,當然名稱空間也改成prom 

#storageClassName: standard

#priorityClassName: system-cluster-critical #這個也注釋掉,要不啟動不起

我們這里使用的是NFS,創建一個PV 

在NFS 的服務器上面配置

mkdir  /data/prometheus

vim /etc/exports

/data/prometheus  10.0.0.0/8(rw,no_root_squash) 

service nfs restart

注意:node節點要執行  yum install nfs-utils  ,否則會出現掛載不上的情況,因為沒有nfs的文件類型

vim pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pvdata
  namespace: prom
  labels:
    name: pv01
spec:
  nfs:
    path: /data/prometheus
    server: k8s-nfs
  accessModes: ["ReadWriteOnce","ReadWriteMany"]
  capacity:
    storage: 20Gi

[root@master volumes]# kubectl get pv|egrep pvdata
pvdata 20Gi RWO,RWX Retain Available 20s
解決了存儲問題呢,下圖是主要修改的地方:

 

cd prometheus

[root@k8s-master prometheus]# kubectl apply -f .

[root@k8s-master prometheus]# kubectl get pod -n prom |egrep prom
prometheus-0 2/2 Running 0 22h

這里在說明一下,為了方便在外部查看prometheus-service的狀態,這里修改一下

prometheus-service.yaml

type: NodePort

 

[root@k8s-master prometheus]# kubectl get svc -n prom |egrep prometheus
prometheus NodePort 10.102.102.188 <none> 9090:32268/TCP 22h

prometheus 本身自己有web 頁面,其也有很多生成的查詢條件,也可以生產圖形數據,這里不做太多了解

具體配置文件參考下面文章:

https://github.com/1046102779/prometheus/blob/master/operating/configuration.md

部署kube-state-metrics 

這個組件的作用事將prometheus收集的數據,轉換成kubernetes 可以識別的數據類型

[root@ kube-state-metrics ]# kubectl get pods -n prom |egrep kube
kube-state-metrics-6f584f4b48-tsmm8 2/2 Running 0 4h26m

部署prometheus-adapter

 

其實k8s-prometheus-adapter既包含自定義指標,又包含核心指標,即如果按照了prometheus,且指標都采集完整,k8s-prometheus-adapter可以替代metrics server。

 

在1.6以上的集群中,k8s-prometheus-adapter可以適配autoscaling/v2的HPA

 

因為一般是部署在集群內,所以k8s-prometheus-adapter默認情況下,使用in-cluster的認證方式,以下是主要參數:

 

  • lister-kubeconfig: 默認使用in-cluster方式

  • metrics-relist-interval: 更新metric緩存值的間隔,最好大於等於Prometheus 的scrape interval,不然數據會為空

  • prometheus-url: 對應連接的prometheus地址

  • config: 一個yaml文件,配置如何從prometheus獲取數據,並與k8s的資源做對應,以及如何在api接口中展示。

 

這個組件的作用是整合收集的數據到api

自定義APIServer通常都要通過Kubernetes aggregator聚合到apiserver提供了一個APIServer服務,名為 custom-metrics-apiserver,提供的API組: custom.metrics.k8s.io,它是自定義指標API(custom.metrics.k8s.io)的實現

下面是git地址:

 https://github.com/DirectXMan12/k8s-prometheus-adapter

cd /data/prometheus/

git clone https://github.com/DirectXMan12/k8s-prometheus-adapter.git

cd /data/prometheus/k8s-prometheus-adapter/deploy/manifests

同樣修改的是名稱空間

 kubectl apply -f .

 kubectl get pods -n prom |egrep metrics-api
custom-metrics-apiserver-667fd4fffd-8fw98 0/1 ContainerCreating 0 3m7s

發現容器一直處於這個狀態,查看詳細內容可以看到下面的錯誤

kubectl describe pod custom-metrics-apiserver-667fd4fffd-8fw98 -n prom

Warning FailedMount 70s (x8 over 2m14s) kubelet, wan19 MountVolume.SetUp failed for volume "volume-serving-cert" : secret "cm-adapter-serving-certs" not found

通過查看文件 custom-metrics-apiserver-deployment.yaml

從上面該組件的deployment看出,它需要掛一個secret存儲卷,secret名為"cm-adapter-serving-certs",這個secret是一個證書,因此這里需要創建相應的證書和key,這個證書必須由k8s的kube-apiserver信任的CA簽發,因此直接用k8s的CA簽發。

創建這個secret :

[root@k8s-master pki]# (umask 077; openssl genrsa -out serving.key 2048)
[root@k8s-master pki]# openssl req -new -key serving.key -out serving.csr -subj "/CN=serving"
[root@k8s-master pki]# openssl x509 -req -in serving.csr -CA ./ca.crt -CAkey ./ca.key -CAcreateserial -out serving.crt -days 3650
[root@k8s-master pki]# kubectl create secret generic cm-adapter-serving-certs --from-file=serving.crt=./serving.crt --from-file=serving.key -n prom
[root@k8s-master pki]# kubectl get secret -n prom|egrep cm-adapter
cm-adapter-serving-certs Opaque 2 20s

cd /data/prometheus/k8s-prometheus-adapter/deploy/manifests

 kubectl delete  -f .

kubectl apply -f .

kubectl get pod -n prom |egrep custom
custom-metrics-apiserver-667fd4fffd-8fw98 1/1 Running 0 11m

kubectl api-versions |grep custom

custom.metrics.k8s.io/v1beta1

kubectl get apiservice

 

有這個api說明安裝成功了

可以起個代理測試下如下:

kubectl proxy --port=8080

curl http://localhost:8080/apis/custom.metrics.k8s.io/v1beta1

K8S 下面測試

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"

獲取CPU

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/cpu_usage"

獲取內存

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/memory_usage_bytes 

增加一個自定義的規則

yum install jq -y 

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests" | jq .

會得到這樣的錯誤提示"message": "the server could not find the metric http_requests for pods"

這個就跟你部署的那個deployment的具體鏡像k8s-prometheus-adapter-amd64它使用的配置文件有關,也就是custom-metrics-config-map.yaml里面定義的rules,

文件中所有的seriesQuery項在prometheus中查詢后的結果都沒有http_request_totals指標,所以也就肯定找不到。

吧下面的規則追加到cm文件里面。

不過訪問還是沒發現,這是因為,我們prommetheus 還沒有采集到Pod相關的指標

 

所以我們引入一個測試用例

測試用例的地址

https://github.com/erlonglong/k8s/tree/master/k8s/prod/prometheus/prometheus-adapter/test

kubectl create -f ./test/podinfo-svc.yaml,./test/podinfo-dep.yaml

kubectl create -f ./test/podinfo-hpa.yaml

現在看到有這個數據了

現在在運行,可以獲取到我們需要的指標數據了

可以查看下面的連接了解詳情:

https://blog.csdn.net/weixin_30300523/article/details/94924334

https://www.yangcs.net/posts/custom-metrics-hpa/

https://github.com/stefanprodan/k8s-prom-hpa

這么需要注意一點,在安裝壓力測試工具的時候

[root@wan132 ~]# go get -v -u github.com/rakyll/hey
github.com/rakyll/hey (download)
created GOPATH=/root/go; see 'go help gopath'

默認安裝到你運行目錄下面的的go目錄里面

http://10.211.55.13:32268/targets 可以看到好多信息

 具體查考下面文章

https://www.jianshu.com/p/9abb697cd833

哈哈,總於搞完了,可以看到需要的數據和自定義的數據都有了,下面我們把這些數據用圖形化的工具展示出來

Grafana數據展示

這里面涉及到的一個grafana.yaml

源文件在這下載

wget   https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/grafana.yaml

我這里面修改的地方:名稱空間

存儲卷修改PVC

鏡像用的是最新的鏡像,所以mountPath 需要修改,因為從grafana 5.1版本以后

mkdir: cannot create directory '/var/lib/grafana/plugins': No such file or directory 會報這個錯

具體參考這篇文章:

https://grafana.com/docs/installation/docker/#migration-from-a-previous-version-of-the-docker-container-to-5-1-or-later

這是我修改的地方

下面就是具體的PVC 情況

 

 注釋下面的內容,因為沒有用到,我們數據源不是這個:

 

 基本修改都是上面,修改好的文件可以到這里下載

wget https://raw.githubusercontent.com/erlonglong/test/master/grafana.yaml

 kubectl get pod -n prom |egrep grafana
monitoring-grafana-54f9dbdc6-4xrx7 1/1 Running 0 23s

kubectl get svc -n prom|egrep grafana
monitoring-grafana NodePort 10.111.238.73 <none> 80:31096/TCP 5h47m

在客戶端通過瀏覽器訪問31096 的端口

 

下面創建默認的元數據為prometheus

格式地址是 $svcname.$namespace.svc:9090

通過下面命令可以了解到

 

可以看到默認已經有源配置了

 接下來是導入圖形模版,下載模版的地方

https://grafana.com/dashboards?dataSource=prometheus&search=kube 

下面基本都是導入相應的模版

 

 

 在點擊紅色部分,把你下載好多json的模版導入進去

 

 

 導入成功,會展示下面的數據

 

 

 當然還有許多模版沒數據,可能和版本或者查詢的數據不一樣,在Status->Targets 下面查看是否 有down 的標簽,導致的

具體為什么不展示還需要自己去研究

 

 

 可以看到引用的查詢數據語句

后期學習地址
https://github.com/coreos/kube-prometheus

https://www.kancloud.cn/huyipow/prometheus/527091
https://yunlzheng.gitbook.io/prometheus-book/
https://www.kubernetes.org.cn/3418.html
https://www.servicemesher.com/blog/prometheus-operator-manual/
https://github.com/dotbalo/k8s/tree/master/prometheus-operator
https://www.servicemesher.com/blog/prometheus-monitor-k8s-2/

 最終整理的配置文檔

https://github.com/erlonglong/k8s/tree/master/k8s/prometheus

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM