服务发现的常见类别说明

- 静态配置: static_configs
	每次修改配置后,都需要重新加载配置或者重启服务。

- 动态配置
	每次修改后,无需重新加载配置或者重启服务。
	
	常见的动态配置:
		<file_sd_config>
			基于文件的服务发现。
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#file_sd_config
			
		<consul_sd_config>
			基于consul的服务发现。
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#consul_sd_config
			
		<kubernetes_sd_config>
			基于K8S实现的服务发现
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#kubernetes_sd_config
			
		<dns_sd_config>:
			基于DNS实现服务发现。
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#dns_sd_config
		<docker_sd_config>:
			基于docker engine的服务发现
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#docker_sd_config
		<http_sd_config>:
			基于http的服务发现。
			https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#http_sd_config

基于文件的服务发现

1.修改Prometheus的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@elk01:5 ~]# vim /softwares/prometheus-2.53.2.linux-amd64/prometheus.yml 
...
- job_name: "linux-file-sd-config"
file_sd_configs:
- files:
- /softwares/prometheus-2.53.2.linux-amd64/test.yaml
- /softwares/prometheus-2.53.2.linux-amd64/test.json

#检查Prometheus语法是否有误
[root@elk01:0 ~]# /softwares/prometheus-2.53.2.linux-amd64/promtool check config /softwares/prometheus-2.53.2.linux-amd64/prometheus.yml

#Prometheus热加载
[root@elk01:0 ~]# curl -X POST http://10.0.0.211:9090/-/reload

此时访问Prometheus的webui,发现没有显示新的服务
http://10.0.0.211:9090/targets

2.编辑服务发现文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
`yaml格式:`
[root@elk01:5 ~]# vim /softwares/prometheus-2.53.2.linux-amd64/test.yaml
- targets:
- 10.0.0.213:9100
- 10.0.0.211:9100
- 10.0.0.212:3000

`json格式`
[root@elk01:5 ~]# vim /softwares/prometheus-2.53.2.linux-amd64/test.json
[
{
"targets": [ "10.0.0.211:9100","10.0.0.212:9100" ]
}
]

3.测试验证

1
2
文件保存后直接访问webUI,即可发现就可以显示了,不需要热加载Prometheus
http://10.0.0.211:9090/targets

image-20241127135024922

基于文件发现和基于consul对比

基于文件的服务发现是在Prometheus本地基于某个文件读取要监控的节点;

基于consul服务发现是需要单独部署一套consul集群,Prometheus-server去对应的consul集群获取需要监控的节点。

部署基于console的服务发现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1 下载consul
wget https://releases.hashicorp.com/consul/1.19.2/consul_1.19.2_linux_amd64.zip

2 解压consul
unzip consul_1.19.2_linux_amd64.zip -d /usr/local/bin/

3 运行consul 集群
服务端211:
consul agent -server -bootstrap -bind=10.0.0.211 -data-dir=/data/consul -client=10.0.0.211 -ui

客户端212:
consul agent -bind=10.0.0.212 -data-dir=/data/consul -client=10.0.0.212 -ui -retry-join=10.0.0.211

客户端213:
consul agent -server -bind=10.0.0.213 -data-dir=/data/consul -client=10.0.0.213 -ui -retry-join=10.0.0.211

4 查看各节点的监听端口
ss -ntl | grep 8500

5 访问console服务的WebUI
http://10.0.0.213:8500/ui/dc1/nodes

image-20241127144212517

基于consul的服务发现应用

1 修改prometheus的配置文件并重新加载配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@elk01:1 ~]# vim /softwares/prometheus-2.53.2.linux-amd64/prometheus.yml 
...
- job_name: "Linux-consul-seriver-discovery"
# 配置基于consul的服务发现
consul_sd_configs:
# 指定consul的服务器地址,若不指定,则默认值为"localhost:8500".
- server: 10.0.0.213:8500
- server: 10.0.0.212:8500
- server: 10.0.0.211:8500
relabel_configs:
# 匹配consul的源标签字段,表示服务名称
- source_labels: [__meta_consul_service]
# 指定源标签的正则表达式,若不定义,默认值为"(.*)"
regex: consul
# 执行动作为删除,默认值为"replace",有效值有多种
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_action
action: drop

#检查Prometheus语法是否有误
[root@elk01:0 ~]# /softwares/prometheus-2.53.2.linux-amd64/promtool check config /softwares/prometheus-2.53.2.linux-amd64/prometheus.yml

#Prometheus热加载
[root@elk01:0 ~]# curl -X POST http://10.0.0.211:9090/-/reload

2.被监控节点注册到console集群

1
2
3
4
5
6
7
8
9
10
11
12
[root@elk01 ~]# curl -X PUT -d '{"id":"elk211","name":"elk01","address":"10.0.0.211","port":9100,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.211:9100","interval":"5m"}]}' http://10.0.0.213:8500/v1/agent/service/register

[root@elk02 ~]# curl -X PUT -d '{"id":"elk212","name":"elk212","address":"10.0.0.212","port":9100,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.212:9100","interval":"5m"}]}' http://10.0.0.213:8500/v1/agent/service/register

[root@elk03 ~]# curl -X PUT -d '{"id":"elk213","name":"elk213","address":"10.0.0.213","port":9100,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.213:9100","interval":"5m"}]}' http://10.0.0.213:8500/v1/agent/service/register


`详解`
"id":"elk211","name":"elk01","address":"10.0.0.211","port":9100 监控211节点,地址和端口
checks": [{"http":"http://10.0.0.211:9100","interval":"5m"}]}' 检查节点,和间隔时间
http://10.0.0.213:8500/v1/agent/service/register 注册到console集群(写211,212,213都可以)

3.检查consul的WebUI

1
http://10.0.0.212:8500/ui/dc1/services

image-20241127153448968

4 检查Prometheus的WebUI

1
http://10.0.0.211:9090/targets?search=

image-20241127153520806

5.注销节点

1
2
3
curl -X PUT http://10.0.0.213:8500/v1/agent/service/deregister/elk213

elk213 `这里写的是id,而不是name`

注意,也可以使用POSTMAN直接进行如下操作,需要使用PUT方法。
PUT http://10.0.0.213:8500/v1/agent/service/deregister/elk213

Prometheus监控console集群

Prometheus监控consul集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1.下载consul exporter
[root@elk02:5 ~]# wget https://github.com/prometheus/consul_exporter/releases/download/v0.12.1/consul_exporter-0.12.1.linux-amd64.tar.gz

2.解压软件包
[root@elk02:5 ~]# tar xf consul_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/bin/ consul_exporter-0.12.1.linux-amd64/consul_exporter --strip-components=1

3.启动console exporter
[root@elk02:5 ~]# consul_exporter --consul.server="http://10.0.0.213:8500" --web.telemetry-path="/metrics" --web.listen-address=:9107

4.访问consul exporter的WebUI
http://10.0.0.212:9107/metrics

5.向consul注册(或者修改Prometheus配置文件,静态发现需要热加载Prometheus)
[root@elk02:5 ~]# curl -X PUT -d '{"id":"elk212-consul","name":"consul-cluster","address":"10.0.0.212","port":9107,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.212:9107","interval":"5m"}]}' http://10.0.0.213:8500/v1/agent/service/register

6.访问consul的WebUI
http://10.0.0.212:8500/ui/dc1/services/consul-cluster/instances

7.访问prometheus的WebUI
http://10.0.0.211:9090/targets

8.grafana导入模板ID
12049