prometheus 自动发现

 

目录

一:通过consul注册中心自动发现prometheus节点

1)部署consul集群

2)通过consul 的API数据写入数据

3)配置prometheus到consul的服务发现

二:通过file_sd_configs 配置文件自动注册


一:通过consul注册中心自动发现prometheus节点

1)部署consul集群

节点信息:

10.19.2.60 node1

10.19.2.61 node2

10.19.2.62 node3

#三个consul节点都要执行

cd /opt/consul
unzip consul_1.12.2_linux_amd64.zip

#创建consul数据目录
mkdir /data/consul -pv


#各consul节点分别执行启动命令
#启动consul服务
node1节点启动命令如下:

nohup ./consul agent -server -bootstrap -bind=10.19.2.60 -client=10.19.2.60 -data-dir=/data/consul -ui -node=10.19.2.60 &


node2节点启动命令如下:

nohup ./consul agent  -bind=10.19.2.61 -client=10.19.2.61 -data-dir=/data/consul -node=10.19.2.61 -join=10.19.2.60 &

node3节点启动命令如下:

nohup ./consul agent  -bind=10.19.2.62 -client=10.19.2.62 -data-dir=/data/consul -node=10.19.2.62 -join=10.19.2.60 &

2)通过consul 的API数据写入数据

#向consul 写入数据

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



#向consul 删除数据

curl --request PUT http://10.19.2.60:8500/v1/agent/service/deregister/node-export-14-9



验证数据信息

prometheus 自动发现_第1张图片

 

3)配置prometheus到consul的服务发现

  - job_name: consul
    honor_labels: true
    metrics_path: /metrics
    scheme: http
    consul_sd_configs:
       - server: 10.19.2.60:8500
         services: []
       - server: 10.19.2.61:8500
         services: []
       - server: 10.19.2.62: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

重启prometheus

systemctl restart prometheus

prometheus 自动发现_第2张图片

 

二:通过file_sd_configs 配置文件自动注册

#2.62上配置sd_config配置文件

vi /opt/prometheus/prometheus/file_sd/sd_my_server.json


[
  {
     "targets": ["10.19.14.21:9100", "10.19.14.22:9100", "10.19.14.22:8080", "10.19.14.21:8080", "10.19.14.21:30100"]
  }
]


#添加jod_name

  - job_name: "file_sd_config"
    file_sd_configs:
      - files:
        - /opt/prometheus/prometheus/file_sd/sd_my_server.json
        refresh_interval: 10s

#重启prometheus

systemctl restart prometheus

prometheus 自动发现_第3张图片

 

你可能感兴趣的:(prometheus,kubernetes,云原生)