基于Prometheus的微服务应用监控

CodePulse
• 阅读 4057

基于Prometheus的微服务应用监控

导读

Prometheus是一套开源的系统监控报警框架。它启发于Google的borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发,并于2015年正式发布。2016年,Prometheus 正式加入 Cloud Native Computing Foundation,成为受欢迎度仅次于Kubernetes 的项目。

现今公司分布式系统都采用Spring Cloud去实现,其简单高效为开发者带来了极大的便利。Spring Cloud本身也有对服务的监控应用指标,如Actuator可以显示系统各种基本信息。但是对于我们业务开发人员,其指标还是过于简单,为了能够详细清楚的描述当前一个微服务的应用指标,我们使用Prometheus对微服务进行指标采集与分析。

材料准备:

1.Spring Boot(被监控的对象微服务应用)

2.Prometheus(指标采集的时间序列数据库)

3.Alertmanager(监控预警组件)

4.Micrometer(微服务指标暴露者)

5.Grafana(对指标进行丰富的图像展示)

Prometheus搭建

Prometheus 是一套开源的系统监控报警框架。它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发,并于 2015 年正式发布。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,成为受欢迎度仅次于 Kubernetes 的项目。

作为新一代的监控框架,Prometheus 具有以下特点:

强大的多维度数据模型:

1.时间序列数据通过metric 名和键值对来区分。

2.所有的metrics 都可以设置任意的多维标签。

3.数据模型更随意,不需要刻意设置为以点分隔的字符串。

4.可以对数据模型进行聚合,切割和切片操作。

5.支持双精度浮点类型,标签可以设为全unicode。

灵活而强大的查询语句(PromQL):在同一个查询语句,可以对多个 metrics 进行乘法、加法、连接、取分数位等操作。

易于管理:Prometheus server 是一个单独的二进制文件,可直接在本地工作,不依赖于分布式存储。

高效:平均每个采样点仅占 3.5 bytes,且一个 Prometheus server 可以处理数百万的 metrics。

使用 pull 模式采集时间序列数据,这样不仅有利于本机测试而且可以避免有问题的服务器推送坏的 metrics。

可以采用 push gateway 的方式把时间序列数据推送至 Prometheus server 端。

可以通过服务发现或者静态配置去获取监控的 targets。

有多种可视化图形界面。

易于伸缩。

需要指出的是,由于数据采集可能会有丢失,所以 Prometheus 不适用对采集数据要 100% 准确的情形。但如果用于记录时间序列数据,Prometheus 具有很大的查询优势,此外,Prometheus 适用于微服务的体系架构。

# 下载2.3.2 / 2018-07-12

wget https://github.com/prometheus...

# 解压

tar -zxvf prometheus-2.3.2.linux-amd64.tar.gz

# 修改配置文件

vim prometheus.yml

global:

scrape_interval: 3s

evaluation_interval: 3s

rule_files:

  • "/home/prometheus/rules/*.rules"

scrape_configs:

  • job_name: 'prometheus'

static_configs:

  • targets: ['127.0.0.1:9091']

# 启动

./prometheus --web.listen-address=0.0.0.0:9091 --web.enable-lifecycle

访问http://127.0.0.1:9091/ 看到下图即安装成功

基于Prometheus的微服务应用监控

Alertmanager搭建

1.下载Alertmanager

# 下载

wget https://github.com/prometheus...

# 解压

tar -zxvf alertmanager-0.15.2.linux-amd64.tar.gz

2.配置 alertmanager.yml

vim alertmanager.yml

global:

resolve_timeout: 2h

smtp_from: '{{ template "email.from" . }}'

smtp_smarthost: 'smtp.exmail.qq.com:25'

smtp_auth_username: 'xxxx@analysys.com.cn'

smtp_auth_password: 'yourpassword'

templates:

- '/home/prometheus/alertmanager/template/*.tmpl'

route:

group_by: ['node_up']

group_wait: 5s

group_interval: 5s

repeat_interval: 1h

receiver: 'dingding'

route:

group_by: ['webhook_http']

group_wait: 5s

group_interval: 5s

repeat_interval: 1h

receiver: 'email'

inhibit_rules:

- source_match:

severity: 'critical'

target_match:

severity: 'warning'

severity: page

equal: ['node_up']

receivers:

- name: 'wechat'

wechat_configs:

  • to_user: '@all'

agent_id: '1000002'

corp_id: 'corp_id'

api_secret: 'api_secret'

message: '{{ template "wechat.text" . }}'

send_resolved: true

- name: 'email'

email_configs:

  • to: '{{ template "email.to" . }}'

