環(huán)境:
OS:Centos7
Docker: 20.10.9 - Community
Centos部署Docker
【Kubernetes】Centos中安裝Docker和Minikube_云服務(wù)器安裝docker和minikube_DivingKitten的博客-CSDN博客
一、拉取Prometheus鏡像
## 拉取鏡像
docker pull prom/prometheus
## 啟動promtheus
docker run --name prometheus -p 9090:9090 -d prom/prometheus
## copy 配置文件至本地
docker cp prometheus:/etc/prometheus/prometheus.yml ./
用瀏覽器訪問Prometheus前端:http://192.168.56.110:9090
二、部署node_exporter
1、安裝服務(wù)
從github下載發(fā)行版,通過systemd進(jìn)行管理。
github:node_exporter
## 從github下載發(fā)行版
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
## 解壓文件
tar -xf node_exporter-1.6.1.linux-amd64.tar.gz
## 創(chuàng)建軟鏈接方便以后升級
ln -s ./node_exporter-1.6.1.linux-amd64 ./node_exporter
創(chuàng)建service文件,保存在/usr/lib/systemd/system/
目錄下
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/root/docker/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
1、ExecStart表示執(zhí)行文件位置,根據(jù)實(shí)際情況具體修改
2、node_exporter默認(rèn)監(jiān)控端口是9100,如需修改端口,可以追加啟動參數(shù):
–web.listen-address=:8100
啟動node_exporter服務(wù),并設(shè)置為開機(jī)器自動啟動
systemctl daemon-reload
systemctl start node-exporter
systemctl enable node-exporter
## 檢查端口
$ netstat -nltp | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 23431/node_exporter
## 檢查監(jiān)控可用性
$ curl http://127.0.0.1:9100/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.0135e-05
go_gc_duration_seconds{quantile="0.25"} 3.6243e-05
go_gc_duration_seconds{quantile="0.5"} 3.9298e-05
go_gc_duration_seconds{quantile="0.75"} 4.4272e-05
go_gc_duration_seconds{quantile="1"} 0.000111722
go_gc_duration_seconds_sum 0.080724852
go_gc_duration_seconds_count 1900
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.19.1"} 1
.....
....
2、配置target
在Prometheus配置scrape_configs中追加target
scrape_configs:
- job_name: "node_exporter"
static_configs:
- targets: ["192.168.56.110:9100"]
由于Prometheus運(yùn)行在容器中,所以不能通過127.0.0.1找到宿主機(jī)上的node_exporter,可以通過宿主機(jī)的網(wǎng)卡IP進(jìn)行訪問
然后將配置拷貝到容器中,重啟Prometheus或者調(diào)用接口重載配置
## 覆蓋配置
docker cp ./prometheus.yml prometheus:/etc/prometheus/prometheus.yml
## 重啟Prometheus
docker restart prometheus
如果是將配置文件掛在到容器中,則不需要每次都copy配置至容器內(nèi)
三、部署B(yǎng)lackbox_exporer
1、安裝服務(wù)
流程和node_exporter部署相同,從github下載發(fā)行版,通過systemd進(jìn)行管理。
github:blackbox_exporter
## 從github下載發(fā)行版
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.23.0/blackbox_exporter-0.23.0.linux-amd64.tar.gz
## 解壓文件
tar -xf blackbox_exporter-0.23.0.linux-amd64
## 創(chuàng)建軟鏈接方便以后升級
ln -s ./blackbox_exporter-0.23.0.linux-amd64 ./blackbox_exporter
創(chuàng)建service文件,保存在/usr/lib/systemd/system/
目錄下
[Unit]
Description=Prometheus blackbox Exporter
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/root/docker/prometheus/blackbox_exporter/blackbox_exporter --config.file=/root/docker/prometheus/blackbox_exporter/blackbox.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
1、ExecStart表示執(zhí)行文件位置,根據(jù)實(shí)際文件位置修改
2、–config.file根據(jù)實(shí)際blackbox_exporter配置配置修改
2、node_exporter默認(rèn)監(jiān)控端口是9115,如需修改端口,可以追加啟動參數(shù):
–web.listen-address=:9195
啟動blackbox_exporter服務(wù),并設(shè)置為開機(jī)器自動啟動
systemctl daemon-reload
systemctl start node-exporter
systemctl enable node-exporter
## 檢查端口
[root@VM-8-11-centos system]# netstat -nltp | grep 9115
tcp6 0 0 :::9115 :::* LISTEN 10150/blackbox_expo
2、配置target
在Prometheus配置scrape_configs中追加target
scrape_configs:
- job_name: "blackbox_exporter"
metrics_path: /probe
static_configs:
- targets: ["192.168.56.110:9195"]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
由于Prometheus運(yùn)行在容器中,所以不能通過127.0.0.1找到宿主機(jī)上的node_exporter,可以通過宿主機(jī)的網(wǎng)卡IP進(jìn)行訪問
然后重啟Prometheus或者調(diào)用接口重載配置
## 覆蓋配置
docker cp ./prometheus.yml prometheus:/etc/prometheus/prometheus.yml
## 重啟Prometheus
docker restart prometheus
四、部署Grafana
1、安裝服務(wù)
拉取鏡像并啟動
docker pull grafana/grafana-oss
docker run -d -p 3000:3000 --name grafana grafana/grafana-oss
初次登錄沒有密碼,然后設(shè)置數(shù)據(jù)源來自Prometheus,測試通過才會正式保存。
2、添加視圖
在grafana的dashboard市場里有很多現(xiàn)成的視圖,可以直接導(dǎo)入,node_exporter和blackbox_exporter都有現(xiàn)成的視圖
官方dashboard市場:
Dashboards | Grafana Labs
Node_exporter視圖:
Node Exporter Full | Grafana Labs
Blackbox_exporter視圖:
Prometheus Blackbox Exporter | Grafana Labs
導(dǎo)航到Dashboard–> import ,填入從官網(wǎng)copy的ID,本地的grfana會自動到官網(wǎng)下載對應(yīng)dashboard的json信息
選擇好默認(rèn)的數(shù)據(jù)源,點(diǎn)擊import等待一會兒,就會自動跳轉(zhuǎn)到對應(yīng)的監(jiān)控視圖上
在search dashboard導(dǎo)航里也可以找到剛加的監(jiān)控視圖
五、部署Alertmanager
1、部署服務(wù)
alertmanager和Prometheus是獨(dú)立發(fā)布的,使用前需要確認(rèn)版本兼容關(guān)系,一般情況最新版之間都是兼容的
## 拉取鏡像
docker pull prom/alertmanager
## 啟動容器
docker run --name alertmanager -d -p 9093:9093 prom/alertmanager
訪問前端頁面
2、配置Prometheus
在Prometheus配置中修改alertmanager的IP,由于alertmanager也運(yùn)行在容器當(dāng)中,因此也通過宿主機(jī)IP+端口
的方式進(jìn)行訪問
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.56.110:9093
3、添加告警規(guī)則
然后添加告警規(guī)則,創(chuàng)建rules.yml文件
groups:
- name: Instance
rules:
- alert: InstanceDown
expr: up != 1
for: 1m
labels:
severity: info
annotations:
summary: "{{ $labels.instance }}"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been in an unhealthy state for more than 1 minutes."
然后將rules文件復(fù)制到容器中,確認(rèn)Prometheus已添加rules文件,重啟Prometheus
rule_files:
- "/etc/prometheus/rules.yml"
docker cp ./rules.yml prometheus:/etc/prometheus/rules.yml
docker restart prometheus
然后可以在Prometheus前端查看到剛配置的告警規(guī)則和告警觸發(fā)情況
五、模擬觸發(fā)告警
前面添加的規(guī)則時當(dāng)有一個target的監(jiān)控任務(wù)指標(biāo)up!=0
觸發(fā),因此手動停止blackbox_exporter來觸發(fā)一個告警。
停用blackbox_exporter之前,檢索up
停用blackbox_exporter
systemctl stop blackbox-exporter
當(dāng)blackbox_exporter停用后,可以看到帶有l(wèi)able:job="blackbox_exporter"的up數(shù)據(jù)變成0,觸發(fā)了告警
由于告警規(guī)則中for: 1m
1分鐘后發(fā)送告警,因此alertmanager需要再觸發(fā)告警規(guī)則(up!=1
)一分鐘后才會收到告警信息。
重新啟動blackbox_exporter文章來源:http://www.zghlxwxcb.cn/news/detail-703093.html
systemctl start blackbox-exporter
告警已經(jīng)關(guān)閉文章來源地址http://www.zghlxwxcb.cn/news/detail-703093.html
到了這里,關(guān)于Docker環(huán)境搭建Prometheus實(shí)驗(yàn)環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!