一、概述

DNS 服务发现是 Prometheus 中一种动态的服务发现机制,通过 DNS 记录自动发现需要监控的目标,无需手动维护配置文件。

优势

  • 动态更新:目标变化时自动同步

  • 集中管理:通过 DNS 服务器统一管理服务地址

  • 负载均衡:支持 SRV 记录的权重和优先级

二、环境准备

2.1 环境信息

text

DNS 服务器:10.0.0.104 (Ubuntu 24.04)
Prometheus:10.0.0.104
监控节点:node1(10.0.0.100), node2(10.0.0.101), node3(10.0.0.102)
域名:wang.org

2.2 DNS 服务器配置

安装 BIND9

bash

# Ubuntu 24.04 安装 BIND9
apt update
apt -y install bind9 bind9utils bind9-doc

# 注意:不需要单独安装 named 包(已包含在 bind9 中)
配置区域文件

bash

# 创建区域文件 /etc/bind/wang.org.zone
vim /etc/bind/wang.org.zone

区域文件内容:

bash

$TTL    604800
@       IN      SOA     master1.wang.org. admin.wang.org. (
                  2026050901      ; Serial (每次修改必须增加)
                  604800          ; Refresh
                  86400           ; Retry
                  2419200         ; Expire
                  604800 )        ; Negative Cache TTL
;
@       IN      NS      master1.wang.org.
;
; A 记录
master1 IN      A       10.0.0.104
node1   IN      A       10.0.0.100
node2   IN      A       10.0.0.101
node3   IN      A       10.0.0.102
flask   IN      A       10.0.0.104

; SRV 记录(用于服务发现)
_prometheus._tcp.wang.org. 3600 IN SRV 10 10 9100 node1.wang.org.
_prometheus._tcp.wang.org. 3600 IN SRV 10 10 9100 node2.wang.org.
_prometheus._tcp.wang.org. 3600 IN SRV 10 10 9100 node3.wang.org.
配置 BIND9

bash

# 编辑 named.conf.local
vim /etc/bind/named.conf.local

# 添加以下内容
zone "wang.org" {
    type master;
    file "/etc/bind/wang.org.zone";
};
启动并验证 DNS

bash

# 检查配置语法
named-checkzone wang.org /etc/bind/wang.org.zone
named-checkconf

# 重启 BIND9
systemctl restart named
systemctl enable named

# 测试 DNS 查询
dig @127.0.0.1 _prometheus._tcp.wang.org SRV

三、常见问题及解决方法

问题 1:BIND9 启动失败

错误现象

text

systemctl status named
Active: failed

原因分析

  • 配置文件语法错误

  • 区域文件格式不正确

  • 权限问题

解决方法

bash

# 1. 检查配置文件语法
named-checkconf /etc/bind/named.conf
named-checkzone wang.org /etc/bind/wang.org.zone

# 2. 查看详细错误日志
journalctl -u named -n 50 --no-pager

# 3. 确保区域文件 serial 已更新
# 每次修改必须增加 serial 号

问题 2:DNS 查询不到 SRV 记录

错误现象

bash

dig _prometheus._tcp.wang.org SRV
;; ANSWER SECTION: (空)

原因分析

  • SRV 记录格式错误

  • 区域文件未正确加载

  • Serial 未更新

解决方法

bash

# 1. 确认 SRV 记录格式正确(注意最后的点)
_prometheus._tcp.wang.org. 3600 IN SRV 10 10 9100 node1.wang.org.

# 2. 增加 serial 并重新加载
vim /etc/bind/wang.org.zone
# 修改 serial: 2026050801 -> 2026050901

# 3. 重新加载配置
rndc reload

# 4. 验证加载状态
rndc dumpdb -all
grep _prometheus /var/cache/bind/named_dump.db

问题 3:系统使用错误的 DNS 服务器

错误现象

bash

dig _prometheus._tcp.wang.org SRV
;; SERVER: 127.0.0.53#53  # 使用了本地解析器

