門戶網(wǎng)站開發(fā)需求分析網(wǎng)絡(luò)營銷未來有哪些發(fā)展趨勢(shì)
準(zhǔn)備環(huán)境
在此專欄的前幾篇文章中已經(jīng)準(zhǔn)備了一臺(tái)服務(wù)器作為我們進(jìn)行環(huán)境的準(zhǔn)備.大家也可以通過虛擬機(jī)創(chuàng)建倆臺(tái)服務(wù)器,一臺(tái)作為Prometheus的安裝另外一臺(tái)進(jìn)行其他軟件安裝并且進(jìn)行監(jiān)控的服務(wù)器.
這里我就不贅述nginx的安裝教程,相信大家都可以搜到,使用docker或者直接通過安裝包解壓的方式都可以,我這里是通過docker的方式進(jìn)行安裝的,后面的操作其實(shí)都是大差不差的.
nginx開啟stub_status
- 監(jiān)控nginx需要with-http_stub_status_module這個(gè)模塊
首先檢查是否有安裝with-http_stub_status_module模塊
docker方式安裝
docker exec -it nginx nginx -v 2>&1 |grep -o with-http_stub_status_module
nginx安裝包方式安裝
nginx nginx -v 2>&1 |grep -o with-http_stub_status_module
nginx開啟stub_status配置
將下面配置文件寫到nginx.conf配置文件中:
server {.....location /stub_status {stub_status on;access_log off;allow 0.0.0.0/0;deny all;}....
}
重新加載配置文件
docker exec -it nginx nginx -s reload
檢查是否開啟成功
curl http://localhost/syub_status
成功如下圖:
安裝Exporter
在上篇文章中說了Prometheus需要監(jiān)控什么軟件需要對(duì)應(yīng)安裝的Exporter,當(dāng)然這里可以使用二進(jìn)制安裝也可以使用docker安裝.這里為了方便,還是選擇docker-compose的方式直接安裝
docker-compose方式進(jìn)行安裝
這里直接通過創(chuàng)建docker-compose.yaml然后進(jìn)行追加配置
cat >docker-compose.yaml <<FOF
version: '3.3'
services:nginx_exproter:image:nginx/nginx-prometheus-exporter:0.11container_name: nginx_exporterhostname: nginx_exportercommand:- '-nginx.scrape-uri=http://localhost/stub_status'restart: alwaysport:- "9113:9113"
EOF
啟動(dòng)
docker-compose up -d
檢查
查看正在運(yùn)行的容器
docker ps或者:查看nginx_exporter容器的運(yùn)行日志
docker logs -f nginx_exporter
metrics地址
安裝好Exporter后會(huì)暴露一個(gè)/metrics結(jié)尾的服務(wù)
名稱 | 地址 |
---|---|
nginx_exporter | http://localhost:9113/metrics |
Prometheus配置
配置Prometheus去采集(拉取)nginx_exporter的監(jiān)控樣本數(shù)據(jù)
cd /data/docker-prometheus# 在scrapc_configs(搜刮配置):下面增加如下配置:
cat >prometheus/prometheus.yml <<FOF- job_name: 'nginx_exporter'static_configs:- targets: ['localhost:9113']labels:instance: test服務(wù)器
EOF
重新加載配置
curl -x POST http://localhost:9090/-/reload
檢查
常用的nginx監(jiān)控指標(biāo)
nginx_connections_accepted 接受請(qǐng)求數(shù)
nginx_connections_active 活動(dòng)連接數(shù)
nginx_connections_handled 成功處理請(qǐng)求數(shù)
nginx_connections_reding 正在進(jìn)行讀操作的請(qǐng)求數(shù)
nginx_connections_waiting 正在等待的請(qǐng)求數(shù)
nginx_connections_writing 正在進(jìn)行寫操作的請(qǐng)求數(shù)
nginx_connections_requests 總請(qǐng)求數(shù)
添加觸發(fā)器
cd /data/docker-prometheus
cat >prometheus/alert.yml <<FOF-name: nginxrules:# 任何勢(shì)力超過30秒無法聯(lián)系的情況發(fā)出警報(bào)- alert: NginxDownexpr: nginx_up ==0for: 30slabels:severity: criticalannotations:summary:"nginx異常,實(shí)例:{{$labels.instance }}"description: "{{$lables.job}} nginx已關(guān)閉"
EOF
檢查:
vim prometheus/alert.yml
檢查配置
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml
重新加載配置
curl -x POST http://localhost:9090/-/reload
檢查
http://localhost:9090/alerts?search=
或:
http://localhost:9090/rules
dashboard
grafana展示Prometheus從nginx_exporter收集到的數(shù)據(jù)