监控之美--prometheus配置文件动态管理

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
简介:

Prometheus是一套开源的监控、报警解决方案,是由SoundCloud公司开发的,从 2012 年开始编写代码,再到 2015 年 开源以来,该项目有非常活跃的社区和开发人员,目前在全世界最大的男性交友社区上已经有了1.1w多star;2016 年 Prometheus 成为继 k8s 后,成为第二名 CNCF(Cloud Native Computing Foundation) 成员。

 Google SRE的书内也曾提到跟他们BorgMon监控系统相似的开源实现是Prometheus,作为新一代开源解决方案,很多理念与 Google SRE 运维之道不谋而合。作为新一代的监控解决方案,现在最常见的用法是与Kubernetes容器管理系统进行结合进行监控,但不要误解为它仅仅是一个容器的监控,当你深入了解他之后,你会发现他能做很多事情。

 这里我想多说一下,之前一直纠结于选择Prometheus还是Open-falcon。这两者都是非常棒的新一代监控解决方案,后者是小米公司开源的,目前包括小米、金山云、美团、京东金融、赶集网等都在使用Open-Falcon,最大区别在于前者采用的是pull的方式获取数据,后者使用push的方式,暂且不说这两种方式的优缺点。简单说下我喜欢Prometheus的原因,大概有5点吧,1、开箱即用,部署运维非常方便 2、prometheus的社区非常活跃 3、自带服务发现功能 4、简单的文本存储格式,进行二次开发非常方便。 5、最重要的一点,他的报警插件我非常喜欢,带有分组、报警抑制、静默提醒机制。这里并没有贬低open-falcon的意思,还是那句老话适合自己的才是最好的。

Consul-template自动刷新配置文件

 由于Prometheus是“拉”的方式主动监测,所以需要在server端指定被监控节点的列表。当被监控的节点增多之后,每次增加节点都需要更改配置文件,非常麻烦,我这里用consul-template+consul动态生成配置文件,这种方式同样适用于其他需要频繁更改配置文件的服务。另外一种解决方案是etcd+confd,基本现在主流的动态配置系统分这两大阵营。consul-template的定位和confd差不多,不过它是consul自家推出的模板系统。

实现

先看下Prometheus的配置文件样例:

      1      

      2      

      3      

      4      

      5      

      6      

      7      

      8      

      9      

      10      

      11      

- job_name: 'node-exporter'

static_configs:

- targets: ['172.30.100.10:9100']

labels:

hostname: 'web1'

- targets: ['172.30.100.11:9100']

labels:

hostname: 'web2'

- targets: ['172.30.100.12:9100']

labels:

hostname: 'web3'

每次新加监控节点的时候,只需要添加一个新的targets即可,“hostname”是我自定义的一个label标签,方便区分。那么这里就产生一个问题,当targets的数量达到几百上千之后,配置文件看起来就会特别冗余。所以有经验的运维人就会想到用include的方式,把其他的配置文件包含进来,这样就把一个大而冗余的主配置文件,切分成一个个小的配置文件。Prometheus这里用的方法就是基于文件的服务发现--"file_sd_config"。我这里在prometheus下面新建了一个conf.d的目录,包含两个子配置文件,分别监控linux和windows的机器:

39fb00048d07978c99b2

file_sd_config参考样例

子配置文件可以是YAML或JSON格式,我这里用的JSON格式,示例如下:

      1      

      2      

      3      

      4      

      5      

      6      

      7      

      8      

      9      

      10      

      11      

      12      

      13      

      14      

      15      

cat conf.d/lnode-discovery.json

[

{

"targets": ["172.30.100.2:9100"],

"labels": {

"hostname": "consul02"

}

},

{

"targets": ["172.30.100.1:9100"],

"labels": {

"hostname": "consul01"

}

}

]

结合服务发现实现文件的动态更新

有了子配置文件,新加监控节点的时候只需要更改子配置文件的内容即可。我们可以预先定义一个子配置文件的模板,用consul-template渲染这个模板,实现文件的动态更新。具体方法如下:

1、下载consul-template


      1      

      2      

      3      

      4      

# cd /data/consul_template #软件安装目录

# wget -c https://releases.hashicorp.com/consul-template/0.19.3/consul-template_0.19.3_linux_amd64.zip

# unzip consul-template_0.19.2_linux_amd64.zip

# mkdir templates # 创建consul-template的模板文件目录

consul-template继承了consul的简约风格,解压之后只有一个二进制软件包。我们创建一个存放模板文件的目录,方便以后使用。

2、创建consul-template的配置文件

