写在前面
本人在dn42网络中有4个节点,本来搭建了LookingGlass来监测节点运行状态,但考虑到不够直观 其实是想折腾,所以打算搭建一个可以图形化监控每台节点Bird运行状态的仪表盘,在社区里面发现可以用Grafana搭配Prometheus作为数据源实现
拓扑图如下:
graph TD
subgraph DN42 Network
N1[Node 1] --> NE1(Node Exporter)
N1 --> BE1(Bird/FRR Exporter)
N2[Node 2] --> NE2(Node Exporter)
N2 --> BE2(Bird/FRR Exporter)
N3[Node 3] --> NE3(Node Exporter)
N3 --> BE3(Bird/FRR Exporter)
N4[Node 4] --> NE4(Node Exporter)
N4 --> BE4(Bird/FRR Exporter)
end
NE1 --> P(Prometheus Server)
NE2 --> P
NE3 --> P
NE4 --> P
BE1 --> P
BE2 --> P
BE3 --> P
BE4 --> P
P --> G(Grafana Server)
User[用户] --> G搭建
部署 Prometheus Server
Prometheus Server 将运行在独立的监控服务器上。
创建工作目录
mkdir -p /opt/prometheus/config /opt/prometheus/data创建 Prometheus 配置文件 (/opt/prometheus/config/prometheus.yml)
global:
scrape_interval: 15s # 默认抓取间隔
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090'] # 监控 Prometheus 自身
- job_name: 'node_exporter'
static_configs:
- targets: ['<node1_ip>:9100', '<node2_ip>:9100', '<node3_ip>:9100', '<node4_ip>:9100'] # 替换为您的 DN42 节点 IP
- job_name: 'bird_exporter'
static_configs:
- targets: ['<node1_ip>:9324', '<node2_ip>:9324', '<node3_ip>:9324', '<node4_ip>:9324'] # 替换为您的 DN42 节点 IP注意: 请将 <nodeX_ip> 替换为您的 DN42 节点的实际 IP 地址。
使用 Docker Compose 部署 Prometheus
创建 docker-compose.yml 文件:
services:
prometheus:
image: prom/prometheus
container_name: prometheus
network_mode: host
ports:
- "9090:9090"
volumes:
- /opt/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
- /opt/prometheus/data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.enable-lifecycle'
restart: unless-stopped将数据目录的权限赋予 Prometheus 用户:
chown -R 65534:65534 /opt/prometheus/data启动 Prometheus:
docker-compose up -d通过访问 http://<监控服务器IP>:9090 来验证 Prometheus 是否正常运行。
部署 Node Exporter (每个 DN42 节点上)
创建工作目录
mkdir -p /opt/node_exporter方式1:使用 Docker 部署 Node Exporter
docker run -d \
--name node_exporter \
--net="host" \
--pid="host" \
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host方式2:使用二进制可执行文件部署 Node Exporter
下载并解压 Node Exporter
访问 Prometheus 下载页面 获取最新版本的 Node Exporter。以下示例使用
1.7.0版本wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz tar xvfz node_exporter-1.7.0.linux-amd64.tar.gz sudo cp node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin sudo chown prometheus:prometheus /usr/local/bin/node_exporter rm -rf node_exporter-1.7.0.linux-amd64.tar.gz node_exporter-1.7.0.linux-amd64创建 Systemd 服务文件 (
/etc/systemd/system/node_exporter.service)[Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/usr/local/bin/node_exporter \ --web.listen-address=":9100" \ --collector.textfile.directory="/var/lib/node_exporter/textfile_collector" [Install] WantedBy=multi-user.target重新加载 Systemd 并启动 Node Exporter
sudo systemctl daemon-reload sudo systemctl start node_exporter sudo systemctl enable node_exporter
Bird Exporter
首先,确保 BIRD 配置允许 bird_exporter 访问其控制套接字。通常需要在 /etc/bird/bird.conf 或 /etc/bird2/bird.conf 中添加类似以下内容:
control socket "/var/run/bird/bird.ctl" mode 0777;然后,使用 Docker 部署 Bird Exporter (推荐用于内存充足的节点):
docker run -d \
--name bird_exporter \
--network host \
-v /var/run/bird/bird.ctl:/var/run/bird/bird.ctl \
czerwonk/bird_exporter:latest使用二进制方式部署 Bird Exporter
下载并解压 Bird Exporter
访问 Bird Exporter GitHub Releases 获取最新版本的 Bird Exporter。以下示例使用
1.1.0版本,请根据实际情况替换。wget https://github.com/czerwonk/bird_exporter/releases/download/v1.1.0/bird_exporter-1.1.0.linux-amd64.tar.gz tar xvfz bird_exporter-1.1.0.linux-amd64.tar.gz sudo cp bird_exporter-1.1.0.linux-amd64/bird_exporter /usr/local/bin sudo chown prometheus:prometheus /usr/local/bin/bird_exporter rm -rf bird_exporter-1.1.0.linux-amd64.tar.gz bird_exporter-1.1.0.linux-amd64创建 Systemd 服务文件 (
/etc/systemd/system/bird_exporter.service)[Unit] Description=Bird Exporter Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/usr/local/bin/bird_exporter \ --bird.socket="/var/run/bird/bird.ctl" \ --web.listen-address=":9324" [Install] WantedBy=multi-user.target重新加载 Systemd 并启动 Bird Exporter
sudo systemctl daemon-reload sudo systemctl start bird_exporter sudo systemctl enable bird_exporter部署 Grafana Server
Grafana Server 建议与 Prometheus Server 部署在同一台监控服务器上。
创建工作目录
mkdir -p /opt/grafana/data4.4.2 使用 Docker Compose 部署 Grafana
更新之前创建的 docker-compose.yml 文件,添加 Grafana 服务:
services:
prometheus:
image: prom/prometheus
container_name: prometheus
ports:
- "9090:9090"
volumes:
- /opt/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
- /opt/prometheus/data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.enable-lifecycle'
restart: unless-stopped
grafana:
image: grafana/grafana
container_name: grafana
ports:
- "3000:3000"
volumes:
- /opt/grafana/data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=your_strong_password # 请替换为强密码
depends_on:
- prometheus
restart: unless-stopped重新启动 Docker Compose 服务以启动 Grafana:
docker-compose up -d配置 Grafana 数据源
- 登录 Grafana。
- 在左侧导航栏中,点击齿轮图标 (Configuration) -> Data Sources。
- 点击
Add data source。 - 选择
Prometheus。 - 在
HTTP部分的URL字段中输入http://prometheus:9090(如果 Prometheus 和 Grafana 在同一个 Docker Compose 网络中) 或http://localhost:9090(如果 Prometheus 运行在宿主机上,且 Grafana 可以直接访问)。 - 点击
Save & Test。如果一切正常,您将看到Data source is working的消息。
导入 Grafana 仪表盘
Grafana 社区提供了许多现成的仪表盘,可以大大简化监控配置。以下是一些推荐的仪表盘 ID:
- Node Exporter Full: ID
1860(或搜索Node Exporter Full),用于监控主机操作系统指标。 - BIRD RS: ID
5259(或搜索BIRD RS),用于监控 BIRD 路由协议状态。 - FRR Exporter - BGP: ID
22943(或搜索FRR Exporter - BGP),用于监控 FRR BGP 状态。
导入步骤
- 在 Grafana 左侧导航栏中,点击
+图标 (Create) ->Import。 - 在
Import via grafana.com字段中输入上述仪表盘 ID,然后点击Load。 - 选择您的 Prometheus 数据源。
- 点击
Import。
重复此过程,导入所有您需要的仪表盘。
告警配置 (可选)
Grafana 允许您基于 Prometheus 收集的指标配置告警规则。当指标达到预设阈值时,Grafana 可以通过邮件、Slack、Webhook 等方式发送通知。
配置告警通道
- 在 Grafana 左侧导航栏中,点击齿轮图标 (Configuration) ->
Alerting->Notification channels。 - 点击
New channel。 - 选择偏好的通知类型 (例如
Email,Slack) 并填写相关配置信息。 - 点击
Save。
创建告警规则
- 打开想要添加告警的仪表盘。
- 选择一个面板,点击面板标题,然后选择
Edit。 - 在面板编辑视图中,切换到
Alert选项卡。 - 点击
Create Alert。 - 定义告警规则的条件、评估周期和通知通道。
- 点击
Save。
评论 (0)