1、scrape_configs 参数介绍

# 默认的全局配置
global:
scrape_interval: 15s # 采集间隔15s,默认为1min一次
evaluation_interval: 15s # 计算规则的间隔15s默认为1min一次
scrape_timeout: 10s # 采集超时时间,默认为10s
external_labels: # 当和其他外部系统交互时的标签,如远程存储、联邦集群时
prometheus: monitoring/k8s # 如:prometheus-operator的配置
prometheus_replica: prometheus-k8s-1 # Alertmanager的配置
alerting:
alertmanagers:
- static_configs:
- targets:
- 127.0.0.1:9093 # alertmanager的服务地址,如127.0.0.1:9093
alert_relabel_configs: # 在抓取之前对任何目标及其标签进行修改。
- separator: ;
regex: prometheus_replica
replacement: $1
action: labeldrop # 一旦加载了报警规则文件,将按照evaluation_interval即15s一次进行计算,rule文件可以有多个
rule_files:
# - "first_rules.yml"
# - "second_rules.yml" # scrape_configs为采集配置,包含至少一个job scrape_configs:
# Prometheus的自身监控 将在采集到的时间序列数据上打上标签job=xx
- job_name: 'prometheus'
# 采集指标的默认路径为:/metrics,如 localhost:9090/metric
# 协议默认为http
static_configs:
- targets: ['localhost:9090'] # 远程读,可选配置,如将监控数据远程读写到influxdb的地址,默认为本地读写
remote_write:
127.0.0.1:8090 # 远程写
remote_read:
127.0.0.1:8090

2、scrape_configs配置案例

prometheus的配置中,最常用的就是scrape_configs配置,比如添加新的监控项,修改原有监控项的地址频率等。
最简单配置为:
scrape_configs:
- job_name: prometheus
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- localhost:9090 完整配置为(附prometheus-operator的推荐配置):
# job 将以标签形式出现在指标数据中,如node-exporter采集的数据,job=node-exporter
job_name: node-exporter # 采集频率:30s
scrape_interval: 30s # 采集超时:10s
scrape_timeout: 10s # 采集对象的path路径
metrics_path: /metrics # 采集协议:http或者https
scheme: https # 可选的采集url的参数
params:
name: demo # 当自定义label和采集到的自带label冲突时的处理方式,默认冲突时会重名为exported_xx
honor_labels: false # 当采集对象需要鉴权才能获取时,配置账号密码等信息
basic_auth:
username: admin
password: admin
password_file: /etc/pwd # bearer_token或者文件位置(OAuth 2.0鉴权)
bearer_token: kferkhjktdgjwkgkrwg
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token # https的配置,如跳过认证,或配置证书文件
tls_config:
# insecure_skip_verify: true
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
server_name: kubernetes
insecure_skip_verify: false # 代理地址
proxy_url: 127.9.9.0:9999 # Azure的服务发现配置
azure_sd_configs: # Consul的服务发现配置
consul_sd_configs: # DNS的服务发现配置
dns_sd_configs: # EC2的服务发现配置
ec2_sd_configs: # OpenStack的服务发现配置
openstack_sd_configs: # file的服务发现配置
file_sd_configs: # GCE的服务发现配置
gce_sd_configs: # Marathon的服务发现配置
marathon_sd_configs: # AirBnB的服务发现配置
nerve_sd_configs: # Zookeeper的服务发现配置
serverset_sd_configs: # Triton的服务发现配置
triton_sd_configs: # Kubernetes的服务发现配置
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- monitoring # 对采集对象进行一些静态配置,如打特定的标签
static_configs:
- targets: ['localhost:9090', 'localhost:9191']
labels:
my: label
your: label # 在Prometheus采集数据之前,通过Target实例的Metadata信息,动态重新写入Label的值。
如将原始的__meta_kubernetes_namespace直接写成namespace,简洁明了 relabel_configs:
- source_labels: [__meta_kubernetes_namespace]
separator: ;
regex: (.*)
target_label: namespace
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_service_name]
separator: ;
regex: (.*)
target_label: service
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_pod_name]
separator: ;
regex: (.*)
target_label: pod
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_service_name]
separator: ;
regex: (.*)
target_label: job
replacement: ${1}
action: replace
- separator: ;
regex: (.*)
target_label: endpoint
replacement: web
action: replace # 指标relabel的配置,如丢掉某些无用的指标
metric_relabel_configs:
- source_labels: [__name__]
separator: ;
regex: etcd_(debugging|disk|request|server).*
replacement: $1
action: drop

