当前位置: 首页 > web >正文

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可选,为每个目标附加标签信息

🚀 常见使用场景

  1. Kubernetes 外部服务发现

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

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

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

💡 实用技巧

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

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

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

http://www.xdnf.cn/news/4125.html

相关文章:

  • Android Retrofit框架分析(三):自动切换回主线程;bulid的过程;create方法+ServiceMethod源码了解
  • 【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
  • Windows系统修改Docker Desktop(WSL2)内存分配
  • Facebook隐私保护措施的优缺点解析
  • Java面试全栈解析:Spring Boot、Kafka与Redis实战揭秘
  • Jenkins+Newman实现接口自动化测试
  • 蓝桥杯-通电(最小生成树java)
  • Axure : 列表分页、 列表翻页
  • 第1.3讲、什么是 Attention?——从点菜说起 [特殊字符]️
  • FastJSON 使用 `Feature.OrderedField` 修复 `JSONObject` 序列化字段顺序问题
  • 用 GRPO 魔法点亮Text2SQL 的推理之路:让模型“思考”得更像人类
  • AI服务器的作用都有哪些?
  • 【工具使用-数据可视化工具】Apache Superset
  • Cursor 被封解决方案
  • 2、Kafka Replica机制与ISR、HW、LEO、AR、OSR详解
  • .NET 通过回调函数执行 Shellcode启动进程
  • 广州华锐视点邀您参与2025广交会VRAR展【5月10-12日】
  • 快速体验 .NET9 提供的 HybridCache 混合缓存
  • wrod生成pdf。[特殊字符]改背景
  • 基于Piecewise Jerk Speed Optimizer的速度规划算法(附ROS C++/Python仿真)
  • C++多态详解
  • ORCAD打印pdf
  • Docker手动重构Nginx镜像,融入Lua、Redis功能
  • 【C++】WSL常用语法
  • 先滤波再降采样 还是 先降采样再滤波
  • IL2CPP 技术深度解析
  • std::move()详解
  • n8n 使用 Merge 节点进行数据聚合
  • 系统思考:困惑源于内心假设
  • 【AI入门】Cherry入门1:Cherry Studio的安装及配置