目錄
靜態(tài)配置基于文件發(fā)現(xiàn)配置基于DNS的A記錄基于DNS的SRV記錄自動(dòng)發(fā)現(xiàn)
簡(jiǎn)介
此時(shí)就需要配置自動(dòng)發(fā)現(xiàn),而只是很多種的自動(dòng)發(fā)現(xiàn)
支持:
........等等,有很多類型,本文中介紹基于靜態(tài)文件 和 DNS 的自動(dòng)發(fā)現(xiàn)
環(huán)境說明
增加一臺(tái) 10.0.20.12 主機(jī)及DNS使用的是bind9
如需bind9 文檔,點(diǎn)擊這里是bind9文檔
以下的所有演示都是案例,可根據(jù)自己的情況做變更。
靜態(tài)配置
簡(jiǎn)單講解下直接修改.yml主配置文件中的靜態(tài)配置。
修改配置如下:
[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' # 增加一個(gè)節(jié)點(diǎn),取名為 node
metrics_path: '/metrics' # 獲取數(shù)據(jù)的路徑 http://10.0.20.12:9100/metrics
static_configs:
- targets: ['10.0.20.12:9100'] # 靜態(tài)配置 node 節(jié)點(diǎn)的地址
重新加載配置文件
檢查配置文件,并重新加載
# 檢查配置文件
[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]#
再次訪問效果如下
基于文件發(fā)現(xiàn)配置
創(chuàng)建對(duì)應(yīng)的目錄
[root@es01 config]# pwd
/opt/prometheus-2.14/config
# 創(chuàng)建targets目錄,并且創(chuàng)建對(duì)應(yīng)的子目錄,用于存放相應(yīng)的配置文件自動(dòng)發(fā)現(xiàn)
[root@es01 config]# mkdir targets/{docker,nodes}
[root@es01 config]# ls targets/
docker nodes
在創(chuàng)建好的nodes目錄下創(chuàng)建 nodes.json文件,并寫入下面內(nèi)容
[root@es01 config]# cat targets/nodes/nodes.json
[{
"targets": [
"10.0.20.12:9100" # 指定node_export鏈接地址
],
"labels": {
"server": "node_export01" # 添加一個(gè)標(biāo)簽
}
}]
修改配置文件如下:
[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: # 這個(gè)配置表示 靜態(tài)發(fā)現(xiàn)
- targets: ['localhost:9090']
- job_name: 'node' # 增加一個(gè)節(jié)點(diǎn),取名為 node
metrics_path: "/metrics" # 獲取數(shù)據(jù)的路徑 http://10.0.20.12:9100/metrics

file_sd_configs: # 這個(gè)配置表示通過文件發(fā)現(xiàn)
- files:
- targets/nodes/*.json # 讀取目錄下的所有json結(jié)尾的文件
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]#
可以看到,獲取到了主機(jī)監(jiān)控,并且能看到增加標(biāo)簽
添加主機(jī)測(cè)試
在配置文件 /nodes/nodes.json 中添加一臺(tái)主機(jī)。
在前面的文檔中, 主機(jī)也部署了 ,這里添加測(cè)試
[{
"targets": [
"10.0.20.11:9100",
"10.0.20.12:9100"
],
"labels": {
"server": "node_export01"
}
}]
當(dāng)編輯文件 /nodes/nodes.json 后保存,再次訪問頁面時(shí),新增加的主機(jī)已經(jīng)自動(dòng)加入
簡(jiǎn)單總結(jié): 定義好對(duì)應(yīng)的主配置文件.yml后,后續(xù)要添加新的node節(jié)點(diǎn)監(jiān)控進(jìn)來,只需要增加node.json內(nèi)容即可,也不需要重載服務(wù),相對(duì)比較簡(jiǎn)單的基于文件的自動(dòng)發(fā)現(xiàn)。
基于DNS的A記錄
首先要在內(nèi)網(wǎng)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" # 獲取數(shù)據(jù)的路徑 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記錄自動(dòng)發(fā)現(xiàn)
需要在DNS中添加SRV解析記錄,下面演示在bind9中添加后測(cè)試
首先要在內(nèi)網(wǎng)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中添加的對(duì)應(yīng)的A記錄和SRV記錄
解析SRV記錄測(cè)試
[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.
能夠解析到上述內(nèi)容,則表示SRV記錄添加正常
注意,先添加es01命令行dns怎么添加a記錄,之后測(cè)試添加SRV后,能否動(dòng)態(tài)添加進(jìn)監(jiān)控
修改配置文件
[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" # 獲取數(shù)據(jù)的路徑 http://10.0.20.12:9100/metrics
dns_sd_configs: # 配置使用DNS解析

- names: ['_prometheus._tcp.k8s.com'] # 配置SRV對(duì)應(yīng)的解析地址
重新加載配置文件
檢查配置文件,并重新加載
# 檢查配置文件
[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]#
動(dòng)態(tài)添加解析測(cè)試
上面通過SRV已經(jīng)可以監(jiān)控了,這里再次測(cè)試下命令行dns怎么添加a記錄,添加一條SRV記錄后,是否能夠自動(dòng)添加
添加記錄并測(cè)試
首先要在內(nèi)網(wǎng)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中添加的對(duì)應(yīng)的A記錄和SRV記錄
解析SRV記錄測(cè)試
[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解析成功后訪問測(cè)試:
上圖中展示,已自動(dòng)添加成功。