Prometheus監控k8s(4)-grafana監控k8s集群/node/資源對象


Prometheus監控k8s(4)-grafana監控k8s集群/node/資源對象

https://blog.51cto.com/14143894/2438026

https://www.qikqiak.com/k8s-book/docs/54.%E7%9B%91%E6%8E%A7Kubernetes%E9%9B%86%E7%BE%A4%E8%8A%82%E7%82%B9.html

 

1 K8S監控指標

對於集群的監控一般我們需要考慮以下幾個方面:

  • Kubernetes 節點的監控:比如節點的 cpu、load、disk、memory 等指標
  • 內部系統組件的狀態:比如 kube-scheduler、kube-controller-manager、kubedns/coredns 等組件的詳細運行狀態
  • 編排級的 metrics:比如 Deployment 的狀態、資源請求、調度和 API 延遲等數據指標

 

node監控

• Node資源利用率 :一般生產環境幾十個node,幾百個node去監控
• Node數量 :一般能監控到node,就能監控到它的數量了,因為它是一個實例,一個node能跑多少個項目,也是需要去評估的,整體資源率在一個什么樣的狀態,什么樣的值,所以需要根據項目,跑的資源利用率,還有值做一個評估的,比如再跑一個項目,需要多少資源。
• Pods數量(Node):其實也是一樣的,每個node上都跑多少pod,不過默認一個node上能跑110個pod,但大多數情況下不可能跑這么多,比如一個128G的內存,32核cpu,一個java的項目,一個分配2G,也就是能跑50-60個,一般機器,pod也就跑幾十個,很少很少超過100個。
• 資源對象狀態 :比如pod,service,deployment,job這些資源狀態,做一個統計。

 

Pod監控
• Pod數量(項目):你的項目跑了多少個pod的數量,大概的利用率是多少,好評估一下這個項目跑了多少個資源占有多少資源,每個pod占了多少資源。
• 容器資源利用率 :每個容器消耗了多少資源,用了多少CPU,用了多少內存
• 應用程序:這個就是偏應用程序本身的指標了,這個一般在我們運維很難拿到的,所以在監控之前呢,需要開發去給你暴露出來,這里有很多客戶端的集成,客戶端庫就是支持很多語言的,需要讓開發做一些開發量將它集成進去,暴露這個應用程序的想知道的指標,然后納入監控,如果開發部配合,基本運維很難做到這一塊,除非自己寫一個客戶端程序,通過shell/python能不能從外部獲取內部的工作情況,如果這個程序提供API的話,這個很容易做到。

 

如果想監控node的資源,就可以放一個node_exporter,這是監控node資源的,node_exporter是Linux上的采集器,你放上去你就能采集到當前節點的CPU、內存、網絡IO,等待都可以采集的。

如果想監控容器,k8s內部提供cAdvisor采集器,pod呀,容器都可以采集到這些指標,都是內置的,不需要單獨部署,只知道怎么去訪問這個Cadvisor就可以了。

如果想監控k8s資源對象,會部署一個kube-state-metrics這個服務,它會定時的API中獲取到這些指標,幫你存取到Prometheus里,要是告警的話,通過Alertmanager發送給一些接收方,通過Grafana可視化展示。

 

 

 

2 POD容器監控—cAdvisor

目前cAdvisor集成到了kubelet組件內,可以在kubernetes集群中每個啟動了kubelet的節點使用cAdvisor提供的metrics接口獲取該節點所有容器相關的性能指標數據。
cAdvisor對外提供服務的默認端口為***4194***,主要提供兩種接口:

  • Prometheus格式指標接口:nodeIP:4194/metrics(或者通過kubelet暴露的cadvisor接口nodeIP:10255/metrics/cadvisor);
  • WebUI界面接口:nodeIP:4194/containers/

以上接口的數據都是按prometheus的格式輸出的。

  • kubelet的節點使用cAdvisor提供的metrics接口獲取該節點所
  • 有容器相關的性能指標數據。

暴露接口地址:

https://NodeIP:10255/metrics/cadvisor

https://NodeIP:10250/metrics/cadvisor

 

 

 

切換到 Graph 路徑下面查詢容器相關數據,比如我們這里來查詢集群中所有 Pod 的 CPU 使用情況,這里用的數據指標是 container_cpu_usage_seconds_total,然后去除一些無效的數據,查詢1分鍾內的數據,由於查詢到的數據都是容器相關的,最好要安裝 Pod 來進行聚合,對應的promQL語句如下:

sum by (pod_name)(rate(container_cpu_usage_seconds_total{image!="", pod_name!=""}[1m] ))

 

 

可以看到上面的結果就是集群中的所有 Pod 在1分鍾之內的 CPU 使用情況的曲線圖

3 Node節點監控—node_exporter

Prometheus 來采集節點的監控指標數據,可以通過node_exporter來獲取,顧名思義,node_exporter 就是抓取用於采集服務器節點的各種運行指標,目前 node_exporter 支持幾乎所有常見的監控點,比如 conntrack,cpu,diskstats,filesystem,loadavg,meminfo,netstat等,詳細的監控點列表可以參考其Github repo

我們可以通過 DaemonSet 控制器來部署該服務,這樣每一個節點都會自動運行一個這樣的 Pod,如果我們從集群中刪除或者添加節點后,也會進行自動擴展。

https://www.cnblogs.com/zqj-blog/p/10895901.html

一個linux系統下的采集硬件和操作系統指標的組件,由Go語言編寫。

  在默認情況下,會顯示所有收集到的指標,可以使用“collect[]”過濾指標,在prometheus的配置下使用詞語法。

  node_exporter會收集許多信息,但是默認情況下,由於內核的安全性設置,它不能收集linux上的perf,若要允許,需要開啟linux系統的sysctl配置