text: '{{ template "email.to.text" . }}'

send_resolved: true

- name: 'dingding'

webhook_configs:

send_resolved: true

3.模版文件实例

{{ define "email.from" }}xxxx@analysys.com.cn{{ end}}

{{ define "email.to" }}xxxx@analysys.com.cn{{ end}}

{{ define "email.to.text" }}

警告:

{{ template "__my_text_alert_list" .Alerts.Firing }}

{{ end}}

4.配置Prometheus Rule 预警规则

vim node_alert.rules

groups:

- name: node_up

rules:

  • alert: node_up

expr: up{job="node"} == 0

for: 15s

labels:

annotations:

summary: "{{ $labels.instance }} 已停止运行!"

# 启动Alertmanager

./alertmanager --config.file=alertmanager.yml

# 刷新Prometheus配置

curl -X POST http://127.0.0.1:9091/-/reload

5.查看Rules

访问:http://127.0.0.1:9091/rules

基于Prometheus的微服务应用监控

Grafana搭建,并配置Prometheus数据源

Grafana是一个开源的度量分析与可视化套件。经常被用作基础设施的时间序列数据和应用程序分析的可视化,它在其他领域也被广泛的使用包括工业传感器、家庭自动化、天气和过程控制等。

Grafana支持许多不同的数据源。每个数据源都有一个特定的查询编辑器,该编辑器定制的特性和功能是公开的特定数据来源。

# 下载Grafana

wget https://s3-us-west-2.amazonaw...

# 解压

tar -zxvf grafana-5.2.2.linux-amd64.tar.gz

# 使用默认配置运行

./grafana-server -config /home/prometheus/grafana/grafana-5.2.2/conf/defaults.ini

1.访问http://127.0.0.1:3000/

登录:admin admin

2.配置数据源
基于Prometheus的微服务应用监控
基于Prometheus的微服务应用监控

保存

开发SpringBoot微服务监控

Micrometer 是一款监控指标的度量类库,可以让在没有供应商锁定的情况下对 JVM 的应用程序代码进行调整。

1.在项目pom.xml中引入Jar

<micrometer.version>1.0.6</micrometer.version>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>io.micrometer</groupId>

<artifactId>micrometer-spring-legacy</artifactId>

<version>${micrometer.version}</version>

</dependency>

<dependency>

<groupId>io.micrometer</groupId>

<artifactId>micrometer-registry-prometheus</artifactId>

<version>${micrometer.version}</version>

</dependency>

2.编写启动配置类

package cn.analysys.monitor.alertmanager.config;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import io.micrometer.core.instrument.MeterRegistry;

import io.micrometer.spring.autoconfigure.MeterRegistryCustomizer;

/**

*

* Description: MicrometerConfiguration<br/>

*

* @author litaiqing

* @date: 2018年8月14日 下午7:10:55

* @version 1.0

* @since JDK 1.8

*/

@Configuration

public class MicrometerConfiguration {

private static final Logger logger = Logger.getLogger(MicrometerConfiguration.class);

@Bean

@ConditionalOnMissingBean

MeterRegistryCustomizer<?> meterRegistryCustomizer(MeterRegistry meterRegistry,

@Value("${spring.application.name}") String application) {

logger.info(application);

return mr -> {

mr.config().commonTags("application", application);

};

}

}

3.配置application.properties

spring:

application:

name: alertmanager-webhook

server:

port: 9101

tomcat:

uri-encoding: UTF-8

context-path: /

security:

basic:

enabled: true

user:

name: webhook

password: analysys_cs

endpoints:

metrics:

enabled: true

health:

enabled: true

path: /health

4.打包并发布应用

访问:http://127.0.0.1:9101/prometheus 即可查看应用实时指标。

让Prometheus拉取监控微服务指标

1.配置Prometheus监控

global:

scrape_interval:     3s

evaluation_interval: 3s

alerting:

alertmanagers:

  • static_configs:
  • targets:
  • 127.0.0.1:9093

rule_files:

  • "/home/prometheus/rules/*.rules"

scrape_configs:

  • job_name: 'prometheus'

static_configs:

  • targets: ['127.0.0.1:9091']
  • job_name: 'node'

static_configs:

  • targets: ['127.0.0.1:9100']
  • job_name: 'alertmanager-webhook'

metrics_path: /prometheus

basic_auth:

username: webhook

password: analysys_cs

static_configs:

  • targets: ['127.0.0.1:9101']

# 刷新Prometheus配置

curl -X POST http://127.0.0.1:9091/-/reload

2.Grafana导入模版

基于Prometheus的微服务应用监控
基于Prometheus的微服务应用监控

