Prometheus is based on consul service discovery

What is consul?

Consul is a distributed, highly available service registration system, composed of server and client. Each server and client is a node of consul. The consul client does not save data, and the client forwards the received request to The server side of the response. Servers realize data consistency through LAN or WAN communication.

Introduction to Prometheus service discovery based on consul

The service discovery process of prometheus based on consul is as follows:

(1) Register service or deregister service in consul (monitoring targets)

(2) Prometheus has been monitoring the consul service. When it finds new changes in the services that meet the requirements in consul, it will update the Prometheus monitoring objects

Install consul cluster

Create three virtual machines. Each machine can be configured with a 4-core CPU, 6G memory, and 60G hard disk. The host names are defined as master1, master2, and master3. The ip plan of my experimental environment is as follows:


ip of master1: 192.168.124.16 ip of master2: 192.168.124.26 ip of node1: 192.168.124.56

Deploy consul on the three nodes of master1, master2, and node1

master1 is consul's server, master2 is consul's client, node1 is consul's clie nt



1. Download the consul binary package and operate on each node

mkdir /opt/consul/data-p  && cd /opt/consul

wget https://releases.hashicorp.com/consul/1.7.1/consul_1.7.1_linux_amd64.zip

unzip consul_1.7.1_linux_amd64.zip

Note: Various versions of consul can be downloaded at https://releases.hashicorp.com/consul , 1.7.1 is the latest version

2. Start consul

1) On master1:

cd / opt / consul

./consul agent  -server  -bootstrap  -bind=192.168.124.16    -client=192.168.124.16  -data-dir=data  -ui  -node=192.168.124.16

This starts consul on master1

2) On master2:

cd / opt / consul

./consul agent -bind=192.168.124.26 -client=192.168.124.26     -data-dir=data -node=192.168124.26  -join=192.168.124.16

3) On node1:

cd / opt / consul

./consul agent-bind=192.168.124.56  -client=192.168.124.56    -data-dir=data -node=192.168.124.56  -join=192.168.124.16

After each node is started

Visit http://192.168.124.16:8500/ in the browser 

You can see the management interface of consul


3. Register the service to consul

Use the HTT PAPI method to register the node-exporter service to Consul, and you can test it on master1, provided that the master1 node needs to deploy node-exporter, which is not explained here. If you want to learn more, please refer to https://edu.51cto.com/sd /76993

Perform the following on the master1 node:

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

Note: The 192.168.124.16 seen above is the master1 node ip of consul

4. Remove the services registered in consul:

curl --request PUT http://192.168.124.16:8500/v1/agent/service/deregister/192.168.124.16

5. Prometheus configuration based on consul service discovery

The premise is that prometheus is required, and the execution is as follows on the machine where prometheus is deployed ,

Find the prometheus configuration file

The cat prometheus.yaml
configuration content is as follows:

scrape_configs:

  - job_name: consul

    metrics_path: /metrics

    scheme: http

    consul_sd_configs:

      - server: 192.168.124.16: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" #service matching "consul"

      action: drop #executed  action

    - source_labels: ['job']

      target_label: 'environment'

      regex:        '(.*)job'

      replacement:   '${1}'


If it is prometheus deployed by docker, the following methods can be used to restart prometheus

docker restart prometheus

docker restart node-exporter

6. View in prometheus web ui interface

http://192.168.124.16:9090/targets#job-prometheus

You can see the following, indicating that prometheus has used consul as a data source:

7. Prometheus configuration instructions based on consul service discovery:

    - source_labels:['__meta_consul_tags']

      target_label: 'product'

    - source_labels: ['__meta_consul_dc']

      target_label: 'idc'

    - source_labels: ['__meta_consul_service']

      regex: "consul" #service matching "consul"

      action: drop #executed  action

    - source_labels: ['job']

      target_label: 'environment'

      regex:        '(.*)job'

      replacement:   '${1}'

__meta_consul_tags: The tag list of the target connected by the tag separator

__meta_consul_dc: Target’s data center name

__meta_consul_service: The name of the service to which the target belongs

job': job of the target server

__meta_consul_service_port: the service port of the target

static_configs:  configuration data source

consul_sd_configs:  configuration based on consul service discovery

rebel_configs : re-mark

consul_sd_configs:

    -server: 192.168.124.16:8500 #This ip:port is the ip of the master1 node of consul


Guess you like

Origin blog.51cto.com/15127502/2655089