部署consul集群

三台主机IP分别是10.0.0.17  10.0.0.6  10.0.0.15

配置consul

wget https://releases.hashicorp.com/consul/1.13.0/consul_1.13.0_linux_amd64.zip
mv consul_1.13.0_linux_amd64.zip /usr/local/src/
cd /usr/local/src && unzip consul_1.13.0_linux_amd64.zip
cp consul /usr/loucal/bin/
scp consul 10.0.0.15:/usr/loucal/bin/
scp consul 10.0.0.6:/usr/loucal/bin/

创建数据目录三台都创建

mkdir -p /data/consul

运行

#第一台
nohup consul agent -server -bootstrap -bind=10.0.0.6 -client=10.0.0.6 -data-dir=/data/consul -ui -node=10.0.0.6 &
#第二台
nohup consul agent -bind=10.0.0.17 -client=10.0.0.17 -data-dir=/data/consul -node=10.0.0.17 -join=10.0.0.6 &
#第三台
nohup consul agent -bind=10.0.0.15 -client=10.0.0.15 -data-dir=/data/consul -node=10.0.0.15 -join=10.0.0.6 &

浏览器直接访问第一台10.0.0.6:8500端口 

三台consul安装node_exporter

wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
tar xvf node_exporter-1.3.1.linux-amd64.tar.gz 
ln -sv /apps/node_exporter-1.3.1.linux-amd64 /apps/node_exporter
# cat /etc/systemd/system/node-exporter.service
[Unit]
Description=Prometheus Node Exporter
After=network.target

[Service]
ExecStart=/apps/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target

# systemctl start node-exporter.service
# systemctl enable node-exporter.service

consul服务注册

curl -X PUT -d '{"id": "node-exporter6","name": "node-exporter6","address": "10.0.0.6","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.0.0.6:9100","interval": "5s"}]}' http://10.0.0.6:8500/v1/agent/service/register
curl -X PUT -d '{"id": "node-exporter17","name": "node-exporter17","address": "10.0.0.17","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.0.0.17:9100","interval": "5s"}]}' http://10.0.0.6:8500/v1/agent/service/register
curl -X PUT -d '{"id": "node-exporter15","name": "node-exporter15","address": "10.0.0.15","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.0.0.15:9100","interval": "5s"}]}' http://10.0.0.6:8500/v1/agent/service/register

 

部署prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar xvf prometheus-2.37.0.linux-amd64.tar.gz

创建软连接

ln -sv /apps/prometheus-2.37.0.linux-amd64 /apps/prometheus
cat /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
Restart=on-failure
WorkingDirectory=/apps/prometheus/
ExecStart=/apps/prometheus/prometheus   --config.file=/apps/prometheus/prometheus.yml --web.enable-lifecycle

[Install]
WantedBy=multi-user.target

启动

systemctl daemon-reload
systemctl restart prometheus
systemctl enable prometheus

修改prometheus.yml

  - job_name: consul
    honor_labels: true
    metrics_path: /metrics
    scheme: http
    consul_sd_configs:
      - server: 10.0.0.6:8500
        services: [] #发现的目标服务器名称,空为所有服务,可以写servicea,serviceb,servicec
      - server: 10.0.0.17:8500
        services: []
      - server: 10.0.0.15:8500
        services: []
    relabel_configs:
    - source_labels: ['__meta_consul_tags']
      target_label: 'product'
    - source_labels: ['__meta_consul_dc']
      target_label: 'idc'
    - source_labels: ['__meta_consul_service']
      regex: "consul"
      action: drop

重启

systemctl restart prometheus

查看prometheus

再多注册一台服务器,注册前提前装好node_exporter

curl -X PUT -d '{"id": "node-exporter10","name": "node-exporter10","address": "10.0.0.10","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.0.0.6:9100","interval": "5s"}]}' http://10.0.0.6:8500/v1/agent/service/register

登录prometheus可以看到已经多了一台服务器的监控

删除注册的服务的命令

curl --request PUT http://10.0.0.6:8500/v1/agent/service/deregister/node-exporter15

 

posted @ 2022-08-11 14:29  Maniana  阅读(110)  评论(0编辑  收藏  举报