sysctl -w kernel.perf_event_paranoid=X
#一般 =1

  其中,

    2    允許用戶

    1    允許內核和用戶

    0 允許訪問特定的CPU,但不允許訪問原始跟蹤點

    -1   無限制

 

3.1 安裝node-exporter(用於收集節點的數據指標)

 

[root@k8s-master node-exporter]# grep image: *
node-exporter-ds.yml:          image: "prom/node-exporter" #用最新版本的,否則有的插件不兼容
[root@k8s-master node-exporter]#
[root@k8s-master prometheus]# cd node-exporter/
[root@k8s-master node-exporter]# ls
node-exporter-ds.yml  node-exporter-service.yaml
[root@k8s-master node-exporter]# kubectl apply -f .
daemonset.apps/node-exporter created
service/node-exporter created 
[root@k8s-master node-exporter]# kubectl get svc,pod,ds -n kube-system |grep node

service/node-exporter              ClusterIP   None             <none>        9100/TCP                 23s
pod/node-exporter-f2m5k                       1/1     Running   0          23s
pod/node-exporter-fk7vs                       1/1     Running   0          23s

pod/node-exporter-hz5m4                       1/1     Running   0          23s
daemonset.extensions/node-exporter             3         3         3       3            3           <none> 23s
[root@k8s-master node-exporter]#

 

3.2 后台訪問

http://10.6.76.23:9100/metrics

 

 

3.3  Prometheus顯示

 

 

 

 

4 資源狀態監控—kube-state-metrics

kube-state-metrics是一個簡單的服務,它監聽Kubernetes API服務器並生成有關對象狀態的指標。它不關注單個Kubernetes組件的運行狀況,而是關注內部各種對象的運行狀況,例如部署,節點和容器。

采集了k8s中各種資源對象的狀態信息:

kube-state-metrics
kube_daemonset_*
kube_deployment_*
kube_job_*
kube_namespace_*
kube_node_*
kube_persistentvolumeclaim_*
kube_pod_container_*
kube_pod_*
kube_replicaset_*
kube_service_*
kube_statefulset_*

4.1 安裝kube-state-metrics

[root@k8s-master kube-state-metrics]# ls
kube-state-metrics-deployment.yaml  kube-state-metrics-rbac.yaml  kube-state-metrics-service.yaml
[root@k8s-master kube-state-metrics]# grep image * #修改一下鏡像地址 kube-state-metrics-deployment.yaml:        image: quay.io/coreos/kube-state-metrics:v1.3.0 kube-state-metrics-deployment.yaml:        image: mirrorgooglecontainers/addon-resizer:1.8.5 kube-state-metrics-deployment.yaml:        #image: registry.cn-hangzhou.aliyuncs.com/criss/addon-resizer
[root@k8s-master kube-state-metrics]# cd ..
[root@k8s-master prometheus]# kubectl apply -f kube-state-metrics
deployment.apps/kube-state-metrics created
configmap/kube-state-metrics-config created
serviceaccount/kube-state-metrics created
clusterrole.rbac.authorization.k8s.io/kube-state-metrics created
role.rbac.authorization.k8s.io/kube-state-metrics-resizer created
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created
rolebinding.rbac.authorization.k8s.io/kube-state-metrics created
service/kube-state-metrics created
[root@k8s-master prometheus]#
[root@k8s-master kube-state-metrics]# kubectl get all -n kube-system -o wide| grep kube-state-metrics

pod/kube-state-metrics-65f56956dd-xmngf       2/2     Running     0          123m    10.254.1.149   k8s-node-1   <none>           <none>

service/kube-state-metrics         ClusterIP   10.103.120.166   <none>        8080/TCP,8081/TCP        129m    k8s-app=kube-state-metrics


deployment.apps/kube-state-metrics         1/1     1            1           123m   kube-state-metrics,addon-resizer   quay.io/coreos/kube-state-metrics:v1.3.0,mirrorgooglecontainers/addon-resizer:1.8.5      k8s-app=kube-state-metrics,version=v1.3.0

replicaset.apps/kube-state-metrics-65f56956dd       1         1         1       123m   kube-state-metrics,addon-resizer   quay.io/coreos/kube-state-metrics:v1.3.0,mirrorgooglecontainers/addon-resizer:1.8.5      k8s-app=kube-state-metrics,pod-template-hash=65f56956dd,version=v1.3.0
replicaset.apps/kube-state-metrics-6d5589454b       0         0         0       123m   kube-state-metrics,addon-resizer   quay.io/coreos/kube-state-metrics:v1.3.0,mirrorgooglecontainers/addon-resizer:1.8.5      k8s-app=kube-state-metrics,pod-template-hash=6d5589454b,version=v1.3.0




[root@k8s-master kube-state-metrics]#

 

 

4.2  Prometheus顯示

沒有安裝之前也能搜索kube_這種語句,但沒有圖形

 

 

 

 

 

 

 

 

5  用Grafana模板可視化展示Prometheus監控數據

推薦模板: 也就是在grafana共享中心里面的,也就是別人寫的模版上傳到這里庫里面的,自己也可以寫,寫完上傳上去,別人也可以訪問到,下面是模版的id,只要獲取這個ID,就能使用這個模版了,只要這個模版,后端提供執行promeQL,只要有數據就能幫你展示出來
Grafana.com
• 集群資源監控:3119
• 資源狀態監控 :6417
• Node監控 :9276

這是常用的,也可以根據自己的需要去官網https://grafana.com/grafana/dashboards

 

模板的使用

 

 

 

 

 

 

 

 

 這里只是部署了grafana,圖形模板確實不好用,建議采用k8s插件,下文

 


免責聲明!

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



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