- 部署實現(xiàn)
- Consul 搭建
(1)下載
wget https://releases.hashicorp.com/consul/1.9.4/consul_1.9.4_linux_amd64.zip
(2)解壓
unzip consul_1.9.4_linux_amd64.zip
(3)移動
mv consul /usr/bin/
(4)編寫Systemd service配置文件酒觅,用systemd控制或監(jiān)視進程
vim /etc/systemd/system/consul.service
- Consul 搭建
[Unit]
Description=Consul.
Documentation=https://www.consul.io
After=network.target
[Service]
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -server -bootstrap -data-dir=/data/consul/data -node=Consul -bind=0.0.0.0 -client=0.0.0.0 -ui
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
創(chuàng)建consul用戶嘉汰、數(shù)據(jù)目錄與配置文件目錄弟蚀,并配置token認證
useradd --no-create-home --shell /sbin/nologin consul
mkdir -p /data/consul/data
chown consul. /data/consul/data/
mkdir /etc/consul/
vim /etc/consul/acl.json
{
"acl": {
"enabled": true,
"default_policy": "deny",
"enable_token_persistence": true,
"tokens": {
"master": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx" //token自定義
}
}
}
啟動服務(wù)并設(shè)置開機自啟
systemctl daemon-reload
systemctl start consul.service
systemctl enable consul.service
修改配置文件,與consul音比、alertmanager連接
vim /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093 # Alertmanager 地址與端口號
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "/etc/prometheus/rules/*.rules" # 報警規(guī)則文件路徑
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'Instance'
consul_sd_configs: # 配置基于consul的服務(wù)發(fā)現(xiàn)
- server: localhost:8500 # consul地址
token: 8dc1eb67-1f5f-4e10-ad9d-5e58b047647c # 自定義的token
refresh_interval: 60s # 刷新間隔
services: [] # 當前需要發(fā)現(xiàn)哪些類型服務(wù)實例的信息,后面會做詳細配置
# 所有服務(wù)會先注冊到consul,prometheus通過基于Consul的服務(wù)發(fā)現(xiàn)監(jiān)控到target
啟動服務(wù)及設(shè)置開機自啟
systemctl start prometheus
systemctl enable prometheus
修改Prometheus配置苛萎,讓其支持基于consul的服務(wù)發(fā)現(xiàn)
vim /etc/prometheus/prometheus.yml
......
scrape_configs:
......
- job_name: 'Instance'
consul_sd_configs:
- server: localhost:8500
token: 8dc1eb67-1f5f-4e10-ad9d-5e58b047647c
refresh_interval: 60s
services: ['node_exporter','alertmanager_exporter','consul_exporter']
relabel_configs: # 對默認的Metadata進行自定義Relabeling
- source_labels: [__meta_consul_service_address]
target_label: 'ipaddress'
- source_labels: [__meta_consul_service_metadata_instance]
target_label: 'instance'
- source_labels: [__meta_consul_service_metadata_group]
target_label: 'group'
- source_labels: [__meta_consul_service_metadata_environment]
target_label: 'environment'
- source_labels: [__meta_consul_service_metadata_Project]
target_label: 'Project'
- source_labels: [__meta_consul_service]
target_label: 'service'
......
注冊被監(jiān)控端信息到consul
curl -X PUT -H "Content-Type: application/x-www-form-urlencoded" -H "X-Consul-Token: 8dc1eb67-1f5f-4e10-ad9d-5e58b047647c" -d @./node_exporter.json http://192.168.122.11:8500/v1/agent/service/register
node_exporter.json文件內(nèi)容如下:
{
"name": "node_exporter",
"id": "prometheus_node_exporter",
"address": "192.168.122.11",
"port": 9100,
"meta": {
"instance": "prometheus",
"group": "demo",
"environment": "UAT",
"Project": "Mon"
},
"checks": [{
"http": "http://192.168.122.11:9100/metrics",
"interval": "35s"
}]
}
#!/bin/bash
HOST=`hostname`
machine_physics_net=$(ls /sys/class/net/ | grep -v "`ls /sys/devices/virtual/net/`")
IP=$(ip addr | grep "$machine_physics_net" | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
server_name=`hostname | awk -F'.' '{print $1}'`
PUBLIC_IP=`curl icanhazip.com`
curl -X PUT -H "Content-Type: application/x-www-form-urlencoded" -H "X-Consul-Token: 8dc1eb67-1f5f-4e10-ad9d-5e58b047647c" -d '{"id": "'$HOST'","name": "'$HOST'","address": "'$IP'","port": 9100,"tags": ["linux_system"],"meta": {"factory": "google", "project": "'$server_name'", "host":"'$HOST'", "public_ip":"'$PUBLIC_IP'", "private_ip":"'$IP'", "monitoring-items": "linux_base"}, "checks": [{"http": "http://'$IP':9100/metrics", "interval": "5s"}]}' http://172.18.5.62:8500/v1/agent/service/register > /dev/null 2>&1 && echo -e "\033[92m $HOST 注冊成功 \033[0m"