配置文件的格式遵循:HashiCorp Configuration Language。我的配置文件示例如下:

      1      

      2      

      3      

      4      

      5      

      6      

      7      

      8      

      9      

      10      

      11      

      12      

      13      

      14      

      15      

      16      

      17      

      18      

      19      

      20      

      21      

      22      

      23      

      24      

      25      

      26      

      27      

      28      

      29      

      30      

      31      

      32      

      33      

      34      

      35      

      36      

      37      

      38      

      39      

      40      

      41      

      42      

      43      

      44      

      45      

      46      

      47      

      48      

      49      

      50      

      51      

      52      

      53      

      54      

      55      

      56      

      57      

      58      

      59      

      60      

      61      

      62      

      63      

      64      

      65      

      66      

      67      

      68      

      69      

      70      

      71      

      72      

      73      

      74      

      75      

      76      

      77      

# cat consul-template.conf

log_level = "warn"

syslog {

# This enables syslog logging.

enabled = true

# This is the name of the syslog facility to log to.

facility = "LOCAL5"

}

consul {

# auth {

# enabled = true

# username = "test"

# password = "test"

# }

address = "172.30.100.45:8500"

# token = "abcd1234"

retry {

enabled = true

attempts = 12

backoff = "250ms"

# If max_backoff is set to 10s and backoff is set to 1s, sleep times

# would be: 1s, 2s, 4s, 8s, 10s, 10s, ...

max_backoff = "3m"

}

}

# This block defines the configuration for a template. Unlike other block

# this block may be specified multiple times to configure multiple templates.

template {

# This is the source file on disk to use as the input template. This is often

# called the "Consul Template template". This option is required if not using

# the `contents` option.

# source = "/path/on/disk/to/template.ctmpl"

source = "/data/consul_template/templates/lnode-discovery.ctmpl"

# This is the destination path on disk where the source template will render.

# If the parent directories do not exist, Consul Template will attempt to

# create them.

# destination = "/path/on/disk/where/template/will/render.txt"

destination = "/data/prometheus/prometheus-1.7.1.linux-amd64/conf.d/lnode-discovery.json"

# This is the optional command to run when the template is rendered. The

# command will only run if the resulting template changes. The command must

# return within 30s (configurable), and it must have a successful exit code.

# Consul Template is not a replacement for a process monitor or init system.

command = ""

# This is the maximum amount of time to wait for the optional command to

# return. Default is 30s.

command_timeout = "60s"

# This option backs up the previously rendered template at the destination

# path before writing a new one. It keeps exactly one backup. This option is

# useful for preventing accidental changes to the data without having a

# rollback strategy.

backup = true

# This is the `minimum(:maximum)` to wait before rendering a new template to

# disk and triggering a command, separated by a colon (`:`). If the optional

# maximum value is omitted, it is assumed to be 4x the required minimum value.

# This is a numeric time with a unit suffix ("5s"). There is no default value.

# The wait value for a template takes precedence over any globally-configured

# wait.

left_delimiter = "{$"

right_delimiter = "$}"

wait {

min = "2s"

max = "20s"

}

}

template {

source = "/data/consul_template/templates/wnode-discovery.ctmpl"

destination = "/data/prometheus/prometheus-1.7.1.linux-amd64/conf.d/wnode-discovery.json"

command = ""

backup = true

command_timeout = "60s"

left_delimiter = "{$"

right_delimiter = "$}"

wait {

min = "2s"

max = "20s"

}

}

主要配置参数:

syslog: 启用syslog,这样服务日志可以记录到syslog里。

consul: 这里需要设置consul服务发现的地址,我这里无需认证,所以把auth注释了。consul服务的搭建可以参考我之前的文章。值得一提的是,backoff和max_backoff选项,backoff设置时间间隔,当未从consul获取到数据时会进行重试,并以2的倍数的时间间隔进行。比如设置250ms,重试5次,那么每次的时间间隔为:250ms,500ms,1s,2s,4s,直到达到max_backoff的阀值;如果max_backoff设为2s,那么第五次重试的时候还是间隔2s,即250ms,500ms,1s,2s,2s。

template:定义模板文件位置。主要选项是source,destination和command,当backup=true的时候,会备份上一次的配置,并以bak后缀结尾。

  • source:consul-template的模板文件,用来进行渲染的源文件。

  • destination:consul-template的模板被渲染之后的文件位置。比如这里即是我prometheus基于文件发现的子配置文件位置:/data/prometheus/prometheus-1.7.1.linux-amd64/conf.d/下的文件。

  • command:文件渲染成功之后需要执行的命令。prometheus这里会自动发现文件的更改,所以我这里无需任何命令,给注释掉了。像nginx、haproxy之类的服务,一般更改完配置文件之后都需要重启,这里可以设置“nginx -s reload”之类的命令。

  • command_timeout:设置上一步command命令执行的超时时间。

  • left_delimiter和right_delimiter:模板文件中分隔符。默认是用“{{}}”设置模板,当产生冲突的时候可以更改这里的设置。比如我这里由于用ansible去推送的模板文件,“{{}}”符号与Jinja2的语法产生了冲突,所以改为了“{$$}”符号。

