Prometheus 之Consul服务发现(6)
上一节 Prometheus pushgateway 代理(5)
介绍
在 Prometheus 中添加监控,需要手动修改 prometheus.yml 配置,也就是说以后每增加一个服务,就得手动修改此配置,并重启Prometheus 服务,十分的麻烦,那么如何做到动态的监听服务呢?这就要借助于服务发现与注册了,常见的有zookeeper、etcd、consul等,我们这里使用consul借助consul实现动态监听服务的功能,当然 Prometheus 支持的服务发现比较多详见官方文档。
consul服务的使用详见
Consul安装
mkdir /data/consul/ wget https://releases.hashicorp.com/consul/1.4.4/consul_1.4.4_linux_amd64.zip unzip consul_1.4.4_linux_amd64.zip mkdir conf vim conf/config.json { "acl": { "enabled": true, "default_policy": "deny", # 策略,默认是allow "down_policy": "extend-cache", "tokens": { "agent": "2bb528e1-9b16-b1dc-ed35-7207865c732a" # agent会使用这个token和consul server进行请求 } } }
默认情况下consul web ui是可以直接访问做各种操作的,所以需要一个ACL来限制操作权限,就像“登录”了一样。
官方文档:https://learn.hashicorp.com/consul/security-networking/production-acls
当acl_default_policy为deny是,默认的api(写)行为都会被阻止,我们可以配置其子项,让比如配置key可写,这样在全部deny的情况下,出现了一个默认可写的行为,这就是白名单,反之,当acl_default_policy为allow时,默认行为都是允许的,我们可以配置子项使它某些行为为deny,这就是黑名单,若要开启all,acl_default_policy需要设置成deny。
Consul启动
vim /usr/lib/systemd/system/consul.service [Unit] Description=consul server daemon Documentation=https://www.consul.io/docs/index.html After=network.target [Service] ExecStart=/data/consul/bin/consul agent -ui -node=node1 -server -bind=127.0.0.1 -bootstrap-expect 1 -config-dir /data/consul/conf.d/ -data-dir /data/consul/data/ -config-file /data/consul/conf/config.json ExecReload=/bin/kill -HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target # 启动 systemctl daemon-reload systemctl start consul.service
-bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap公用
-bind:用来在集群内部的通讯,默认是0.0.0.0
-config-file:指定配置文件
-data-dir:指定一个目录用来存放agent的状态,所有的agent允许都需要该目录,该目录必须是稳定的,系统重启后都继续存在
-server:定义agent运行在server模式,每个集群至少有一个server,建议每个集群的server不要超过5个
-dc:agent允许的datacenter的名称,默认是dc1
Consul配置ACL
./consul acl bootstrap # 创建一个全局token global-management and SecretID (只能执行一次) AccessorID: 815a7dcb-be9a-e36a-2de3-c62eeaa638f8 SecretID: 969ec772-b115-c819-111f-2b382dc5bc15 Description: Bootstrap Token (Global Management) Local: false Create Time: 2019-06-24 16:04:20.785070812 +0800 CST Policies: 00000000-0000-0000-0000-000000000001 - global-management # 命令行使用 export CONSUL_HTTP_TOKEN=969ec772-b115-c819-111f-2b382dc5bc15 ./consul acl token list AccessorID: 815a7dcb-be9a-e36a-2de3-c62eeaa638f8 Description: Bootstrap Token (Global Management) Local: false Create Time: 2019-06-24 16:04:20.785070812 +0800 CST Legacy: false Policies: 00000000-0000-0000-0000-000000000001 - global-management AccessorID: 00000000-0000-0000-0000-000000000002 Description: Anonymous Token Local: false Create Time: 2019-06-24 16:03:44.871737561 +0800 CST Legacy: false # API使用 curl -H "X-Consul-Token: 969ec772-b115-c819-111f-2b382dc5bc15" \ http://127.0.0.1:8500/v1/acl/token/self
访问http://192.168.8.152:8500/ui/dc1/acls/tokens, 输入 SecretID 点击Save保存
Consul创建agent使用Token
curl --request PUT --header "X-Consul-Token: 969ec772-b115-c819-111f-2b382dc5bc15" \ --data '{ "Name": "Agent Token", "Type": "client", "Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }" }' http://192.168.8.152:8500/v1/acl/create {"ID":"2bb528e1-9b16-b1dc-ed35-7207865c732a"} # 或 ./consul acl policy create -name "agent-token" -description "Agent Token Policy" -rules @agent-policy.hcl cat agent-policy.hcl node_prefix "" { policy = "write" } service_prefix "" { policy = "read" }
Prometheus配置Consul
vim prometheus.yml # 添加如下配置 consul_sd_configs: - server: 192.168.8.152:8500 token: '969ec772-b115-c819-111f-2b382dc5bc15' services: [] relabel_configs: - source_labels: [__address__] regex: 192.168.8.152:8300 action: drop - source_labels: [__meta_consul_tags] regex: ".*,prod,.*" replacement: prod action: replace target_label: env # 添加完成后,重新加载配置 systemctl reload prometheus.service
Consul注册服务测试Prometheus是否有新的实例
# 注册web01服务 curl -X PUT --header "X-Consul-Token: 969ec772-b115-c819-111f-2b382dc5bc15" -d \ '{ "id": "web01", "name": "web", "address": "1192.168.8.119", "port": 80, "env": ["prod"] }' http://192.168.8.152:8500/v1/agent/service/register # 销毁web01服务 curl -X PUT --header "X-Consul-Token: 969ec772-b115-c819-111f-2b382dc5bc15" \ http://192.168.8.152:8500/v1/agent/service/deregister/web01
打开Prometheus web 查看新注册的实例有没有监控数据,至此 Prometheus 使用 Consul 服务发现完成