简介

在实际的配置中,经常会遇到增加或者减少监控主机的状况,如果每次都需要修改prometheus.yml配置文件,实在是太麻烦了。

此时就需要配置自动发现,而prometheus只是很多种的自动发现

支持:

  • 基于公有云
  • 基于私有云
  • 基于文件,file的自动发现
  • 基于DNS 的自动发现,分为SRV解析自动发现和A记录解析的自动发现

........等等,有很多类型,本文中介绍基于静态文件 和 DNS 的自动发现

环境说明

增加一台 10.0.20.12 node_export 主机及DNS使用的是bind9

如需bind9 文档,点击这里是bind9文档

以下的所有演示都是案例,可根据自己的情况做变更。

静态配置

简单讲解下直接修改prometheus.yml主配置文件中的静态配置。

修改配置如下:

[root@es01 config]# cat prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: rule_files: scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] - job_name: 'node' # 增加一个节点,取名为 node
metrics_path: '/metrics' # 获取数据的路径 http://10.0.20.12:9100/metrics
static_configs:
- targets: ['10.0.20.12:9100'] # 静态配置 node 节点的地址

重新加载配置文件

检查配置文件,并重新加载

# 检查配置文件
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found # 重新加载配置文件
[root@es01 config]# curl -X POST httP://10.0.20.11:9090/-/reload
[root@es01 config]#

再次访问效果如下

基于文件发现配置

创建对应的目录

[root@es01 config]# pwd
/opt/prometheus-2.14/config
# 创建targets目录,并且创建对应的子目录,用于存放相应的配置文件自动发现
[root@es01 config]# mkdir targets/{docker,nodes}
[root@es01 config]# ls targets/
docker nodes

在创建好的nodes目录下创建 nodes.json文件,并写入下面内容

[root@es01 config]# cat targets/nodes/nodes.json
[{
"targets": [
"10.0.20.12:9100" # 指定node_export链接地址
],
"labels": {
"server": "node_export01" # 添加一个标签
}
}]

修改配置文件如下:

