网站首页 > 技术教程 正文
1查看NODE-IP
```kubectl get node -o wide
NAME 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
--------
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 | 可选,为每个目标附加标签信息 |
常见使用场景
- Kubernetes 外部服务发现:
- 比如你用脚本从数据库或外部 API 拉取服务地址并生成一个 JSON 文件。
- 使用 Ansible / Terraform 动态生成目标文件:
- 基础设施即代码中经常这样用,保持 Prometheus 配置简洁。
- 手动维护少量目标:
- 不想频繁改 prometheus.yml,可以只改 JSON 文件。
实用技巧
- JSON 文件格式必须是合法 JSON,最常见的错误是逗号和引号。
- file_sd_config 和 relabel_configs 搭配使用能实现复杂的目标筛选和重命名。
- 可以结合 cron job、bash 脚本自动生成 targets.json。
猜你喜欢
- 2025-05-28 k8s实战
- 2025-05-28 k8s中部署基于nfs的StorageClass
- 2025-05-28 Kubernetes详解——NFS对PVC和PV使用实战
- 2025-05-28 Kubernetes日志采集ELK|部署ElasticSearch存储
- 2025-05-28 Milvus部署指南:从单机到分布式,手把手教你玩转向量数据库
- 2025-05-28 kubernetes环境手动部署 Prometheus 监控系统安装文档
- 2025-05-28 k8s集群资源监控
- 2025-05-28 一文了解k8s核心高频常用的内置对象
- 2025-05-28 动手动脑学Kubernetes系列之StatefulSet
- 2025-05-28 「云原生」k8s中volumeMounts.subPath的巧妙用法
你 发表评论:
欢迎- 05-30壁纸|Q版灌篮高手
- 05-30Q版插画|今天的小姑娘有点小情绪,不爱笑,还是笑起来招人喜欢
- 05-30头像背景图|十二生肖Q版水墨(上)
- 05-308张Q版动漫人物-可爱的男生女生马克笔设计绘画,色彩漂亮鲜艳
- 05-30头像‖q卡通呆萌版
- 05-30Q版古风美人,十二花神系列,每一张都很可爱
- 05-30次元|动漫|Q版|萝莉|头像
- 05-30Q版古风人物|红楼梦系列 天上掉下来个林妹妹(附线稿)
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)