查看JVM监控面板

基于Prometheus的微服务应用监控

预警展示

在node_up停机的情况下会收到下面的通知:

基于Prometheus的微服务应用监控

重新启动node_up运行情况下会收到下面的通知:

基于Prometheus的微服务应用监控

监控总结

此实例仅描述微服务监控解决的主流程,在实际生产过程中,监控预警系统需要有高可靠性。

因此Prometheus要采用多台服务器拉去标签,分别存储。Alertmanager预警需要配置集群预警,使用Gossip机制去完成过滤、去噪等操作。

同时由于微服务上下线非常频繁,迭代速度较快,因此需要有自动化服务发现(如Consul)配置来替代当前的手动配置。

针对上述问题,易观技术群组已经解决并投入了使用,本次技术分享主要分享监控的主体思路与实践操作,请敬请期待Prometheus微服务监控生产实战的后篇分享。

点赞
收藏
评论区
推荐文章
DevOpSec DevOpSec
4年前
Prometheus架构与实践分享
Prometheus已经被广泛应用于数据中心监控,尤其是和Kubernetes结合的容器监控。本文主要从架构分析到落地实践,详细介绍Prometheus原理和使用。对比Prometheus与其他监控工具(Zabbix、OpenFalcon)的特点与使用场景。然后介绍Prometheus与Kubernetes集成,主要从监控和自动伸缩两个方面。最后通过企业案
Johnny21 Johnny21
4年前
Prometheus学习系列(四)之FAQ
一、一般问题1\.Prometheus是什么?Prometheus是一款高活跃生态系统的开源系统监控和警告工具包。详见2\.Prometheus与其他的监控系统比较详见3\.Prometheus有什么依赖?Prometheus服务独立运行,没有其他依赖4\.Prometheus有高可用的保证吗?
Johnny21 Johnny21
4年前
Prometheus学习系列(二)之Prometheus first steps
欢迎来到Prometheus!Prometheus是一个监控平台,通过在监控目标上的HTTP端点来收集受监控目标的指标。本指南将向您展示如何使用Prometheus安装,配置和监控我们的第一个资源。您将下载,安装并运行Prometheus。您还将下载并安装exporter,这些工具可在主机和服务上公开时间序列数据。我们的第一个exporter将是Prome
Johnny21 Johnny21
4年前
Prometheus学习系列(三)之监控对比
一、Prometheusvs.Graphite1.1范围Graphite专注于查询语言和图表特征的时间序列数据库。其他都需要依赖外部组件实现。Prometheus是一个基于时间序列数据的完整监控系统和趋势系统,包括内置和主动抓取、存储、查询、图表展示和报警功能。它懂得监控系统和趋势系统应该是什么
Stella981 Stella981
4年前
Prometheus部署+邮箱告警+企业微信告警+钉钉告警
Prometheus部署邮箱报警企业微信报警钉钉报警1部署Prometheusserver1.1下载二进制包$wgethttps://github.com/prometheus/prometheus/releases/download/v2
Stella981 Stella981
4年前
Prometheus 和 它的监控需求朋友们 ;)
这篇博文的面向群体是还不太了解Prometheus和想要开始使用Prometheus的人群.本文想做的事是想尽力讲清楚Prometheus是如何看待监控这件事情以及Prometheus是如何实现这些需求的.本文中不会出现的内容:跟Prometheus实现细节有太多相关的东西等当想看监控的时候,我们到底想要什么?
Stella981 Stella981
4年前
K8S 部署 Web UI
在早期的版本中Kubernetes可以在Dashboard中看到heapster提供的一些图表信息,在后续的版本中会陆续移除掉heapster,现在更加流行的监控工具是prometheus,prometheus是Google内部监控报警系统的开源版本,所以这里的Dashboard 不会看到有图表信息。1.下载官方的yaml文件
Stella981 Stella981
4年前
Prometheus简介
一、介绍PrometheusPrometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fsoundcloud.com%2F)公司开发的。Prometheus的主要特点是:
Stella981 Stella981
4年前
Prometheus监控告警浅析
前言最近有个新项目需要搞一套完整的监控告警系统,我们使用了开源监控告警系统Prometheus;其功能强大,可以很方便对其进行扩展,并且可以安装和使用简单;本文首先介绍Prometheus的整个监控流程;然后介绍如何收集监控数据,如何展示监控数据,如何触发告警;最后展示一个业务系统监控的demo。监控架构Prometheus的整个
CodePulse
CodePulse
Lv1
堪怨王孙,不记归期早。
文章
5
粉丝
0
获赞
0