Docker部署Prometheus并启动
1. 编写Prometheus配置文件
位置:/etc/prometheus/prometheus.yml
# 全局配置
global:
# 默认拉取频率
scrape_interval: 15s
# 拉取超时时间
scrape_timeout: 10s
# 评估规则频率
evaluation_interval: 15s
# 规则文件配置
rule_files: ['/etc/prometheus/rules/*.yml']
# 告警配置
alerting:
alertmanagers:
- follow_redirects: true
scheme: http
timeout: 10s
api_version: v2
static_configs:
- targets: []
# 拉取配置,添加监控项
scrape_configs:
# 监控prometheus
- job_name: prometheus
metrics_path: /metrics
static_configs:
- targets:
- localhost:9090
# 监控ip是否能ping通
- job_name: icmp_ping
metrics_path: /probe
params:
module: [icmp]
file_sd_configs:
- files: ['/etc/prometheus/conf.d/icmp_ping/*.yml']
refresh_interval: 10s
relabel_configs:
- source_labels: [__address__]
regex: (.*)(:80)?
target_label: __param_target
replacement: ${1}
- source_labels: [__param_target]
target_label: instance
- source_labels: [__param_target]
regex: (.*)
target_label: ping
replacement: ${1}
- source_labels: []
regex: .*
target_label: __address__
replacement: 192.168.7.254:9115
# 监控端口是否能连通
- job_name: tcp_port
metrics_path: /probe
params:
module: [tcp_connect]
file_sd_configs:
- files: ['/etc/prometheus/conf.d/tcp_port/*.yml']
refresh_interval: 10s
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.7.254:9115
# 监控get请求
- job_name: http_get
metrics_path: /probe
params:
module: [http_2xx]
file_sd_configs:
- files: ['/etc/prometheus/conf.d/http_get/*.yml']
refresh_interval: 10s
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.7.254:9115
# 监控post请求
- job_name: http_post
metrics_path: /probe
params:
module: [http_post_2xx]
file_sd_configs:
- files: ['/etc/prometheus/conf.d/http_post/*.yml']
refresh_interval: 10s
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.7.254:9115
# 监控post请求,有参数{"token":"prometheus_post_check_token"}
- job_name: http_post_with_token
metrics_path: /probe
params:
module: [http_post_2xx_with_prometheus_post_check_token]
file_sd_configs:
- files: ['/etc/prometheus/conf.d/http_post_with_token/*.yml']
refresh_interval: 10s
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.7.254:9115
2. 运行启动命令
docker run --name prometheus \
-p 0.0.0.0:9090:9090 \
-v /etc/prometheus:/etc/prometheus \
--restart=always \
-itd ubuntu/prometheus:latest \
--config.file=/etc/prometheus/prometheus.yml --web.enable-lifecycle
安装blackbox_exporter
很必要,有这个可以监控web是否200、机器是否能ping通等
一、Docker安装blackbox_exporter(建议)
1. 编写blackbox_exporter配置文件
位置:/etc/blackbox-exporter/blackbox.yml
modules:
http_2xx:
prober: http
http_post_2xx:
prober: http
http:
method: POST
tcp_connect:
prober: tcp
pop3s_banner:
prober: tcp
tcp:
query_response:
- expect: "^+OK"
tls: true
tls_config:
insecure_skip_verify: false
grpc:
prober: grpc
grpc:
tls: true
preferred_ip_protocol: "ip4"
grpc_plain:
prober: grpc
grpc:
tls: false
service: "service1"
ssh_banner:
prober: tcp
tcp:
query_response:
- expect: "^SSH-2.0-"
- send: "SSH-2.0-blackbox-ssh-check"
irc_banner:
prober: tcp
tcp:
query_response:
- send: "NICK prober"
- send: "USER prober prober prober :prober"
- expect: "PING :([^ ]+)"
send: "PONG ${1}"
- expect: "^:[^ ]+ 001"
icmp:
prober: icmp
# 自定义配置
http_post_2xx_with_prometheus_post_check_token:
prober: http
http:
method: POST
headers:
Content-Type: application/json #添加头部
body: '{"token":"prometheus_post_check_token"}' #发送的相关数据
2. 执行运行命令自动安装
docker run --name prometheus-blackbox-exporter \
-p 0.0.0.0:9115:9115 \
-v /etc/blackbox-exporter/blackbox.yml:/opt/bitnami/blackbox-exporter/blackbox.yml \
--restart=always \
-itd bitnami/blackbox-exporter:latest
二、源码安装blackbox_exporter(不建议,还是用docker安装方便)
1. 到github下载black_expoter最新安装包
https://github.com/prometheus/blackbox_exporter/releases
2. 解压
tar -xvf blackbox_exporter-0.20.0.linux-amd64.tar.gz
3. 移动位置
mv blackbox_exporter-0.20.0.linux-amd64 /usr/local/blackbox_exporter
4. 查看版本,看是否安装成功
/usr/local/blackbox_exporter/blackbox_exporter --version
5. 添加service文件
sudo vim /usr/lib/systemd/system/blackbox_exporter.service
写入
[Unit]
Description=blackbox_exporter
[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure
6. 添加开机启动,并马上启动服务
sudo systemctl daemon-reload
sudo systemctl enable --now blackbox_exporter
7. 查看服务状态
sudo systemctl status blackbox_exporter
8. 查看black_expoter的端口9115是否启动
sudo netstat -tunlp
网友评论