Prometheus學習筆記(2)Prometheus部署


Prometheus的安裝配置啟動

1、Prometheus二進制安裝

Prometheus下載鏈接:https://prometheus.io/download/

Prometheus本身的存儲是通過時間序列化存儲的,所以對時間是很有要求的,系統時間需要通過ntp進行同步,避免因為時間造成數據無法顯示。

# 時間同步
[root@prometheus ~]# ntpdate ntp1.aliyun.com
[root@prometheus ~]# crontab -e
*/5 * * * * ntpdate ntp1.aliyun.com &> /dev/null

# 下載
[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz

# 解壓
[root@prometheus ~]# tar -zxf prometheus-2.14.0.linux-amd64.tar.gz -C /usr/local
[root@prometheus ~]# mv /usr/local/prometheus-2.14.0.linux-amd64 /usr/local/prometheus-2.14.0
[root@prometheus ~]# ln -sv /usr/local/prometheus-2.14.0 /usr/local/prometheus

# 配置自我監控
[root@prometheus ~]# cd /usr/local/prometheus
[root@prometheus ~]# vim prometheus.yaml
global:
  scrape_interval:     15s # 全局配置,默認15s收集一次數據.

  # 配置外部標簽
  external_labels:
    monitor: 'codelab-monitor'

# 監控配置
scrape_configs:
  # 監控任務名稱,KV形式.
  - job_name: 'prometheus'

    # 覆蓋前面的全局配置,以5s收集一次數據.
    scrape_interval: 5s

    # 目標監控主機和收集數據的端口
    static_configs:
      - targets: ['localhost:9090']

# 啟動
[root@prometheus prometheus]# ./prometheus &
[root@prometheus prometheus]# netstat -tulnp |grep 9090 
[root@prometheus prometheus]# netstat -tulnp |grep 9090 
tcp6       0      0 :::9090                 :::*                    LISTEN      21407/./prometheus  

上面可以看到監聽了9090端口,即可通過localhost:9090/metrics來獲取指標數據,也可以通過瀏覽器直接訪問localhost:9090通過web界面來查看數據。

Prometheus的安裝還是比較簡單的,但是由於Prometheus沒有使用systemd來進行管理,用./的方式啟動並不方便,這里雖然使用&、nohup的掛起方式也不盡完美,那么這里再來2種守護進程方式運行prometheus。如下:

  • (1)安裝screen工具,將prometheus放入后台運行
[root@prometheus prometheus]# yum install screen -y
[root@prometheus prometheus]# screen 
[root@prometheus prometheus]# ./prometheus
#按Ctrl+a+d后進入后台運行模式
[root@prometheus prometheus]# screen -ls
There is a screen on:
	28915.pts-0.prometheus	(Detached)
1 Socket in /var/run/screen/S-root.

[root@prometheus prometheus]# ps -ef |grep prometheus
root     28927 28916  6 15:07 pts/2    00:00:01 ./prometheus
  • (2)使用systemd進行管理
[root@prometheus ~]# vim /usr/lib/systemd/system/prometheus.service 
[Unit]
Description=prometheus
Documentation=https://prometheus.io/docs/introduction/overview
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=simple
PIDFile==/var/run/prometheus.pid
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.read-timeout=5m --web.max-connections=512 --storage.tsdb.retention=15d --storage.tsdb.path="data/" --query.max-concurrency=20 --query.timeout=2m
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl start prometheus
[root@prometheus ~]# ps -ef |grep prometheus
root     29688     1  1 16:31 ?        00:00:00 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.read-timeout=5m --web.max-connections=512 --storage.tsdb.retention=15d --storage.tsdb.path="data/" --query.max-concurrency=20 --query.timeout=2m
[root@prometheus ~]# netstat -tulnp |grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      29707/prometheus    

這里需要了解的幾個啟動優化的參數:

  • --config.file:指定prometheus的配置文件路徑

  • --web.read-timeout:請求鏈接的最大等待時間,為了防止過多查詢鏈接請求或太多的空閑鏈接占用資源

  • --web.max-connections:最大連接數設置

  • --storage.tsdb.retention:prometheus采集數據后會保存在內存和硬盤當中,設定保留數據的時長,避免高消耗

  • --storage.tsdb.path:指定采集監控數據保存的磁盤路徑

  • --query.max-concurrency --query.timeout:這兩個參數是用於在用戶執行查詢時,防止太多用戶同時查詢,也防止用戶執行過大的查詢而不退出,導致資源過載。

2、Prometheus容器化安裝

直接使用官方的鏡像啟動,並映射prometheus.yml配置文件到本地進行管理

[root@prometheus ~]# docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

還有另外一種方式是通過自定義鏡像方式啟動Prometheus

[root@prometheus ~]# vim dockerfile
FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/

[root@prometheus ~]# docker build -t my-prometheus .
[root@prometheus ~]# docker run -p 9090:9090 my-prometheus


免責聲明!

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



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