監控實戰Prometheus+Grafana


 

這期的分享是監控實戰,其實不想寫這篇的,因為網上相關的文章也挺多的,但是出於光說不練都是假把式,而且也想告訴你:當帥氣的普羅米修斯(Prometheus)遇到高顏值的格拉法納(Grafana)究竟會擦出什么樣的火花?所以忍不住還是想分享啊。

 

640?wx_fmt=png

 

為了實戰,我們再次請出架構圖,請注意圖中紅色圈 1 的部分,主要分兩條線去實戰。

 

第一條戰線:Prometheus 如何監控機器?

采用標准的PGOne技術組件Prometheus Server + Grafana + node_exporter完成對機器的性能監控。

 

第二條戰線:Prometheus 如何監控 flink?

采用技術組件client lib(flink-metrics-prometheus_x.jar) + PushGateway + Prometheus Server + Grafana完成對 flink 的監控。

 

1. Prometheus 如何監控機器?

工欲善其事必先利其器,先下載相關組件包。prometheus 提供了兩種下載方式,第一種是二進制壓縮包的方式,第二種是 docker 鏡像的方式。

#方式1:二進制壓縮包下載鏈接	
https://prometheus.io/download/	

	
#方式2:docker鏡像鏈接	
https://hub.docker.com/u/prom

本次實戰均采用 docker 鏡像下載。

docker pull prom/node-exporter	
docker pull prom/prometheus	
docker pull grafana/grafana

  

下載完成成, 輸入命令 docker images 列出本地主機上的鏡像(由於pushgateway鏡像之前在本機已經下載過,你如果第一次跟着做,應該看不到這個,后面操作會進行下載)。

640?wx_fmt=png

 

做好准備工作。

#創建 grafana 數據存儲目錄	
mkdir /opt/grafana-storage	
#因為 grafana 會在這個目錄寫入文件,賦權限。	
chmod 777 -R /opt/grafana-storage	
#創建 prometheus 配置文件存放目錄	
mkdir /opt/prometheus/	
#在 prometheus 配置文件目錄下,創建prometheus.yml文件	
vi /opt/prometheus/prometheus.yml	
# prometheus.yml中配置靜態監控對象 targets,輸入如下配置內容(請注意修改 IP 為你的真實 IP):	
global:	
  scrape_interval:     60s	
  evaluation_interval: 60s	

	
scrape_configs:	
  - job_name: prometheus	
    static_configs:	
      - targets: ['localhost:9090']	
        labels:	
          instance: 'prometheus'	

	
  - job_name: linux	
    static_configs:	
      - targets: ['IP:9100']	
        labels:	
          instance: 'linux'

  

准備就緒,逐個啟動組件。

# 啟動 node-exporter	
docker run -d -p 9100:9100  -v "/proc:/host/proc:ro"  -v "/sys:/host/sys:ro"  -v "/:/rootfs:ro"  --net="host"  prom/node-exporter	
# 啟動 prometheus	
docker run -d -p 9090:9090  -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  prom/prometheus	
# 啟動 grafana	
docker run -d -p 3000:3000  --name=grafana -v /opt/grafana-storage:/var/lib/grafana grafana/grafana

  

確認一下是否都啟動了,輸入 docker ps -a 一探究竟。

640?wx_fmt=png

再次確認一下服務是否都 OK 了, 逐個訪問一下。

  • node_exporter 訪問輸入 http://YOUR_CONF_IP:9100/metrics,效果如下

    640?wx_fmt=png

  • Prometheus 訪問輸入 http://YOUR_CONF_IP:9090/targets,效果如下

    640?wx_fmt=png

  • Grafana 訪問輸入 http://YOUR_CONF_IP:3000,效果如下

    640?wx_fmt=jpeg

默認用戶名密碼 : admin/admin

640?wx_fmt=jpeg

點擊 Add data source,選擇 Prometheus。

640?wx_fmt=jpeg

配置url 輸入Prometheus的 ip + 端口,然后點擊 Save&Test 按鈕,會提示Data source is working。

640?wx_fmt=png

回到首頁,點擊 New dashboard --> Add Query。

640?wx_fmt=jpeg

640?wx_fmt=jpeg

Panel Title 下拉菜單選擇 edit,輸入指標會自動提示呦。

640?wx_fmt=jpeg

效果所見即所得。

640?wx_fmt=jpeg

 

到這兒,采用 Prometheus Server + Grafana + node_exporter 對機器性能指標監控的實戰,就算演示操作完畢,點到為止,接下來看看 flink 監控如何集成。

 

2. Prometheus 如何監控 flink?

第一步:下載 pushgateway 鏡像,並完成啟動。