3、常见案例

1.获取集群中各节点信息,并按可用区或地域分类

如使用k8s的role:node采集集群中node的数据,可以通过"meta_domain_beta_kubernetes_io_zone"标签来获取到该节点的地域,该label为集群创建时为node打上的标记,kubectl decribe node可以看到。
然后可以通过relabel_configs定义新的值
relabel_configs:
- source_labels: ["meta_domain_beta_kubernetes_io_zone"]
regex: "(.*)"
replacement: $1
action: replace
target_label: "zone" 后面可以直接通过node{zone="XX"}来进行地域筛选 2.过滤信息,或者按照职能(RD、运维)进行监控管理 对于不同职能(开发、测试、运维)的人员可能只关心其中一部分的监控数据,他们可能各自部署的自己的Prometheus Server用于监控自己关心的指标数据,不必要的数据需要过滤掉,以免浪费资源,可以最类似配置;
metric_relabel_configs:
- source_labels: [__name__]
separator: ;
regex: etcd_(debugging|disk|request|server).*
replacement: $1
action: drop action: drop代表丢弃掉符合条件的指标,不进行采集。 3.搭建prometheus联邦集群,管理各IDC(地域)监控实例
如果存在多个地域,每个地域又有很多节点或者集群,可以采用默认的联邦集群部署,每个地域部署自己的prometheus server实例,采集自己地域的数据。然后由统一的server采集所有地域数据,进行统一展示,并按照地域归类 配置:
scrape_configs:
- job_name: 'federate'
scrape_interval: 15s
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
- '{__name__=~"job:.*"}'
- '{__name__=~"node.*"}'
static_configs:
- targets:
- '192.168.77.11:9090'
- '192.168.77.12:9090'

4、服务发现

[root@VM_0_14_centos prometheus]# cat  prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s). # Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- 172.18.0.1:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
- "alert_rules/rules.yml" # A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus' # metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs:
- targets: ['localhost:9090']
labels:
idc: bj - job_name: 'harbor_server'
file_sd_configs: ####基于文件的发现
- files:
- /opt/prometheus/file_sd_configs/harbor_monitor.json ###
refresh_interval: 10s - job_name: 'container'
static_configs:
- targets: ['172.18.0.1:8080'] 配置文件:
[root@VM_0_14_centos prometheus]# cat /opt/prometheus/file_sd_configs/harbor_monitor.json
[
{
"targets": ["172.19.0.14:9100","124.156.173.164:9100"]
}
]

  

