1、Prometheus概述
除了前面的資源指標(如CPU、內存)以外,用戶或管理員需要了解更多的指標數據,比如Kubernetes
指標、容器指標、節點資源指標以及應用程序指標等等。自定義指標API允許請求任意的指標,其指標API的實現要指定相應的后端監視系統。而Prometheus
是第一個開發了相應適配器的監控系統。這個適用於Prometheus
的Kubernetes 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
自定義指標。
Prometheus是一個開源的服務監控系統和時序數據庫,其提供了通用的數據模型和快捷數據采集、存儲和查詢接口。它的核心組件Prometheus服務器定期從靜態配置的監控目標或者基於服務發現自動配置的目標中進行拉取數據,新拉取到啊的 數據大於配置的內存緩存區時,數據就會持久化到存儲設備當中。Prometheus組件架構圖如下:
如上圖,每個被監控的主機都可以通過專用的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。
2、Prometheus部署
由於官方的YAML部署方式需要使用到PVC,這里使用馬哥提供的學習類型的部署,具體生產還是需要根據官方的建議進行。本次部署的YAML
2.1、創建名稱空間prom
[root@k8s-master ~]# git clone https://github.com/iKubernetes/k8s-prom.git && cd k8s-prom [root@k8s-master k8s-prom]# kubectl apply -f namespace.yaml namespace/prom created
2.2、部署node_exporter
[root@k8s-master k8s-prom]# kubectl apply -f node_exporter/ daemonset.apps/prometheus-node-exporter created service/prometheus-node-exporter created [root@k8s-master k8s-prom]# kubectl get pods -n prom NAME READY STATUS RESTARTS AGE prometheus-node-exporter-6srrq 1/1 Running 0 32s prometheus-node-exporter-fftmc 1/1 Running 0 32s prometheus-node-exporter-qlr8d 1/1 Running 0 32s
2.3、部署prometheus-server
[root@k8s-master k8s-prom]# kubectl apply -f prometheus/ configmap/prometheus-config unchanged deployment.apps/prometheus-server configured clusterrole.rbac.authorization.k8s.io/prometheus configured serviceaccount/prometheus unchanged clusterrolebinding.rbac.authorization.k8s.io/prometheus configured service/prometheus unchanged [root@k8s-master k8s-prom]# kubectl get all -n prom NAME READY STATUS RESTARTS AGE pod/prometheus-node-exporter-6srrq 1/1 Running 0 11m pod/prometheus-node-exporter-fftmc 1/1 Running 0 11m pod/prometheus-node-exporter-qlr8d 1/1 Running 0 11m pod/prometheus-server-66cbd4c6b-j9lqr 1/1 Running 0 4m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/prometheus NodePort 10.96.65.72 <none> 9090:30090/TCP 10m service/prometheus-node-exporter ClusterIP None <none> 9100/TCP 11m NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/prometheus-node-exporter 3 3 3 3 3 <none> 11m NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.apps/prometheus-server 1 1 1 1 10m NAME DESIRED CURRENT READY AGE replicaset.apps/prometheus-server-65f5d59585 0 0 0 10m replicaset.apps/prometheus-server-66cbd4c6b 1 1 1 4m
2.4、部署kube-sate-metrics
[root@k8s-master k8s-prom]# kubectl apply -f kube-state-metrics/ deployment.apps/kube-state-metrics created serviceaccount/kube-state-metrics created clusterrole.rbac.authorization.k8s.io/kube-state-metrics created clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created service/kube-state-metrics created [root@k8s-master k8s-prom]# kubectl get pods -n prom -o wide NAME READY STATUS RESTARTS AGE IP NODE kube-state-metrics-78fc9fc745-g66p8 1/1 Running 0 11m 10.244.1.22 k8s-node01 prometheus-node-exporter-6srrq 1/1 Running 0 31m 192.168.56.11 k8s-master prometheus-node-exporter-fftmc 1/1 Running 0 31m 192.168.56.12 k8s-node01 prometheus-node-exporter-qlr8d 1/1 Running 0 31m 192.168.56.13 k8s-node02 prometheus-server-66cbd4c6b-j9lqr 1/1 Running 0 24m 10.244.0.4 k8s-master
2.5、制作證書
[root@k8s-master pki]# (umask 077; openssl genrsa -out serving.key 2048) Generating RSA private key, 2048 bit long modulus ......................+++ ....+++ e is 65537 (0x10001) [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 Signature ok subject=/CN=serving Getting CA Private Key [root@k8s-master pki]# kubectl create secret generic cm-adapter-serving-certs --from-file=serving.crt=./serving.crt --from-file=serving.key -n prom secret/cm-adapter-serving-certs created [root@k8s-master pki]# kubectl get secret -n prom NAME TYPE DATA AGE cm-adapter-serving-certs Opaque 2 20s
2.6、部署k8s-prometheus-adapter
這里自帶的custom-metrics-apiserver-deployment.yaml和custom-metrics-config-map.yaml有點問題,需要下載k8s-prometheus-adapter
項目中的這2個文件
[root@k8s-master k8s-prometheus-adapter]# wget https://raw.githubusercontent.com/DirectXMan12/k8s-prometheus-adapter/master/deploy/manifests/custom-metrics-apiserver-deployment.yaml [root@k8s-master k8s-prometheus-adapter]# vim k8s-prometheus-adapter/custom-metrics-apiserver-deployment.yaml #修改名稱空間為prom [root@k8s-master k8s-prometheus-adapter]# wget https://raw.githubusercontent.com/DirectXMan12/k8s-prometheus-adapter/master/deploy/manifests/custom-metrics-config-map.yaml #也需要修改名稱空間為prom [root@k8s-master k8s-prom]# kubectl apply -f k8s-prometheus-adapter/ clusterrolebinding.rbac.authorization.k8s.io/custom-metrics:system:auth-delegator created rolebinding.rbac.authorization.k8s.io/custom-metrics-auth-reader created deployment.apps/custom-metrics-apiserver created clusterrolebinding.rbac.authorization.k8s.io/custom-metrics-resource-reader created serviceaccount/custom-metrics-apiserver created service/custom-metrics-apiserver created apiservice.apiregistration.k8s.io/v1beta1.custom.metrics.k8s.io created clusterrole.rbac.authorization.k8s.io/custom-metrics-server-resources created clusterrole.rbac.authorization.k8s.io/custom-metrics-resource-reader created clusterrolebinding.rbac.authorization.k8s.io/hpa-controller-custom-metrics created configmap/adapter-config created [root@k8s-master k8s-prom]# kubectl get pods -n prom NAME READY STATUS RESTARTS AGE custom-metrics-apiserver-65f545496-l5md9 1/1 Running 0 7m kube-state-metrics-78fc9fc745-g66p8 1/1 Running 0 40m prometheus-node-exporter-6srrq 1/1 Running 0 1h prometheus-node-exporter-fftmc 1/1 Running 0 1h prometheus-node-exporter-qlr8d 1/1 Running 0 1h prometheus-server-66cbd4c6b-j9lqr 1/1 Running 0 53m [root@k8s-master k8s-prom]# kubectl api-versions |grep custom custom.metrics.k8s.io/v1beta1 [root@k8s-master ~]# kubectl get svc -n prom NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE custom-metrics-apiserver ClusterIP 10.99.14.141 <none> 443/TCP 11h kube-state-metrics ClusterIP 10.107.23.237 <none> 8080/TCP 11h prometheus NodePort 10.96.65.72 <none> 9090:30090/TCP 11h prometheus-node-exporter ClusterIP None <none> 9100/TCP 11h
訪問192.168.56.11:30090,如下圖:選擇 需要查看的指標,點擊Execute