使用docker方式構建prometheus監控的學習


一、背景:近期學習部署prometheus監控系統,經研究發現prometheus提供docker運行模式。根據我的經驗,能夠使用docker模式構建系統一定多快好省。

二、環境:

1、centos7.5虛擬機一台,分配4G內存,擬作prometheus服務器,ip:192.168.0.208

2、centos8.0虛擬機一台,分配2G內存,擬作node-exporter客戶機,ip:192.168.0.202

3、兩台機器都在不同的實體機上,已安裝docker-ce軟件

三、部署過程

1、服務器(考慮安裝prometheus服務器和node-exporter客戶端):

(1)pull服務器鏡像:

docker pull prom/prometheus

(2)pull客戶端鏡像:

docker pull prom/node-exporter

(3)配置prometheus.yml

mkdir -p /root/prometheus/prometheus-data          #prometheus的工作目錄和數據目錄

mkdir -p /root/prometheus/node-exporter-data       #node-exporter的數據目錄

cd /root/prometheus

vi promethe.yml

內容如下:

...

scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']

- job_name: 'prometheus-node-exporter'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['192.168.0.208:9100']

- job_name: 'node-dell5460'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['192.168.0.202:9100']

(4)運行prometheus服務

[root@ELK prometheus]# docker run -d -p 9090:9090 -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml -v $PWD/prometheus-data:/prometheus  --hostname ELK.localdomain --name prometheus prom/prometheus

說明:

-p 9090:9090     #服務端口對應到宿主機的相同端口

-v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml    #容器內/etc/prometheus/prometheus.yml配置文件掛載到宿主機/root/prometheus目錄下。

-v $PWD/prometheus-data:/prometheus    #容器內prometheus工作目錄掛載到宿主機的/root/prometheus/prometheus-data目錄下。

--hostname ELK.localdomain   #容器的主機名稱(ELK.localdomain是208機的主機名),若不加這個選項,docker會自動把容器的短id號作為容器主機名,在web頁面就會發生無法訪問的問題。

測試:curl http://localhost:9090/metrics

(5)運行node-exporter

[root@ELK prometheus]# docker run -d   --net="host"   --pid="host"   -v "/root/prometheus/node-exporter-data:/host:ro,rslave"   prom/node-exporter   --path.rootfs=/host

測試:curl http://192.168.0.208:9100/metrics

2、node-exporter客戶機

(1)pull客戶端鏡像

 docker pull prom/node-exporter

(2)運行node-exporter

[root@ELK prometheus]# docker run -d   --net="host"   --pid="host"   -v "/home/node-exporter-data:/host:ro,rslave"   prom/node-exporter   --path.rootfs=/host

3、訪問測試:

在瀏覽器中輸入網址:http://192.168.0.208:9090

 四、使用

1、metric的表現形式是鍵值對{k,v}

2、metrics的頁面說明:

#HELP 關於指標的說明

#TYPE 指標的類型,常見gauge,conter等類型

鍵  值     以空格隔開

3、把metrics頁面存在的鍵拷貝后粘貼到graph頁面的搜索欄中即可看到值或圖形。

4、在搜索欄中可靈活應用各類函數,如rate、increase、sum、topk、等等

例:

rate(node_cpu_seconds_total{mode="user"}[1m])   #1分鍾內用戶態cpu時間每秒增加量

rate(node_cpu_seconds_total{mode="system",instance="192.168.0.208:9100",job="prometheus-node-exporter"}[1m])

(1-(sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by(instance))  /  (sum(increase(node_cpu_seconds_total[1m])) by(instance)))*100   #每台機器1分鍾內cpu負荷

topk(3,rate(node_network_receive_bytes_total[5m]))  #網絡5分鍾內平均每秒接收字節數前3位的數據展示

5、關於web頁面的時間顯示問題,默認頁面顯示的是UTS時區時間,與本地時間相差8小時。這是因為Prometheus 為避免時區混亂,在所有組件中專門使用 Unix Time 和 Utc 進行顯示。不支持在配置文件中設置時區,也不能讀取本機 /etc/timezone 時區。prometheus在新版web頁面已提供本地時區時間顯示功能,可點擊web頁面右上角“Try experimental React UI”切換到新版頁面,在新版頁面上部勾選“Use local time”即可。

 

附:更多的函數參考 https://prometheus.io/docs/prometheus/latest/querying/functions/

 


免責聲明!

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



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