当有多个模板需要渲染的时候,这里可以写多个template。

3、服务启动

启动consul-template服务,指定配置文件。

      1      

#./consul-template -config ./consul-template.conf

4、模板渲染

根据目标文件的格式去渲染consul-template的模板,比如我这里的prometheus基于文件的服务发现模板如下:

      1      

      2      

      3      

      4      

      5      

      6      

      7      

      8      

      9      

      10      

      11      

      12      

      13      

      14      

      15      

      16      

      17      

cat templates/lnode-discovery.ctmpl

[

{$ range tree "prometheus/linux" $}

{

"targets": ["{$ .Value $}"],

"labels": {

"hostname": "{$ .Key $}"

}

},

{$ end $}

{

"targets": ["172.30.100.1:9100"],

"labels": {

"hostname": "consul01"

}

}

]

循环读取consul的K/V存储prometheus/linux/目录下的值,"targets"取的是Key,hostname取的是Key的值。

Consul的K/V存储示例如下,每次录入一个数据,即是对应prometheus配置文件里的"hostname:targets"

wKiom1m4r3DQ-xoZAAC10KvmM3Y473.png-wh_50

consul K/V示例

这里有一个小技巧:prometheus的配置文件里,多个targets是用逗号“,”分割的,而最后的那一个targets后面不能带逗号,所以我在模板文件里单独写了一个targets,这样就无需关心这一例外情况。

5、数据在线添加实现配置文件的动态更新

现在在打开consul的ui界面,默认是8500端口,在KEY/VALUE的prometheus/linux/目录下新加一个consul02、consul03...,最后生成的配置文件格式如下:

wKiom1m4r0Cz5DaTAAM45g752d0626.png-wh_50

至此,prometheus基于文件的服务发现,初步完成。





     本文转自Jx战壕  51CTO博客,原文链接:http://blog.51cto.com/xujpxm/1964878,如需转载请自行联系原作者






相关文章
|
4月前
|
Prometheus 监控 Cloud Native
云原生监控实战:Prometheus+Grafana快速搭建指南
云原生监控实战:Prometheus+Grafana快速搭建指南
|
4月前
|
存储 Prometheus 监控
OSS监控体系搭建:Prometheus+Grafana实时监控流量、错误码、存储量(开源方案替代云监控自定义视图)
本方案基于Prometheus构建OSS监控系统,涵盖架构设计、指标采集、可视化、告警及性能优化,助力企业实现高可用、低成本的自建监控体系。
461 1
|
5月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
433 79
|
4月前
|
存储 监控 Cloud Native
云原生监控实战:Prometheus+Grafana打造RDS多维度预警体系
本方案构建了基于Prometheus与Thanos的云原生RDS监控体系,涵盖数据采集、存储、可视化与告警全流程。支持10万+QPS采集、90%存储压缩,具备<30秒告警延迟能力。通过自定义指标与智能预警策略,显著提升故障发现效率,实现分钟级响应。
343 5
|
4月前
|
Prometheus 监控 Cloud Native
|
3月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
|
7月前
|
Prometheus Kubernetes 监控
Kubernetes监控:Prometheus与AlertManager结合,配置邮件告警。
完成这些步骤之后,您就拥有了一个可以用邮件通知你的Kubernetes监控解决方案了。当然,所有的这些配置都需要相互照应,还要对你的Kubernetes集群状况有深入的了解。希望这份指南能帮助你创建出适合自己场景的监控系统,让你在首次发现问题时就能做出响应。
355 22
|
11月前
|
Prometheus 运维 监控
智能运维实战:Prometheus与Grafana的监控与告警体系
【10月更文挑战第26天】Prometheus与Grafana是智能运维中的强大组合,前者是开源的系统监控和警报工具,后者是数据可视化平台。Prometheus具备时间序列数据库、多维数据模型、PromQL查询语言等特性,而Grafana支持多数据源、丰富的可视化选项和告警功能。两者结合可实现实时监控、灵活告警和高度定制化的仪表板,广泛应用于服务器、应用和数据库的监控。
1008 3
|
10月前
|
存储 数据采集 Prometheus
Grafana Prometheus Altermanager 监控系统
Grafana、Prometheus 和 Alertmanager 是一套强大的开源监控系统组合。Prometheus 负责数据采集与存储,Alertmanager 处理告警通知,Grafana 提供可视化界面。本文简要介绍了这套系统的安装配置流程,包括各组件的下载、安装、服务配置及开机自启设置,并提供了访问地址和重启命令。适用于希望快速搭建高效监控平台的用户。
537 20
|
10月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
852 7
下一篇
oss教程