k8s全方位监控-prometheus-配置文件介绍以及基于文件服务发现的更多相关文章

  1. k8s全方位监控-prometheus部署

    1.k8s 监控资源对象 2. prometheus简单介绍. https://github.com/prometheus •多维数据模型:由度量名称和键值对标识的时间序列数据•PromSQL:一种灵 ...

  2. k8s全方位监控 -prometheus实现短信告警接口编写(python)

    1.prometheus短信告警接口实现(python)源码如下: import subprocess from flask import Flask from flask import reques ...

  3. .NET Core微服务之路:基于gRPC服务发现与服务治理的方案

    重温最少化集群搭建,我相信很多朋友都已经搭建出来,基于Watch机制也实现了出来,相信也有很多朋友有了自己的实现思路,但是,很多朋友有个疑问,我API和服务分离好了,怎么通过服务中心进行发现呢,这个过 ...

  4. 基于Kubernetes服务发现机制的探讨Non Service

    服务注册 注册中⼼作为一般的RPC/Web服务中的底层设施提供了服务进程元数据(IP, Port, Interface, Group,Method等)存储,被Watch的功能,每个服务进程均需接⼊同⼀ ...

  5. k8s全方位监控中-常用rules配置

    [root@VM_0_48_centos prometheus]# cat alertmanager-configmap.yaml apiVersion: v1 kind: ConfigMap met ...

  6. k8s全方位监控-prometheus-alertmanager部署-配置第一条告警邮件

    1.alertmanager告警插件部署 [root@VM_0_48_centos prometheus]# cat alertmanager-pvc.yaml apiVersion: v1 kind ...

  7. k8s入坑之路(11)kubernetes服务发现

    kubernetes访问场景 1.集群内部访问 2.集群内部访问外部 3.集群外部访问内部 1.集群内部访问 1.pod之间直接ip通讯(利用calico通过路由表经过三层将ip流量转发)由于容器之间 ...

  8. Prometheus 基于文件的服务发现

    Prometheus 基于文件的服务发现 官方文档:https://github.com/prometheus/prometheus/tree/master/discovery 服务发现支持: end ...

  9. prometheus(5)之consul服务自动发现及pushgetway

    pushgetway(push上传metric数据) Pushgateway简介 Pushgateway是prometheus的一个组件,prometheus server默认是通过exporter主 ...

随机推荐

  1. 微服务架构 | 7.1 基于 OAuth2 的安全认证

    目录 前言 1. OAuth2 基础知识 1.1 安全性的 4 个组成部分 1.2 OAuth2 的工作原理 1.3 OAuth2 规范的 4 种类型的授权 1.4 OAuth2 的优势 1.5 OA ...

  2. JavaScript通过父节点ID递归生成JSON树

    JavaScript通过父节点ID递归生成JSON树: · 实现思路:通过递归实现(第一次递归的时候查询出所有的父节点,然后通过当前父节点id不断地去查询所有子节点,直到递归完毕返回)   · 代码示 ...

  3. pandas目录

    pandas目录 1 Lesson1--Pandas是什么 2 Lesson2--Pandas库下载和安装 3 Lesson3--Pandas Series结构 4 Lesson4--Pandas D ...

  4. 「ZJOI2014」星系调查

    「ZJOI2014」星系调查 本题核心在于快速求XPs 的线性假设相斥度. 点\((x1,y1)\)到直线\(y=kx+b\)的距离的平方为\(\displaystyle {(kx1+b-y1)^2} ...

  5. Lua 语言: 语法

    转载请注明来源:https://www.cnblogs.com/hookjc/ -- 两个横线开始单行的注释 --[[  加上两个[和]表示     多行的注释.--]] -------------- ...

  6. 简单理解Zookeeper的Leader选举

    Leader选举是保证分布式数据一致性的关键所在.Leader选举分为Zookeeper集群初始化启动时选举和Zookeeper集群运行期间Leader重新选举两种情况.在讲解Leader选举前先了解 ...

  7. iOS 即使通讯第三方SDK 资料

    第三方即时通讯SDK,下面是一些主流的第三方的即时通讯SDK,尽管不能查看里面的源代码,但通过查看头文件,能为实现自己的即使通讯SDK提供很好的思路.(备用) 容云 容联.云通讯 IMSDK - 轻松 ...

  8. PyTorch深度学习入门笔记(一)PyTorch环境配置及安装

    @ 目录 一.工具安装 1.1 Anaconda 安装 1.2 Pytorch安装 二.编辑器安装 2.1 Pycharm安装 2.2 Jupyter安装 OS: ubuntu 20.04(虚拟机) ...

  9. 鸟哥的Linux私房菜学习笔记——文件权限与目录配置

    Linux的文件权限和目录配置 在linux中的每个用户必需属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念. (1)所有者 一般为文件的创建者,谁创建了该文件,就是天 ...

  10. 记录使用WKWebView进行OC与JS交互所踩过的坑

    目录: 1.页面cookie缓存 2.允许弹出JS的弹框 3.在webview页面加载的时候,添加加载进度条 4.禁止掉webview页面的长按复制粘贴功能 5.设置webview的userAgent ...