# 下載 pushgateway 鏡像	
docker pull prom/pushgateway	

	
# 啟動 pushgateway	
docker run -d -p 9091:9091 prom/pushgateway

  

第二步:在 prometheus.yml 中添加 pushgateway 的配置,用於告訴 Prometheus 監控 pushgateway,並重新啟動 prometheus。

global:	
  scrape_interval:     60s	
  evaluation_interval: 60s	

	
scrape_configs:	
  - job_name: prometheus	
    static_configs:	
      - targets: ['localhost:9090']	
        labels:	
          instance: 'prometheus'	

	
  - job_name: linux	
    static_configs:	
      - targets: ['IP:9100']	
        labels:	
          instance: 'linux'	
  - job_name: 'pushgateway'	
    static_configs:	
      - targets: ['IP:9091']	
        labels:	
          instance: 'pushgateway'

  

第三步:針對 flink 添加監控集成包,直接把 flink-1.8.1/opt 目錄下的 flink-metrics-prometheus-1.8.1.jar 包復制一份到 flink-1.8.1/lib 目錄下即可。

 

第四步:然后在 flink 配置文件 flink-conf.yml 中添加如下內容(注意修改IP),啟動 flink 即可。

##metrics	
metrics.reporter.promgateway.class: org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporter	
metrics.reporter.promgateway.host: YOUR_CONF_IP	
metrics.reporter.promgateway.port: 9091	
metrics.reporter.promgateway.jobName: myJob	
metrics.reporter.promgateway.randomJobNameSuffix: true	
metrics.reporter.promgateway.deleteOnShutdown: false

  

第五步:回到 Grafana 首頁,點擊 New dashboard,創建一個新的 dashboard,選擇 flink(注意如果沒有出現 flink,那說明 flink 沒有啟動)。

640?wx_fmt=jpeg

 

選擇並添加相關指標看一看。

640?wx_fmt=jpeg

 

好了,到這 Prometheus 監控 flink 也就完畢了,后續就是監控指標如何展示的更好的問題,不再贅述。

 

3. 有鍾意的 dashboard,Grafana 如何讓她變成自己的?

網站 https://grafana.com/grafana/dashboards 提供了一系列的模板,可供使用,那該如何導入到自己的 Grafana 下呢?

 

第一步:選擇鍾意的 dashboard,獲取對應的 dashboard 編號。

640?wx_fmt=jpeg

 

第二步:回到自己的 Grafana 首頁,選擇"+" --> Import

640?wx_fmt=jpeg

 

然后輸入 Copy 的 dashboard 編號,點擊 load。

640?wx_fmt=jpeg

 

 

效果所見即所得,高端大氣上檔次。

640?wx_fmt=jpeg

 

好了,帥氣的 Prometheus 與高顏值的 Grafana 擦出的煙火就放到這兒吧。不過在結束之前,還是歸攏一下本次演示遇到的問題吧,以供你參考。

 

4. 問題集錦

 

問題一:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

linux解決方案	
    systemctl daemon-reload	
    systemctl restart docker.service	
    	
mac下直接啟動 docker 服務就行了。

  

問題二:Get http://localhost:9100/metrics: dial tcp [::1]:9100: connect: connection refused

解決方案:修改 prometheus.yml 文件中 targets: ['localhost:PORT'] 中的 localhost:PORT 修改為真實 IP:PORT 就行了。

 

問題三:啟動 grafana 時始終失敗。

mkdir: cannot create directory '/var/lib/grafana/plugins': Permission denied	
GF_PATHS_DATA='/var/lib/grafana' is not writable.	
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migration-from-a-previous-version-of-the-docker-container-to-5-1-or-later

解決方案:chmod 777 /opt/grafana-storage

 

問題四:Prometheus 監控 flink 時,始終找不到 PrometheusPushGatewayReporter。

java.lang.ClassNotFoundException: org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporter

解決方案:直接把 flink-1.8.1/opt 目錄下的 flink-metrics-prometheus-1.8.1.jar 包復制一份到 flink-1.8.1/lib 目錄下即可。

 

5. 命令集錦

docker pull prom/node-exporter  //拉取鏡像	
docker images //查看本機所有鏡像	
docker run ... //創建一個新的容器	
docker stop $(docker ps -a -q)  //停止所有容器	
docker rm $(docker ps -a -q) //刪除所有容器	
docker logs -f --tail=10 CONTAINER_ID //查看容器的最后10行的日志

  

好了,每天進步一點點, 一年后你的進步將遠遠超乎你的想象。如果感覺文章有點意思,請多多分享轉發吧。

 

 


免責聲明!

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



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