原因分析

  • systemd-resolved 拦截 DNS 查询

  • /etc/resolv.conf 配置错误

解决方法

bash

# 方法1:修改 netplan 配置
vim /etc/netplan/01-netcfg.yaml

添加 DNS 配置:

yaml

network:
  version: 2
  ethernets:
    ens33:
      nameservers:
        search: [wang.org]
        addresses: [10.0.0.104]  # 指定 DNS 服务器

bash

# 应用配置
netplan apply

# 方法2:临时测试(指定 DNS 服务器)
dig @10.0.0.104 _prometheus._tcp.wang.org SRV

# 方法3:禁用 systemd-resolved(可选)
systemctl stop systemd-resolved
systemctl disable systemd-resolved
echo "nameserver 10.0.0.104" > /etc/resolv.conf

问题 4:Prometheus 无法发现目标

错误现象

  • Prometheus targets 页面没有显示目标

  • 日志显示 DNS 解析失败

解决方法

bash

# 1. 测试 Prometheus 所在机器的 DNS 解析
dig @10.0.0.104 _prometheus._tcp.wang.org SRV

# 2. 检查 Prometheus 配置
promtool check config /etc/prometheus/prometheus.yml

# 3. 查看 Prometheus 日志
journalctl -u prometheus -f | grep -i dns

# 4. 手动触发重新加载
curl -X POST http://localhost:9090/-/reload

# 5. 查看 API 返回的 targets
curl -s http://localhost:9090/api/v1/targets | jq '.'

四、Prometheus 配置 DNS 发现

4.1 支持的 DNS 记录类型

类型 说明 使用场景
SRV 服务记录 服务发现,支持端口、权重、优先级
A 地址记录 直接解析到 IP 地址
AAAA IPv6 地址 IPv6 环境
MX 邮件记录 邮件服务发现
NS 域名服务器 DNS 服务器发现

4.2 基础配置示例

yaml

# /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  # 使用 SRV 记录发现 Node Exporter
  - job_name: 'node_exporter'
    dns_sd_configs:
      - names: ['_prometheus._tcp.wang.org']
        type: SRV
        refresh_interval: 30s
        port: 9100  # 可选,SRV 记录中已包含端口

  # 使用 A 记录发现 Flask 应用
  - job_name: 'flask_app'
    dns_sd_configs:
      - names: ['flask.wang.org']
        type: A
        port: 8000
        refresh_interval: 30s

4.3 高级配置:添加自定义标签

yaml

  - job_name: 'node_exporter_with_labels'
    dns_sd_configs:
      - names: ['_prometheus._tcp.wang.org']
        type: SRV
        refresh_interval: 30s
    
    # 重新标记配置
    relabel_configs:
      # 提取服务名称
      - source_labels: ['__meta_dns_srv_name']
        regex: '_(.+?)\._(tcp|udp)\.'
        target_label: 'service'
        replacement: '$1'
      
      # 提取主机名
      - source_labels: ['__meta_dns_name']
        regex: '(.+?)\.wang\.org'
        target_label: 'host'
        replacement: '$1'
      
      # 添加环境标签
      - target_label: 'environment'
        replacement: 'production'

4.4 多种发现方式组合

yaml

  - job_name: 'mixed_discovery'
    # 同时使用多种发现方式
    dns_sd_configs:
      - names: ['_prometheus._tcp.wang.org']
        type: SRV
    
    file_sd_configs:
      - files: ['targets/static-nodes.yml']
        refresh_interval: 5m
    
    static_configs:
      - targets: ['localhost:9090']
        labels:
          service: 'prometheus'
    
    # 统一标签处理
    relabel_configs:
      - action: labelmap
        regex: '__meta_dns_(.+)'

4.5 Prometheus DNS 相关的元标签

标签 说明 示例
__meta_dns_name 记录的名字 node1.wang.org
__meta_dns_srv_name SRV 记录名 _prometheus._tcp.wang.org
__meta_dns_srv_priority SRV 优先级 10
__meta_dns_srv_weight SRV 权重 10
__meta_dns_srv_port SRV 端口 9100
__meta_dns_srv_target SRV 目标 node1.wang.org
__address__ 目标地址 node1.wang.org:9100

