第九講內容如下
1)prometheus服務端安裝和后台穩定運行
2)prometheus服務端配置文件寫法
3)node_exporter安裝和后台運行
4)node_exporter觀察和采集數據
5)prometheus查詢采集回來的各種數據
6) 使用我們之前學過的prometheus命令行的形式練習組合各種監控圖
(一)prometheus服務端的安裝和后台穩定運行
下載地址:https://github.com/prometheus/prometheus/releases/tag/v2.10.0
解壓
mv prometheus-2.10.0.linux-amd64.tar.gz /usr/local/ cd /usr/local/ tar -xf prometheus-2.10.0.linux-amd64.tar.gz mv prometheus-2.10.0.linux-amd64 prometheus
運行
cd prometheus ./prometheus
我們需要讓prometheus_server 運⾏在后台 ⽽不是前端
第一種方法安裝screen
yum -y install screen
使用screen啟動
#進入screen screen #啟動prometheus ./prometheus
ctrl+a+d退出
查看放入后台的進程
screen -ls
screen還有另外⼀個好處 就是 可以隨時切換進⼊ 程序前台窗 ⼜ 查看各種調試信息
screen -r
screen 也有不好的地⽅ • 不夠正規化 總覺得還是個臨時辦法
• screen -l 提供的后台 列表 不夠⼈性化,很多時候 你記不 住 到底哪個是哪個
• 很容易被誤關閉 操作的時候 ctrl +ad / ctrl +d 不⼩⼼操作 錯了 直接就退出去了
方法二
使⽤daemonize 放⼊后台⽅式
編譯安裝
git clone git://github.com/bmc/daemonize.git && cd daemonize ./configure && make && make install daemonize -v
指定運行啟動腳本
# cat /usr/local/prometheus/up.sh /usr/local/prometheus/prometheus --config.file="prometheus.yml" --web.listen-address="0.0.0.0:9090" --web.read-timeout=5m --web.max-connections=10 --storage.tsdb.retention=15d --storage.tsdb.path="/usr/local/prometheus/data" --query.max-concurrency=20 --query.timeout=2m
參數解釋
–config.file=“prometheus.yml” 指定配置文件 –web.read-timeout=5m 請求鏈接的最大等待時間,防止太多的空閑鏈接占用資源 –web.max-connections=512 針對prometheus,獲取數據源的時候,建立的網絡鏈接數,做一個最大數字的限制,防止鏈接數過多造成資源過大的消耗 –storage.tsdb.retention=15d 重要參數,prometheus 開始采集監控數據后,會存在內存和硬盤中;對於保存期限的設置。時間過長,硬盤和內存都吃不消;時間太短,要查歷史數據就沒了。企業15天最為合適。 –storage.tsdb.path="/usr/local/prometheus/data" 存儲數據路徑,不要隨便定義 –query.max-concurrency=20 用戶查詢最大並發數 –query.timeout=2m 慢查詢強制終止
設置up.sh執行權限
chmod +x /usr/local/prometheus/up.sh
后台啟動
daemonize -c /usr/local/prometheus /usr/local/prometheus/up.sh
查看進程是否啟動
放入開機自啟動
# cat /etc/rc.local touch /var/lock/subsys/local daemonize -c /usr/local/prometheus /usr/local/prometheus/up.sh
重啟正常啟動即可
prometheus對時間比較敏感,需要設置ntpdate隨時同步時間
數據目錄
其中 這些 長串字母的 是歷史數據保留 ⽽ 當前近期數據 實際上保留在內存中
並且 按照⼀定間隔 存放在 wal / ⽬錄中 防⽌突然斷電 或者 重 啟 以⽤來 恢復內存中的數據
(⼆) prometheus 服務端配置⽂件寫法
配置文件
# cat prometheus.yml # my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. 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','192.168.1.101:9100','192.168.1.102:9100','192.168.1.11:9100']
注意:targets也可以使用域名,前提是需要設置好DNS或者是修改了本機的hosts
(三) node_exporter 安裝和后台運⾏
下載地址:https://github.com/prometheus/node_exporter/releases/tag/v0.15.2
解壓運行
tar -xf node_exporter-0.15.2.linux-amd64.tar.gz mv node_exporter-0.15.2.linux-amd64 node_exporter cd node_exporter ./node_exporter &
設置systemctl啟動
# cat /usr/lib/systemd/system/node_exporter.service [Unit] Description=Prometheus Node Exporter After=network.target [Service] ExecStart=/usr/local/node_exporter/node_exporter User=nobody [Install] WantedBy=multi-user.target
啟動和設置成自啟動
systemctl start node_exporter systemctl enable node_exporter
默認運行端口是9100
本機可以使用curl目錄查看數據
curl localhost:9100/metrics
然后 我們去到 node_exporter在 github上的地址 來看看 我們偉⼤的社區開發者們 都給咱們提供了 哪些有⽤的 采集項⽬
https://github.com/prometheus/node_exporter
(五)prometheus查詢采集回來的各種數據
接下來 我們回到 prometheus的主界⾯ 驗證⼀下 我們新部署的 監控機器上的node_exporter 是否給我 們 正確返回了 數據 隨便挑⼏個 key 就可以查看
另外 prometheus 的命令⾏ 本⾝也⽀持suggest 功能(輸⼊提 ⽰)
隨便找個key 查詢⼀下 是否有輸出圖輸出 就可以了 本⾝node_exporter提供的 keys 實在太多了 (因為 都是從 Linux系統中的 底層 各種挖掘數據回來) 我們沒有時間 也沒有必要 把每⼀個key 都掌握 只要知道 ⼀ 部分重要的 必須的key 就⾜夠了
(六) 使⽤我們之前的學過的 prometheus 命令⾏的形式 練習 組合各種監控圖
接下來 咱們找⼀個 ⽐較重要的key 然后 ⽤我們學過的 命令⾏ ⽅式 給他組成⼀個 臨時監控圖
比如
node_cpu
node_memory
node_disk