k8s+Prometheus+Grafana的監控部署


一、安裝部署k8s集群

可以參考https://www.cnblogs.com/liugp/p/12115945.html

二、Prometheus+Grafana的監控部署

2.1、master/node節點環境部署

在【master可以進行安裝部署
安裝git,並下載相關yaml文件

https://gitee.com/liugpwwwroot/k8s-prometheus-grafana.git

2.2、采用daemonset方式部署node-exporter組件

kubectl create -f k8s-prometheus-grafana/node-exporter.yaml

2.3、部署prometheus組件

2.3.1、rbac文件

kubectl create -f  k8s-prometheus-grafana/prometheus/rbac-setup.yaml

2.3.2 以configmap的形式管理prometheus組件的配置文件

kubectl create -f  k8s-prometheus-grafana/prometheus/configmap.yaml 

2.3.3 Prometheus deployment 文件

kubectl create -f  k8s-prometheus-grafana/prometheus/prometheus.deploy.yml

2.3.4 Prometheus service文件

kubectl create -f  k8s-prometheus-grafana/prometheus/prometheus.svc.yml 

2.4、部署grafana組件

2.4.1 grafana deployment配置文件

kubectl create -f   k8s-prometheus-grafana/grafana/grafana-deploy.yaml

2.4.2 grafana service配置文件

kubectl create -f   k8s-prometheus-grafana/grafana/grafana-svc.yaml

2.4.3 grafana ingress配置文件

kubectl create -f   k8s-prometheus-grafana/grafana/grafana-ing.yaml

2.5、查看訪問相應服務

 2.5.1、訪問node-exporter(上圖對應的端口:31672),這個采集器只是采集宿主機上的一些指標

2.5.2、cAdvisor:一種用於監控資源使用情況並分析容器性能的開源代理,即容器指標采集器,cAdvisor集成在Kubelet中,所有安裝了k8s就自動安裝了cAdvisor,cAdvisor的UI已經差不多被棄用了,Kubernetes 1.12版本之后cAdvisor的UI會被徹底刪除,但是可以用granfana展示圖表,還有一個問題就是默認不被外部訪問,但是可以通過代理方式訪問。使用代理如下:

# 這種方式只允許宿主機上訪問,也就是localost或者127.0.0.1訪問
$ kubectl proxy

# 設置API server接收所有主機的請求
$ kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'

示例:

# 訪問cadvisor采集的指標數據
$ http://192.168.182.138:8001/api/v1/nodes/k8s-master/proxy/metrics/cadvisor

2.5.3、訪問prometheus(上圖對應的端口:30003

 2.5.4、訪問granfana,默認用戶名,密碼均為admin(上圖對應的端口:30106

 1、添加數據源,這里使用代理模式(推薦),僅限k8s內部訪問

 當然也可以使用直接訪問模式

2、導入面板:Home->Dashboards->Import

 導入面板,可以直接輸入模板編號315在線導入,或者下載好對應的json模板文件本地導入,面板模板下載地址https:///dashboards/315

 稍等片刻,即可展示如下圖,記得選擇prometheus數據源為prometheus

 點擊Import,即可查看展示效果

三、測試

3.1、部署測試實例

  •  部署實例
  • [root@k8s-master k8s-prometheus-grafana]# kubectl run apache --image=httpd --replicas=2
  • 查看
  • [root@k8s-master k8s-prometheus-grafana]# kubectl get deployments.apps -o wide
  • 創建svc
  • [root@k8s-master k8s-prometheus-grafana]# kubectl expose deployment apache --port=88 --target-port=80 --type=NodePort

  •  訪問:http://192.168.182.138:30085/

最后如果要刪掉重置可以執行:

# 倒敘強制刪除
kubectl delete -f k8s-prometheus-grafana/grafana/grafana-ing.yaml --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/grafana/grafana-svc.yaml --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/grafana/grafana-deploy.yaml --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/prometheus/prometheus.svc.yml  --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/prometheus/prometheus.deploy.yml  --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/prometheus/configmap.yaml  --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/prometheus/rbac-setup.yaml  --grace-period=0 --force

kubectl delete -f k8s-prometheus-grafana/node-exporter.yaml  --grace-period=0 --force

# 再順序執行創建
kubectl create -f k8s-prometheus-grafana/node-exporter.yaml

kubectl create -f k8s-prometheus-grafana/prometheus/rbac-setup.yaml

kubectl create -f k8s-prometheus-grafana/prometheus/configmap.yaml 

kubectl create -f k8s-prometheus-grafana/prometheus/prometheus.deploy.yml

kubectl create -f k8s-prometheus-grafana/prometheus/prometheus.svc.yml

kubectl create -f k8s-prometheus-grafana/grafana/grafana-deploy.yaml

kubectl create -f k8s-prometheus-grafana/grafana/grafana-svc.yaml

kubectl create -f k8s-prometheus-grafana/grafana/grafana-ing.yaml

 ~~~以上就全部部署,有不清楚的,或有不足的地方,歡迎評論^_^ ~~~


免責聲明!

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



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