五、验证和测试

5.1 DNS 层面验证

bash

# 1. 测试 SRV 记录
dig @10.0.0.104 _prometheus._tcp.wang.org SRV +short
# 预期输出:
# 10 10 9100 node1.wang.org.
# 10 10 9100 node2.wang.org.
# 10 10 9100 node3.wang.org.

# 2. 测试 A 记录
dig @10.0.0.104 node1.wang.org A +short
# 预期输出:10.0.0.100

# 3. 测试反向解析
dig @10.0.0.104 -x 10.0.0.100 +short
# 预期输出:node1.wang.org.

5.2 Prometheus 层面验证

bash

# 1. 检查配置文件
promtool check config /etc/prometheus/prometheus.yml

# 2. 查看服务发现状态
curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | 
  select(.job=="node_exporter") | 
  {scrapeUrl, labels: .labels.discovered_labels}'

# 3. 查看 DNS 发现的具体信息
curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | 
  select(.job=="node_exporter") | 
  .discoveredLabels'

5.3 Web UI 验证

访问 Prometheus Web UI:

  • Targets 页面http://10.0.0.104:9090/targets

  • 服务发现页面http://10.0.0.104:9090/service-discovery

六、性能优化建议

6.1 DNS 缓存设置

yaml

# prometheus.yml
scrape_configs:
  - job_name: 'node_exporter'
    dns_sd_configs:
      - names: ['_prometheus._tcp.wang.org']
        type: SRV
        refresh_interval: 60s  # 不要太频繁,避免 DNS 服务器压力

6.2 DNS 服务器优化

bash

# BIND9 配置优化
vim /etc/bind/named.conf.options

options {
    # 增加缓存大小
    cache-file "cache.db";
    max-cache-size 512M;
    
    # 启用递归限制
    allow-recursion { 10.0.0.0/24; };
    
    # 减少查询超时
    fetch-glue no;
};

七、监控 DNS 发现状态

7.1 创建告警规则

yaml

# /etc/prometheus/rules/dns_discovery.yml
groups:
  - name: dns_discovery
    interval: 30s
    rules:
      - alert: DNSSDScrapeFailed
        expr: up{job=~".*dns.*"} == 0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "DNS discovery target down"
          description: "Target {{ $labels.instance }} from DNS discovery is down"

      - alert: DNSSDNoTargets
        expr: count(up{job=~".*dns.*"}) == 0
        for: 10m
        labels:
          severity: critical
        annotations:
          summary: "No targets found via DNS discovery"

7.2 查看 Prometheus 自身指标

bash

# Prometheus 关于服务发现的指标
curl -s http://localhost:9090/metrics | grep -E "sd_(dns|file)"

八、总结

8.1 DNS 发现 vs 文件发现

特性 DNS 发现 文件发现
配置复杂度
动态性 高(实时) 低(需修改文件)
依赖服务 DNS 服务器
适用场景 动态环境 静态环境
负载均衡 支持 SRV 权重 需手动配置

8.2 最佳实践

  1. 使用 SRV 记录:比 A 记录更适合服务发现

  2. 合理设置 TTL:平衡动态性和 DNS 缓存负载

  3. 配置重标记:为不同服务添加有意义的标签

  4. 监控发现状态:设置告警规则,及时发现问题

  5. 生产环境建议:配合 Consul、Kubernetes 等更完善的服务发现机制

8.3 排错检查清单

  • DNS 服务器是否正常运行

  • SRV 记录格式是否正确

  • Serial 是否已更新

  • 系统 DNS 配置是否正确

  • Prometheus 配置语法是否正确

  • 防火墙是否放行 DNS 端口(53)

  • Prometheus 日志是否有报错

  • Targets 页面是否显示发现的目标

九、参考资料


更新日期:2026-05-09
环境版本:Ubuntu 24.04, Prometheus 2.x, BIND9 9.18

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