Cài đặt Blackbox Exporter
1. Tổng quan.
Blackbox Exporter là một exporter cho phép gọi tới các endpoint thông qua các giao thức HTTP, HTTPS, DNS, TCP và ICMP. Điều này có nghĩa có thể sử dụng nó để monitor các endpoint bất kì, có sử dụng một trong các giao thức trên.
Với Blackbox Exporter, chúng ta cần cấu hình các module thực thi việc thăm dò tới các target được định nghĩa ở Prometheus. Do cơ chế Service Discovery nên các target này được đặt ở Prometheus và ở Blackbox Exporter chúng ta chỉ cần cấu hình các module thôi
2. Link document
https://github.com/prometheus/blackbox_exporter/tree/master
3. Triển khai.
Tạo thư mục cho Prometheus
mkdir -p /opt/prometheus/data
mkdir -p /opt/prometheus/config
mkdir -p /opt/prometheus/blackbox/config
Phân quyền cho thư mục của Prometheus.
chown -R 65534:65534 /opt/prometheus/data
chmod -R 775 /opt/prometheus/data
Vào thư mục Prometheus
cd /opt/prometheus/
Tạo file /opt/prometheus/docker-compose.yml với nội dung dưới.
version: '3'
services:
blackbox:
image: prom/blackbox-exporter
container_name: blackbox
restart: always
tty: true
ports:
- 9115:9115
dns: 8.8.8.8
command: --config.file=/etc/blackbox/blackbox.yml
volumes:
- /opt/prometheus/blackbox/config/blackbox.yml:/etc/blackbox/blackbox.yml
Đây là ví dụ một file docker-compose.yml kết hợp đầy đủ các thành phần.
cat > /opt/prometheus/docker-compose.yml << 'OEF'
version: '3'
services:
prometheus:
image: prom/prometheus
container_name: prometheus
restart: always
network_mode: host
volumes:
- /opt/prometheus/data:/prometheus
- /opt/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
command:
- --config.file=/etc/prometheus/prometheus.yml
- --web.listen-address=:9090
- --storage.tsdb.retention.time=360d
influxdb:
image: influxdb:1.8
container_name: influxdb
hostname: influxdb
environment:
- INFLUXDB_CONFIG_PATH=/etc/influxdb/influxdb.conf
volumes:
- /opt/influxdb/config/influxdb.conf:/etc/influxdb/influxdb.conf
- /opt/influxdb/data:/var/lib/influxdb
restart: always
network_mode: host
blackbox:
image: prom/blackbox-exporter
container_name: blackbox
restart: always
tty: true
ports:
- 9115:9115
dns: 8.8.8.8
command: --config.file=/etc/blackbox/blackbox.yml
volumes:
- /opt/prometheus/blackbox/config/blackbox.yml:/etc/blackbox/blackbox.yml
grafana:
image: grafana/grafana
container_name: grafana
restart: always
network_mode: host
environment:
GF_SECURITY_ADMIN_PASSWORD: Aa@123567
ports:
- 3000:3000
OEF
Tạo file config cho InfluxDB.
cat > /opt/influxdb/config/influxdb.conf << 'OEF'
[meta]
dir = "/var/lib/influxdb/meta"
[data]
dir = "/var/lib/influxdb/data"
wal-dir = "/var/lib/influxdb/wal"
series-id-set-cache-size = 100
[coordinator]
[retention]
[shard-precreation]
[monitor]
[http]
bind-address = ":8086"
auth-enabled = false
[logging]
[subscriber]
[[graphite]]
[[collectd]]
[[opentsdb]]
[[udp]]
[continuous_queries]
[tls]
OEF
Define các modules vào file /opt/prometheus/blackbox/config/blackbox.yml.
cat > /opt/prometheus/blackbox/config/blackbox.yml << 'OEF'
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: "ip4"
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"
ssh_banner_extract:
prober: tcp
timeout: 5s
tcp:
query_response:
- expect: "^SSH-2.0-([^ -]+)(?: (.*))?$"
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
icmp_ttl5:
prober: icmp
timeout: 5s
icmp:
ttl: 5
OEF
Tạo file /opt/prometheus/config/prometheus.yml
cat > /opt/prometheus/config/prometheus.yml << 'OEF'
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
- job_name: 'blackbox'
scrape_interval: 10s
metrics_path: /probe
static_configs:
- targets:
- https://stacklab.vn
- https://tools.stacklab.vn
params:
module: [http_2xx]
relabel_configs:
- source_labels: [ __address__ ]
target_label: __param_target
- source_labels: [ __param_target ]
target_label: instance
- target_label: __address__
replacement: "localhost:9115"
OEF
Chạy stack.
docker-compose -f /opt/prometheus/docker-compose.yml up -d
4. Factory (Quy trình này xẽ xoá hết metrics cũ)
Xoá hết container.
docker-compose -f /opt/prometheus/docker-compose.yml down
Xoá hết metrics bằng cách loại bỏ các file DB nằm trong thư mục data của mỗi container.
rm -rf /opt/prometheus/data/*
Chạy lại stack.
docker-compose -f /opt/prometheus/docker-compose.yml up -d
Verify
docker ps | grep 'prometheus\|grafana'