共計(jì) 2899 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
這篇文章主要介紹“基于 prometheus 如何監(jiān)控 nginx”,在日常操作中,相信很多人在基于 prometheus 如何監(jiān)控 nginx 問(wèn)題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”基于 prometheus 如何監(jiān)控 nginx”的疑惑有所幫助!接下來(lái),請(qǐng)跟著丸趣 TV 小編一起來(lái)學(xué)習(xí)吧!
安裝準(zhǔn)備
nginx 服務(wù)器:172.22.3.164
prometheus 主機(jī)已安裝 prometheus 和 grafnana
基于 prometheus 監(jiān)控 nginx 可選兩個(gè) exporter,一個(gè)是通過(guò) nginx_exporter 主要是獲取 nginx-status 中的內(nèi)建的指標(biāo),nginx 自身提供 status 信息,較為簡(jiǎn)單,promethues 中對(duì)應(yīng)的 metrics 也較少。另外一個(gè)是可以通過(guò) nginx-vts-exporter 監(jiān)控更多的指標(biāo),但 nginx-vts-exporter 依賴在編譯 nginx 的時(shí)候添加 nginx-module-vts 模塊來(lái)實(shí)現(xiàn)。vts 提供了訪問(wèn)虛擬主機(jī)狀態(tài)的信息,包含 server,upstream 以及 cache 的當(dāng)前狀態(tài),顯然指標(biāo)會(huì)更豐富一些。本實(shí)踐主要是介紹基于 prometheus 使用 nginx-vts-exporter 對(duì) nginx 進(jìn)行監(jiān)控。
相關(guān)安裝文件
https://github.com/vozlt/nginx-module-vts/archive/v0.1.18.tar.gz
https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
nginx 添加 nginx-module-vts 支持
# cd /opt/nginx-1.19.6
# wget https://github.com/vozlt/nginx-module-vts/archive/v0.1.18.tar.gz
# tar -zxf v0.1.18.tar.gz
## 重新編譯
# ./configure --add-module=nginx-module-vts-0.1.18/
# make
# cp objs/nginx /usr/local/nginx/sbin/nginx
修改 nginx.conf
http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
server {
listen 10011;
server_name localhost;
# vhost_traffic_status off;
location /nginx_status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
重啟 nginx 后,訪問(wèn) http://172.22.3.164:10011/stub_status
安裝 nginx-vts-exporter
在 nginx 服務(wù)器安裝 nginx-vts-exporter
# cd /opt/
# wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
# tar -zxf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
# cp nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/bin
創(chuàng)建 service
## 創(chuàng)建 service
# vi /usr/lib/systemd/system/nginx-vts-exporter.service
[Unit]
Description=nginx-vts-exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/nginx-vts-exporter \
-nginx.scrape_timeout 10 \
-nginx.scrape_uri http://127.0.0.1:10011/nginx_status/format/json
Restart=on-failure
[Install]
WantedBy=multi-user.target
設(shè)置自動(dòng)啟動(dòng)
# systemctl daemon-reload
## 設(shè)置自啟動(dòng)
# systemctl enable nginx-vts-exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx-vts-exporter.service to /usr/lib/systemd/system/nginx-vts-exporter.service.
配置 nginx-vts-exporter 監(jiān)控
修改 prometheus 配置文件 prometheus.yml,在 scrape_configs 下增加如下配置
- job_name: nginx-hosts
file_sd_configs:
- files: [/opt/prometheus/sd_config/nginx-hosts.yml]
refresh_interval: 5s
新增 /opt/prometheus/sd_config/docker-hosts.yml 文件,內(nèi)容如下
cat /opt/prometheus/sd_config/nginx-hosts.yml
- targets:
- 172.22.3.164:9913
重啟 prometheus
# systemctl restart prometheus
瀏覽器地址欄輸入 http://172.22.3.148:9090/targets
可以看到 targets 已經(jīng)增加了對(duì)服務(wù) nginx-hosts 的監(jiān)控
在 grafana 中配置面板顯示對(duì)主機(jī) nginx-hosts 監(jiān)控內(nèi)容
點(diǎn)“+”按鈕,彈出導(dǎo)入面板窗口
如圖輸入 2949,點(diǎn) Load,grafana 會(huì)直接從官方網(wǎng)站導(dǎo)入編號(hào)為 2949 的面板如下
選擇數(shù)據(jù)源 Prometheus,繼續(xù)“Import”,顯示 nginx 監(jiān)控界面顯示如下
到此,關(guān)于“基于 prometheus 如何監(jiān)控 nginx”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!