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: 1
  selector:
    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: 9090
        volumeMounts:
        - 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.json
        refresh_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: production
    job: node_exporter

- targets: ['localhost:8080']
  labels:
    env: dev
    job: custom_app

⚙️ 配置详解

字段说明
files需要监控的目标文件路径,支持 glob 语法(如 /etc/prometheus/*.json
refresh_interval可选,多久检查一次文件变动,默认是 5m
targets目标列表,格式为 <host>:<port>
labels可选,为每个目标附加标签信息

🚀 常见使用场景

  1. Kubernetes 外部服务发现

    • 比如你用脚本从数据库或外部 API 拉取服务地址并生成一个 JSON 文件。
  2. 使用 Ansible / Terraform 动态生成目标文件

    • 基础设施即代码中经常这样用,保持 Prometheus 配置简洁。
  3. 手动维护少量目标

    • 不想频繁改 prometheus.yml,可以只改 JSON 文件。

💡 实用技巧

  • JSON 文件格式必须是合法 JSON,最常见的错误是逗号和引号。

  • file_sd_configrelabel_configs 搭配使用能实现复杂的目标筛选和重命名。

  • 可以结合 cron job、bash 脚本自动生成 targets.json

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值