0%

Prometheus入门篇

Prometheus简介

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open source project and maintained independently of any company. To emphasize this, and to clarify the project’s governance structure, Prometheus joined the Cloud Native Computing Foundation in 2016 as the second hosted project, after Kubernetes.

特性、组件和架构等更多信息,参考Prometheus官方文档

安装Prometheus server

本节在CentOS7虚拟机上安装Prometheus server,虚拟机IP为192.168.56.130,安装流程主要参考Prometheus - GETTING STARTED

1、下载Prometheus server
访问Prometheus官网下载页,这里选择下载prometheus-2.15.2.linux-amd64.tar.gz

1
2
cd /opt
wget https://github.com/prometheus/prometheus/releases/download/v2.15.2/prometheus-2.15.2.linux-amd64.tar.gz

2、解压并进行配置

1
2
3
4
tar -xzvf prometheus-2.15.2.linux-amd64.tar.gz
mv prometheus-2.15.2.linux-amd64 prometheus
cd prometheus
vim prometheus.yml

修改targets为0.0.0.0:9090,修改后的prometheus.yml如下:

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

# 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:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_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: ['0.0.0.0:9090']

3、启动prometheus
./prometheus --config.file=prometheus.yml

除了上面的安装方法,还可以下载Prometheus的Docker images,使用Docker安装。

简单使用

1、访问metrics
浏览器访问 http://192.168.56.130:9090/metrics
可以看到prometheus的一些信息:

1
2
3
4
5
6
7
8
9
10
11
12
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 6.078e-06
go_gc_duration_seconds{quantile="0.25"} 9.877e-06
go_gc_duration_seconds{quantile="0.5"} 9.9e-06
go_gc_duration_seconds{quantile="0.75"} 1.1475e-05
go_gc_duration_seconds{quantile="1"} 1.2132e-05
go_gc_duration_seconds_sum 4.9462e-05
go_gc_duration_seconds_count 5
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 35

2、查看图表
浏览器访问 http://192.168.56.130:9090/graph
在Expression输入框中输入:prometheus_target_interval_length_seconds,然后点击Execute,再点击下面Tab中的Graph。
可以看到一个图表,图表的含义是,emm,不知道。

3、收集其他metrics
想要收集metrics,首先要安装配置好exporter。
比如想要收集jmx metrics,那么需要安装prometheus/jmx_exporter

Prometheus服务发现

1
2
3
4
5
6
7
8
9
static_configs: #静态服务发现,基于prometheus配置文件指定的监控目标

file_sd_configs: #基于指定的文件实现服务发现,基于指定的文件发现监控目标

kubernetes_sd_configs: #基于 Kubernetes API实现的服务发现,让prometheus动态发现kubernetes中被监控的目标

dns_sd_configs: #DNS服务发现监控目标

consul_sd_configs: #Consul服务发现,基于consul服务动态发现监控目标

参考文档:

后记

Prometheus也有不少问题,比如数据量大的时候需要拆分集群,聚合数据很难数据去重,可用性较低等。因此出现了Thanos,能够解决Prometheus的很多问题,详情参考分布式 Promethues 之 Thanos

  • 本文作者: 好好学习的郝
  • 本文链接: https://www.voidking.com/dev-prometheus-start/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!源站会及时更新知识点及修正错误,阅读体验也更好。欢迎分享,欢迎收藏~