Prometheus基于consul集群来实现自动服务发现

1 安装consul集群(docker)

1.1 安装docker-compose

pip3 install docker-compose
docker-compose version

1.2 安装consul集群

cat > /data0/consul/docker-compose.yaml << \EOF
version: '2'
networks:
  byfn:

services:
  consul1:
    image: consul
    container_name: node1
    volumes: 
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    networks:
      - byfn

  consul2:
    image: consul
    container_name: node2
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    ports:
       - 8500:8500
    depends_on:
        - consul1
    networks:
      - byfn

  consul3:
    image: consul
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -config-dir=/consul/config
    depends_on:
        - consul1
    networks:
      - byfn

  consul4:
    image: consul
    container_name: node4
    volumes:
      - /data0/consul/conf_with_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8501:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

  consul5:
    image: consul
    container_name: node5
    volumes:
      - /data0/consul/conf_without_acl:/consul/config
    command: agent -retry-join=node1 -node=ndoe5 -bind=0.0.0.0 -client=0.0.0.0 -ui -config-dir=/consul/config
    ports:
      - 8502:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn
EOF

cd /data0/consul/
docker-compose up -d

1.3 验证

docker-compose ps 
Name               Command               State                                                       Ports                                                     
-------------------------------------------------------------------------------------------------------------------------------------------
node1   docker-entrypoint.sh agent ...   Up      8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 8500/tcp, 8600/tcp, 8600/udp                                
node2   docker-entrypoint.sh agent ...   Up      8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8500->8500/tcp,:::8500->8500/tcp, 8600/tcp, 8600/udp
node3   docker-entrypoint.sh agent ...   Up      8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 8500/tcp, 8600/tcp, 8600/udp                                
node4   docker-entrypoint.sh agent ...   Up      8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8501->8500/tcp,:::8501->8500/tcp
,:::8501->8500/tcp, 8600/tcp, 8600/udp
node5   docker-entrypoint.sh agent ...   Up      8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8502->8500/tcp,:::8502->8500/tcp, 8600/tcp, 8600/udp

浏览器访问

Prometheus基于consul集群来实现自动服务发现

2 API注册服务到Consul

我在测试环境搭建了一个redis-exporter ,https://devopstack.cn/zabbix/2050.html
upsmart@lrma06:~/gtj/redis_exporter-v1.3.4.linux-amd64$ netstat -ntlp |grep 9121
tcp6       0      0 :::9121                 :::*                    LISTEN      32274/./redis_export

现在把这个实例注册到consul里
curl -X PUT -d '{"id": "redis-exporter-lrma06","name": "redis-exporter-1rma06","address": "192.168.66.167","port": 9121,"tags": ["service"],"checks": [{"http": "http://192.168.66.167:9121/metrics","interval": "5s"}]}' http://192.168.33.12:8502/v1/agent/service/register

# 查询指定节点以及指定的服务信息
curl http://192.168.33.12:8501/v1/catalog/service/redis-exporter-1rma06 

Prometheus基于consul集群来实现自动服务发现
浏览器查看
Prometheus基于consul集群来实现自动服务发现

#删除指定服务 redis为要删除服务的id
curl -X PUT  http://192.168.33.12:8502/v1/agent/service/deregister/redis-exporter-lrma06

3 配置Prometheus实现自动服务发现

    prometheus添加consul监控

    vi prometheus.yml
        #新增如下配置
       - job_name: 'consul-prometheus'
         consul_sd_configs:
           - server: '192.168.33.12:8502'
             services: []
重载配置文件
curl -XPOST http://192.168.33.12:9090/-/reload
查看prometheus监控targets
Prometheus基于consul集群来实现自动服务发现
可以看到,在Targets中能够成功的自动发现Consul中的Services信息,后期需要添加新的 Targets时,只需要通过API往Consul中注册服务即可,Prometheus就能自动发现该服务,是不是很方便。 也存在着问题,如下 会发现 Prometheus 同时加载出来了默认服务 consul,这个是不需要的。 默认只显示 job 及 instance 两个标签,其他标签都默认属于 before relabeling 下,有些必要的服务信息,也想要在标签中展示,该如何操作呢? 如果需要自定义一些标签,例如 team、group、project 等关键分组信息,方便后边 alertmanager 进行告警规则匹配,该如何处理呢? 所有 Consul 中注册的 Service 都会默认加载到 Prometheus 下配置的 consul_prometheus 组,如果有多种类型的 exporter,如何在 Prometheus 中配置分配给指定类型的组,方便直观的区别它们? 以上问题,我们可以通过 Prometheus 配置中的 relabel_configs 参数来解决。

4 配置relabel_configs实现自定义标签及分类

通过下面这种方式把api注册到consul中,可以添加各种业务标签,方便后期根据业务和各种情况分类。
$ vim consul-1.json
{
  "ID": "node-exporter",
  "Name": "node-exporter-192.168.66.167",
  "Tags": [
    "node-exporter"
  ],
  "Address": "192.168.66.167",
  "Port": 9100,
  "Meta": { 
    "app": "spring-boot",
    "team": "appgroup",
    "project": "bigdata"
  },
  "EnableTagOverride": false,
  "Check": {
    "HTTP": "http://192.168.66.167:9100/metrics",
    "Interval": "10s"
  },
  "Weights": {
    "Passing": 10,
    "Warning": 1
  }
}

 更新注册服务
$ curl --request PUT --data @consul-1.json http://192.168.33.12:8502/v1/agent/service/register?replace-existing-checks=1

$ vim consul-2.json
{
  "ID": "redis-exporter",
  "Name": "cadvisor-exporter-192.168.66.167",
  "Tags": [
    "redis-exporter"
  ],
  "Address": "192.168.66.167",
  "Port": 9121,
  "Meta": {
    "app": "redis",
    "team": "ops",
    "project": "mid"
  },
  "EnableTagOverride": false,
  "Check": {
    "HTTP": "http://192.168.66.167:9121/metrics",
    "Interval": "10s"
  },
  "Weights": {
    "Passing": 10,
    "Warning": 1
  }
}

 注册服务
$ curl --request PUT --data @consul-2.json  http://192.168.33.12:8502/v1/agent/service/register?replace-existing-checks=1

查看注册结果

Prometheus基于consul集群来实现自动服务发现

我们修改 prometheus.yml 配置如下:

  - job_name: 'consul_redis'
    consul_sd_configs:
      - server: '192.168.33.12:8502'
        services: []  
    relabel_configs:
      - source_labels: [__meta_consul_tags]
        regex: .*redis-exporter.*
        action: keep
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap

  - job_name: 'consul_node'
    consul_sd_configs:
      - server: '192.168.33.12:8502'
        services: []
    relabel_configs:
      - source_labels: [__meta_consul_tags]
        regex: .*node-exporter.*
        action: keep
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap

curl -XPOST http://192.168.33.12:9090/-/reload

浏览器验证分类是否成功

Prometheus基于consul集群来实现自动服务发现

如上图,已分类成功。

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: