目录
- 1 consule配置使用
- 1.1 Prometheus 支持的多种服务发现机制
- 1.2 安装
- 1、基于 docker 的 consul 集群 - docker-compose
- 2、单节点cosule安装-docker安装
- 1.3 使用
- 1、使用接口注册服务
- 2、配置 Prometheus 对接Consul 实现自动服务发现
- 3、使用接口注册服务2-JSON文件
- 4、relabel_action 的作用
- 1.4 测试
- 1.5 consul 常用
- 1、常用CLI
- 2、常用API
- 注册服务
- 查看所有注册服务列表
- 查看服务实例状态(最佳接口)
- 查询服务实例列表
- 查询注册节点情况
- 查看Consul集群成员 (常用来测试网络联通性)
- 查询Consul集群Leader(常用来判断集群状态)
1 consule配置使用
1.1 Prometheus 支持的多种服务发现机制
Prometheus数据源的配置主要分为静态配置和动态发现, 常用的为以下几类:
- 1)static_configs: #静态服务发现
- 2)file_sd_configs: #文件服务发现
- 3)dns_sd_configs: DNS #服务发现
- 4)kubernetes_sd_configs: #Kubernetes 服务发现
- 5)consul_sd_configs: Consul #服务发现
…
在监控kubernetes的应用场景中,频繁更新的pod,svc,等等资源配置应该是最能体现Prometheus监控目标自动发现服务的好处
1.2 安装
1、基于 docker 的 consul 集群 - docker-compose
这里使用 docker-compose 方式部署 consul 集群
mkdir -p /data0/consul; cd /data0/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
docker-compose up -d
# 访问:http://192.168.92.100:8502/
2、单节点cosule安装-docker安装
# 安装
docker run --name consul -d -p 8500:8500 consul
# 或者
consul agent -dev -ui -client 0.0.0.0
# 登录:http://172.16.0.242:8500/
1.3 使用
1、使用接口注册服务
# 注册服务
curl -X PUT -d '{"id": "redis","name": "redis","address": "192.168.92.100","port": 9121,"tags": ["service"],"checks": [{"http": "http://192.168.92.100:9121/","interval": "5s"}]}' http://192.168.92.100:8502/v1/agent/service/register
# 注册服务
curl -X PUT -d '{
"id": "node-exporter",
"name": "node-exporter-172-16-0-242",
"address": "172.16.0.242",
"port": 9100,
"tags": ["node2"],
"checks": [{"http": "http://192.168.92.100:9100/metrics", "interval": "5s"}]
}' http://192.168.92.100:8500/v1/agent/service/register
# 查询指定节点以及指定的服务信息
curl http://192.168.92.100:8500/v1/catalog/service/redis
[{"ID":"f2a99a8e-c9ee-c3d1-601b-75992d7097e6","Node":"ndoe5","Address":"172.18.0.5","Datacenter":"dc1","TaggedAddresses":{"lan":"172.18.0.5","lan_ipv4":"172.18.0.5","wan":"172.18.0.5","wan_ipv4":"172.18.0.5"},"NodeMeta":{"consul-network-segment":""},"ServiceKind":"","ServiceID":"redis","ServiceName":"redis","ServiceTags":["service"],"ServiceAddress":"192.168.92.100","ServiceTaggedAddresses":{"lan_ipv4":{"Address":"192.168.92.100","Port":9121},"wan_ipv4":{"Address":"192.168.92.100","Port":9121}},"ServiceWeights":{"Passing":1,"Warning":1},"ServiceMeta":{},"ServicePort":9121,"ServiceSocketPath":"","ServiceEnableTagOverride":false,"ServiceProxy":{"Mode":"","MeshGateway":{},"Expose":{}},"ServiceConnect":{},"CreateIndex":68,"ModifyIndex":68}]
#删除指定服务 redis为要删除服务的id
curl -X PUT http://192.168.92.100:8502/v1/agent/service/deregister/redis
2、配置 Prometheus 对接Consul 实现自动服务发现
# 现在 Consul 服务已经启动完毕,并成功注册了一个服务,接下来,我们需要配置 Prometheus 来使用 Consul 自动服务发现,目的就是能够将上边添加的服务自动发现到 Prometheus 的 Targets 中,增加 prometheus.yml 配置如下:
- job_name: 'consul-prometheus'
consul_sd_configs:
- server: '192.168.92.100:8500'
services: []
# 可以配置 relabel_configs 来实现标签过滤,只加载符合规则的服务。以上边为例,可以通过过滤 __meta_consul_tags 标签为 test 的服务,relabel_config 向 Consul 注册服务的时候,只加载匹配 regex 表达式的标签的服务到自己的配置文件。修改 prometheus.yml 配置如下:
- job_name: 'consul-prometheus'
consul_sd_configs:
- server: '192.168.92.100:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*test.*
action: keep
# 可以在上述案例的基础上修改标签
- job_name: consul
consul_sd_configs:
- server: '127.0.0.1:8500'
relabel_configs:
- source_labels: ['__meta_consul_service_metadata_project_code']
target_label: 'project_code'
- source_labels: ['__meta_consul_service_metadata_env_code']
target_label: 'env_code'
- source_labels: ['__meta_consul_service_metadata_role_code']
target_label: 'role_code'
- source_labels: ['__meta_consul_service_metadata_server_code']
target_label: 'server_code'
- source_labels: ['__meta_consul_service_metadata_server_name']
target_label: 'server_name'
- source_labels: ['__meta_consul_service_metadata_remote_ip']
target_label: 'remote_ip'
- source_labels: ['__meta_consul_service_metadata_monitor_type']
target_label: 'monitor_type'
- source_labels: ['__meta_consul_service_metadata_monitor_port']
target_label: 'monitor_port'
- source_labels: [__meta_consul_tags]
regex: '(.*port.*|.*ping.*)'
action: drop
# 重新加载配置文件
curl -XPOST http://192.168.92.100:9090/-/reload
# 如果返回:Lifecycle API is not enabled. 那么就是
#启动的时候没有开启热更新配置,需要在启动的命令行增加参数: --web.enable-lifecycle
# 需要修改启动文件 /lib/systemd/system/prometheus.service
systemctl restart prometheus.service; systemctl status prometheus.service
3、使用接口注册服务2-JSON文件
$ vim consul-0.json
{
"ID": "node-exporter",
"Name": "node-exporter-172.30.12.167",
"Tags": [
"test"
],
"Address": "172.30.12.167",
"Port": 9100,
"Meta": {
"app": "spring-boot",
"team": "appgroup",
"project": "bigdata"
},
"EnableTagOverride": false,
"Check": {
"HTTP": "http://172.30.12.167:9100/metrics",
"Interval": "10s"
},
"Weights": {
"Passing": 10,
"Warning": 1
}
}
$ curl --request PUT --data @a.json http://192.168.92.100:8500/v1/agent/service/register
# 以前的版本中需要加这个参数?replace-existing-checks=1,新版没有这个参数了,会直接覆盖原来的数据
4、relabel_action 的作用
...
- job_name: 'consul-prometheus'
consul_sd_configs:
- server: '172.30.12.167:8500'
services: []
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*test.*
action: keep
说明:
- replace: 根据 regex 的配置匹配 source_labels 标签的值(注意:多个 source_label 的值会按照 separator 进行拼接),并且将匹配到的值写入到 target_label 当中,如果有多个匹配组,则可以使用 ${1}, ${2} 确定写入的内容。如果没匹配到任何内容则不对 target_label 进行重新, 默认为 replace。
- keep: 丢弃 source_labels 的值中没有匹配到 regex 正则表达式内容的 Target 实例
- drop: 丢弃 source_labels 的值中匹配到 regex 正则表达式内容的 Target 实例
- hashmod: 将 target_label 设置为关联的 source_label 的哈希模块
- labelmap: 根据 regex 去匹配 Target 实例所有标签的名称(注意是名称),并且将捕获到的内容作为为新的标签名称,regex 匹配到标签的的值作为新标签的值
- labeldrop: 对 Target 标签进行过滤,会移除匹配过滤条件的所有标签
- labelkeep: 对 Target 标签进行过滤,会移除不匹配过滤条件的所有标签
1.4 测试
# 安装允许redis
docker run -d \
--name redis_exporter \
-p 9121:9121 \
oliver006/redis_exporter \
--redis.addr redis://172.16.90.9:6379 \
--redis.password 123456
# 注册到consul中
curl -X PUT -d '{
"id": "redis_exporter",
"name": "redis_exporter",
"address": "192.168.92.100",
"port": 9121,
"tags": ["test"],
"checks": [{"http": "http://192.168.92.100:9121", "interval": "5s"}]
}' http://192.168.92.100:8500/v1/agent/service/register
### 参数说明
# id : 注册ID 在consul中为唯一标识
# name :Service名称
# address:自动注册绑定ip
# port:自动注册绑定端口
# tags:注册标签,可多个
# checks : 健康检查
# http: 检查数据来源
# interval: 检查时间间隔
# http://192.168.0.181:8500/v1/agent/service/register192.168.0.181
# 查看
curl http://192.168.92.100:8500/v1/catalog/service/redis_exporter
# 重载
curl -X POST http://192.168.92.100:9090/-/reload
# 删除其下的一个instance
curl -X PUT http://192.168.92.100:8500/v1/agent/service/deregister/service_id
## 所有实例删除完毕,这个services就好默认删除
1.5 consul 常用
1、常用CLI
# 查看服务列表
consul catalog services
# 注销服务
consul services deregister -id=:service-id
// :service-id 为用户服务Id
# 查看节点成员
consul members
# 查看主节点
consul info
2、常用API
注册服务
payload.json
====
{
"ID": "spring-cloud-provider-8080",
"Name": "spring-cloud-provider",
"Tags": ["primary", "v1"],
"Address": "127.0.0.1",
"Port": 8080,
"EnableTagOverride": false,
"Check": {
"DeregisterCriticalServiceAfter": "1m",
"Interval": "10s",
"Timeout": "5s"
"Tcp":"127.0.0.1:8080"
},
"Weights": {
"Passing": 10,
"Warning": 1
}
}
curl \
--request PUT \
--data @payload.json \
http://127.0.0.1:8500/v1/agent/service/register?replace-existing-checks=true
查看所有注册服务列表
curl 127.0.0.1:8500/v1/catalog/services
查看服务实例状态(最佳接口)
查看健康检查通过的
curl http://127.0.0.1:8500/v1/health/checks/:serviceName?passing |python -m json.tool
查看全部
curl http://127.0.0.1:8500/v1/health/checks/:serviceName | python -m json.tool
查询服务实例列表
健康实例(会拉取到未设置check的service)
curl http://127.0.0.1:8500/v1/health/service/:serviceName?passing | python -m json.tool
查看全部
curl http://127.0.0.1:8500/v1/health/service/:serviceName | python -m json.tool
查询注册节点情况
curl http://127.0.0.1:8500/v1/catalog/service/:serviceName | python -m json.tool
查看Consul集群成员 (常用来测试网络联通性)
curl http://127.0.0.1:8500/v1/agent/members | python -m json.tool
查询Consul集群Leader(常用来判断集群状态)
curl http://127.0.0.1:8500/v1/status/leader