[root@es01 config]# cat prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: rule_files: scrape_configs:
- job_name: 'prometheus'
static_configs: # 这个配置表示 静态发现
- targets: ['localhost:9090'] - job_name: 'node' # 增加一个节点,取名为 node
metrics_path: "/metrics" # 获取数据的路径 http://10.0.20.12:9100/metrics
file_sd_configs: # 这个配置表示通过文件发现
- files:
- targets/nodes/*.json # 读取目录下的所有json结尾的文件
refresh_interval: 1m # 刷新频率,1 分钟

重新加载配置文件

检查配置文件,并重新加载

# 检查配置文件
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found # 重新加载配置文件
[root@es01 config]# curl -X POST httP://10.0.20.11:9090/-/reload
[root@es01 config]#

可以看到,获取到了主机监控,并且能看到增加标签

添加主机测试

在配置文件 targets/nodes/nodes.json 中添加一台主机。

在前面的文档中,prometheus 主机也部署了node_export ,这里添加测试

[{
"targets": [
"10.0.20.11:9100",
"10.0.20.12:9100"
],
"labels": {
"server": "node_export01"
}
}]

当编辑文件 targets/nodes/nodes.json 后保存,再次访问页面时,新增加的主机已经自动加入

简单总结: 定义好对应的主配置文件prometheus.yml后,后续要添加新的node节点监控进来,只需要增加node.json内容即可,也不需要重载prometheus服务,相对比较简单的基于文件的自动发现。

基于DNS的A记录

首先要在内网DNS上添加A记录解析:

[root@es01 config]# nslookup es01.k8s.com
Server: 192.168.1.122
Address: 192.168.1.122#53 Name: es01.k8s.com
Address: 10.0.20.11 # 解析后的地址 [root@es01 config]# nslookup es02.k8s.com
Server: 192.168.1.122
Address: 192.168.1.122#53 Name: es02.k8s.com
Address: 10.0.20.12 # 解析后的地址

这里添加了两条A记录

修改配置文件

[root@es01 config]# cat prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: rule_files: scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] - job_name: 'node-A' # 名称
metrics_path: "/metrics" # 获取数据的路径 http://10.0.20.12:9100/metrics
dns_sd_configs:
- names: ["es01.k8s.com", "es02.k8s.com"] # 这里填写A记录
type: A # 解析类型 A记录
port: 9100 # 端口

重新加载配置文件

检查配置文件,并重新加载

# 检查配置文件
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found # 重新加载配置文件
[root@es01 config]# curl -X POST httP://10.0.20.11:9090/-/reload
[root@es01 config]#

通过上图可以看出添加成功。

基于DNS的SRV记录自动发现

需要在DNS中添加SRV解析记录,下面演示在bind9中添加后测试

首先要在内网DNS上添加A记录解析:

[root@es01 config]# nslookup es01.k8s.com
Server: 192.168.1.122
Address: 192.168.1.122#53 Name: es01.k8s.com
Address: 10.0.20.11 # 解析后的地址

然后添加SRV记录:

[root@bind named]# cat k8s.com.zone
es01 IN A 10.0.20.11
_prometheus._tcp.k8s.com. IN SRV 10 10 9100 es01

上述是在bind中添加的对应的A记录和SRV记录

解析SRV记录测试

[root@es01 config]# dig @192.168.1.122 +noall +answer SRV _prometheus._tcp.k8s.com
_prometheus._tcp.k8s.com. 86400 IN SRV 10 10 9100 es01.k8s.com.

能够解析到上述内容,则表示SRV记录添加正常

注意,先添加es01,之后测试添加SRV后,prometheus能否动态添加进监控

修改配置文件

[root@es01 config]# cat prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: rule_files: scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] - job_name: 'node_srv' # 名称
metrics_path: "/metrics" # 获取数据的路径 http://10.0.20.12:9100/metrics
dns_sd_configs: # 配置使用DNS解析
- names: ['_prometheus._tcp.k8s.com'] # 配置SRV对应的解析地址

重新加载配置文件

检查配置文件,并重新加载

# 检查配置文件
[root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
Checking /opt/prometheus-2.14/config/prometheus.yml
SUCCESS: 0 rule files found # 重新加载配置文件
[root@es01 config]# curl -X POST httP://10.0.20.11:9090/-/reload
[root@es01 config]#

动态添加解析测试

上面通过SRV已经可以监控了,这里再次测试下,添加一条SRV记录后,prometheus是否能够自动添加

添加记录并测试

首先要在内网DNS上添加A记录解析:

[root@es01 config]# nslookup es01.k8s.com
Server: 192.168.1.122
Address: 192.168.1.122#53 Name: es01.k8s.com
Address: 10.0.20.11 # 解析后的地址 [root@es01 config]# nslookup es02.k8s.com
Server: 192.168.1.122
Address: 192.168.1.122#53 Name: es02.k8s.com
Address: 10.0.20.12 # 解析后的地址

然后添加SRV记录:

[root@bind named]# cat k8s.com.zone
es01 IN A 10.0.20.11
es02 IN A 10.0.20.12
_prometheus._tcp.k8s.com. IN SRV 10 10 9100 es01
_prometheus._tcp.k8s.com. IN SRV 10 10 9100 es02

上述是在bind中添加的对应的A记录和SRV记录

解析SRV记录测试

[root@es01 config]# dig @192.168.1.122 +noall +answer SRV _prometheus._tcp.k8s.com
_prometheus._tcp.k8s.com. 86400 IN SRV 10 10 9100 es01.k8s.com.
_prometheus._tcp.k8s.com. 86400 IN SRV 10 10 9100 es02.k8s.com.

添加SRV解析成功后访问测试:

上图中展示,已自动添加成功。

Prometheus 自动发现的更多相关文章

  1. Prometheus基于consul自动发现监控对象 https://www.iloxp.com/archive/11/

      Prometheus 监控目标为什么要自动发现 频繁对Prometheus配置文件进行修改,无疑给运维人员带来很大的负担,还有可能直接变成一个“配置小王子”,即使是配置小王子也会存在人为失误的情况 ...

  2. Prometheus监控神技--自动发现配置

    一.自动发现类型 在上一篇文中留了一个坑: 监控某个statefulset服务的时候,我在service文件中定义了个EP,然后把pod的ip写死在配置文件中,这样,当pod重启后,IP地址变化,就监 ...

  3. Prometheus Operator 自动发现和持久化

    Prometheus Operator 自动发现和持久化 之前在 Prometheus Operator 下面自定义一个监控选项,以及自定义报警规则的使用.那么我们还能够直接使用前面课程中的自动发现功 ...

  4. prometheus(5)之consul服务自动发现及pushgetway

    pushgetway(push上传metric数据) Pushgateway简介 Pushgateway是prometheus的一个组件,prometheus server默认是通过exporter主 ...

  5. zabbix自动发现功能实现批量web url监控

    需求: 现在有大量url需要监控,形式如http://www.baidu.com ,要求url状态不为200即报警. 需求详细分析: 大量的url,且url经常变化,现在监控用的是zabbix,如果手 ...

  6. zabbix_server的自动发现,实现批量添加主机,并链接到模板

    一.需求 zabbix 服务器可以手动加入zabbix-agent客户端,对于少量的机器,这没有什么.但到了线上,我们有大量的服务器需要监控时,如果再一个个的手动加的话,工作量势必会增加很多.这时,z ...

  7. zabbix自动发现监控url

    1.在监控客户机上 web_site_code_status.sh: #!/bin/bash UrlFile="/opt/scripts/WEB.txt" IFS=$'\n' we ...

  8. 【面试题】如何让C语言自动发现泄漏的内存

    1. 题目 改造malloc和free函数,使C语言能自动发现泄漏的内存,在程序退出时打印中遗漏的内存地址和大小. 2. 思路 用一个链表来记录已经分配的内存地址.在malloc时,把分配的内存地址和 ...

  9. Docker集群实验环境布署--swarm【3 注册服务监控与自动发现组件--consul】

    参考官网集群配置方式 https://hub.docker.com/r/progrium/consul/   集群中需要manager与node能通信consul的发现服务,不然,管理节点选举不了,无 ...

随机推荐

  1. HTML5+CSS:02用户注册表单

            新的学期已开始接近两个月了,还记得是在国庆节那几天申请的博客账号,可过了一个月都还没开始写博客,(>_<)有点小偷懒了,不过,学习还是不能落下的,今写一个有点实践运用的关于 ...

  2. 汇编语言——物理地址=段地址x16+偏移地址,检测点2.2

    一.为什么 物理地址=段地址x16+偏移地址? 刚开始学时,我都笨到不明白为什么是2的N次方,咱把物理地址就当数字,计算机中数字是由很多位0或1自由组合的, 而每一位上要么是0要么是1,只有这两种情况 ...

  3. Spring 动态代理 之 but was actually of type 'com.sun.proxy.$Proxy14 Exception

    今天在写Spring的引介代理的时候,报了一个错: Exception in thread "main" org.springframework.beans.factory.Bea ...

  4. Numpy 中的比较和 Fancy Indexing

    # 导包 import numpy as np Fancy Indexing 应用在一维数组 x = np.arange(16) x[3] x[3:9] # array([3, 4, 5, 6, 7, ...

  5. 20190820 Tue 集训总结&NOIP模拟 27

    低谷度过了? 但是skyh阿卡了,还是反衬出我的辣鸡. T1知道要sort,却忘了判重,正解不如暴力分高,555. T2成功化出正解柿子,然后化过头了,化出了无法DP的柿子. 果然不够强,大神们一眼就 ...

  6. HashMap 中的容量与扩容实现,细致入微,值的一品!

    前言 开心一刻 巴闭,你的脚怎么会有味道,我要闻闻看是不是好吃的,嗯~~爸比你的脚臭死啦!! …… 高手过招,招招致命 JDK1.8 中 HashMap 的底层实现,我相信大家都能说上来个 一二,底层 ...

  7. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths——dsu on tree

    题目描述 一棵根为1 的树,每条边上有一个字符(a-v共22种). 一条简单路径被称为Dokhtar-kosh当且仅当路径上的字符经过重新排序后可以变成一个回文串. 求每个子树中最长的Dokhtar- ...

  8. vue 选择图片(限定大小)上传到服务器

    FormData:  https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects  成果: htm ...

  9. 将训练集构建成ImageNet模型

    以下程序实现将训练集构建为ImageNet模型,训练集图片为56个民族 import java.io.File; import java.io.FileNotFoundException; impor ...

  10. pat 1069 The Black Hole of Numbers(20 分)

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...