监控之美--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,如需转载请自行联系原作者






相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
相关文章
|
2月前
|
存储 数据采集 Prometheus
Grafana Prometheus Altermanager 监控系统
Grafana、Prometheus 和 Alertmanager 是一套强大的开源监控系统组合。Prometheus 负责数据采集与存储,Alertmanager 处理告警通知,Grafana 提供可视化界面。本文简要介绍了这套系统的安装配置流程,包括各组件的下载、安装、服务配置及开机自启设置,并提供了访问地址和重启命令。适用于希望快速搭建高效监控平台的用户。
176 20
|
2月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
233 7
|
2月前
|
Prometheus 运维 监控
Prometheus+Grafana+NodeExporter:构建出色的Linux监控解决方案,让你的运维更轻松
本文介绍如何使用 Prometheus + Grafana + Node Exporter 搭建 Linux 主机监控系统。Prometheus 负责收集和存储指标数据,Grafana 用于可视化展示,Node Exporter 则采集主机的性能数据。通过 Docker 容器化部署,简化安装配置过程。完成安装后,配置 Prometheus 抓取节点数据,并在 Grafana 中添加数据源及导入仪表盘模板,实现对 Linux 主机的全面监控。整个过程简单易行,帮助运维人员轻松掌握系统状态。
330 3
|
2月前
|
Prometheus 监控 Cloud Native
无痛入门Prometheus:一个强大的开源监控和告警系统,如何快速安装和使用?
Prometheus 是一个完全开源的系统监控和告警工具包,受 Google 内部 BorgMon 系统启发,自2012年由前 Google 工程师在 SoundCloud 开发以来,已被众多公司采用。它拥有活跃的开发者和用户社区,现为独立开源项目,并于2016年加入云原生计算基金会(CNCF)。Prometheus 的主要特点包括多维数据模型、灵活的查询语言 PromQL、不依赖分布式存储、通过 HTTP 拉取时间序列数据等。其架构简单且功能强大,支持多种图形和仪表盘展示模式。安装和使用 Prometheus 非常简便,可以通过 Docker 快速部署,并与 Grafana 等可
651 2
|
3月前
|
存储 Prometheus 监控
监控堆外第三方监控工具Prometheus
监控堆外第三方监控工具Prometheus
88 3
|
3月前
|
存储 Prometheus 运维
在云原生环境中,阿里云ARMS与Prometheus的集成提供了强大的应用实时监控解决方案
在云原生环境中,阿里云ARMS与Prometheus的集成提供了强大的应用实时监控解决方案。该集成结合了ARMS的基础设施监控能力和Prometheus的灵活配置及社区支持,实现了全面、精准的系统状态、性能和错误监控,提升了应用的稳定性和管理效率。通过统一的数据视图和高级查询功能,帮助企业有效应对云原生挑战,促进业务的持续发展。
110 3
|
3月前
|
Prometheus 监控 Cloud Native
在 HBase 集群中,Prometheus 通常监控哪些类型的性能指标?
在 HBase 集群中,Prometheus 监控关注的核心指标包括 Master 和 RegionServer 的进程存在性、RPC 请求数、JVM 内存使用率、磁盘和网络错误、延迟和吞吐量、资源利用率及 JVM 使用信息。通过 Grafana 可视化和告警规则,帮助管理员实时监控集群性能和健康状况。
|
3月前
|
Prometheus 运维 监控
智能运维实战:Prometheus与Grafana的监控与告警体系
【10月更文挑战第26天】Prometheus与Grafana是智能运维中的强大组合,前者是开源的系统监控和警报工具,后者是数据可视化平台。Prometheus具备时间序列数据库、多维数据模型、PromQL查询语言等特性,而Grafana支持多数据源、丰富的可视化选项和告警功能。两者结合可实现实时监控、灵活告警和高度定制化的仪表板,广泛应用于服务器、应用和数据库的监控。
479 3
|
6月前
|
Prometheus 监控 Cloud Native
【监控】prometheus传统环境监控告警常用配置
【监控】prometheus传统环境监控告警常用配置
【监控】prometheus传统环境监控告警常用配置
|
3月前
|
Prometheus 运维 监控
智能运维实战:Prometheus与Grafana的监控与告警体系
【10月更文挑战第27天】在智能运维中,Prometheus和Grafana的组合已成为监控和告警体系的事实标准。Prometheus负责数据收集和存储,支持灵活的查询语言PromQL;Grafana提供数据的可视化展示和告警功能。本文介绍如何配置Prometheus监控目标、Grafana数据源及告警规则,帮助运维团队实时监控系统状态,确保稳定性和可靠性。
445 0