Prometheus实战教程:k8s平台-使用文件服务发现案例
date: 2025-03-06
tags:- prometheus
---
1查看NODE-IP```kubectl get node -o wideNAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIMEk8s-node01 Ready <none> 336d v1.28.2 10.255.209.37 <none> CentOS Linux 7 (Core) 6.6.9-1.el7.elrepo.x86_64 docker://24.0.9k8s-node02 Ready <none> 336d v1.28.2 10.255.209.38 <none> CentOS Linux 7 (Core) 6.6.9-1.el7.elrepo.x86_64 docker://24.0.9k8s-node03 Ready <none> 336d v1.28.2 10.255.209.40 <none> CentOS Linux 7 (Core) 6.6.9-1.el7.elrepo.x86_64 docker://24.0.9```1、编辑服务发现文件,支持yaml 和json格式```cat /root/file-sd.yaml
- targets:- '10.255.209.37:9100'- '10.255.209.38:9100'- '10.255.209.40:9100'labels:environment: node_export```2、配置服务发现,修改promentheus-configmap文件``` - job_name: "file_sd"file_sd_configs:- files:- /apps/prometheus/file-sd.yaml refresh_interval: 1m```3、挂载服务发现文件到pod中```cat prometheus-deployment0227.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheus namespace: monitoring labels:app: prometheus
spec:replicas: 1selector:matchLabels:app: prometheus template:metadata:labels:app: prometheus spec:serviceAccountName: prometheus containers:- name: prometheus image: harbor.fq.com/prometheus/prometheus:v3.1.0 args:- --config.file=/etc/prometheus/prometheus.yml - --storage.tsdb.path=/prometheus - --web.console.templates=/etc/prometheus/consoles - --web.console.libraries=/etc/prometheus/console_libraries ports:- containerPort: 9090volumeMounts:- name: prometheus-config mountPath: /etc/prometheus - name: prometheus-storage mountPath: /prometheus - name: file-sd # 添加挂载路径mountPath: /apps/prometheus/file-sd.yaml volumes:- name: prometheus-config configMap:name: prometheus-config - name: prometheus-storage emptyDir: {}- name: file-sd # 挂载文件服务发现配置文件到pod中,使用hostPath,注意文件路径,需要放置到pod所在node上hostPath:path: /root/file-sd.yaml type: File```4、web界面,查看示例
![[IMG-3、k8s平台:文件-服务发现示例-20250317144644522.png]]--------
file_sd_config
是 Prometheus 中用于 服务发现(Service Discovery) 的一种配置方式,主要用于通过读取文件中定义的目标地址(targets)来动态更新 Prometheus 的抓取目标(scrape targets)。它非常适合与外部系统集成,比如你用脚本或配置管理工具生成目标列表。
🧩 工作原理
Prometheus 会周期性地读取指定的 JSON 或 YAML 文件,并根据这些文件中的配置更新抓取目标列表。每当文件内容发生变更,Prometheus 都会自动重新加载(不需要重启服务)。
📁 基本语法
Prometheus 配置文件(prometheus.yml
)中的一个 file_sd_configs
示例:
scrape_configs:- job_name: 'example-file-sd'file_sd_configs:- files:- targets.jsonrefresh_interval: 30s
📂 文件格式
支持 .json
和 .yaml
文件,文件内容格式如下:
✅ JSON 格式
[{"targets": ["192.168.1.10:9100", "192.168.1.11:9100"],"labels": {"env": "production","job": "node_exporter"}},{"targets": ["localhost:8080"],"labels": {"env": "dev","job": "custom_app"}}
]
✅ YAML 格式(效果相同)
- targets: ['192.168.1.10:9100', '192.168.1.11:9100']labels:env: productionjob: node_exporter- targets: ['localhost:8080']labels:env: devjob: custom_app
⚙️ 配置详解
字段 | 说明 |
---|---|
files | 需要监控的目标文件路径,支持 glob 语法(如 /etc/prometheus/*.json ) |
refresh_interval | 可选,多久检查一次文件变动,默认是 5m |
targets | 目标列表,格式为 <host>:<port> |
labels | 可选,为每个目标附加标签信息 |
🚀 常见使用场景
-
Kubernetes 外部服务发现:
- 比如你用脚本从数据库或外部 API 拉取服务地址并生成一个 JSON 文件。
-
使用 Ansible / Terraform 动态生成目标文件:
- 基础设施即代码中经常这样用,保持 Prometheus 配置简洁。
-
手动维护少量目标:
- 不想频繁改 prometheus.yml,可以只改 JSON 文件。
💡 实用技巧
-
JSON 文件格式必须是合法 JSON,最常见的错误是逗号和引号。
-
file_sd_config
和relabel_configs
搭配使用能实现复杂的目标筛选和重命名。 -
可以结合 cron job、bash 脚本自动生成
targets.json
。