Prometheus + Grafana 环境搭建
什么是Prometheus?
Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本。
2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将Prometheus纳入其下第二大开源项目。Prometheus目前在开源社区相当活跃。
Prometheus和Heapster(Heapster是K8S的一个子项目,用于获取集群的性能数据。)相比功能更完善、更全面。Prometheus性能也足够支撑上万台规模的集群。
Prometheus的特点
- 多维度数据模型。
- 灵活的查询语言。
- 不依赖分布式存储,单个服务器节点是自主的。
- 通过基于HTTP的pull方式采集时序数据。
- 可以通过中间网关进行时序列数据推送。
- 通过服务发现或者静态配置来发现目标服务对象。
- 支持多种多样的图表和界面展示,比如Grafana等。
安装步骤
本文中安装环境为mac,其他环境的安装请自行到官网查看:
下载解压
在官网 下在最新版本压缩包:prometheus-2.3.0.darwin-amd64.tar.gz
解压并进入该目录:
tar xvfz prometheus-*.tar.gz
cd prometheus-*
修改配置文件
本文将配置文件修改为检测Prometheus自身:
修改配置文件为:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
配置文件具体含义: configuration documentation
启动Prometheus
在当前文件夹中启动Prometheus:
# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml
在浏览器页面 localhost:9090 中就可以看到Prometheus的web页面了:

Grafana
Grafana安装
安装步骤参考grafana官方文档:http://docs.grafana.org/installation/mac/
使用homebrew安装:
brew update
brew install grafana
安装成功后你可以使用默认配置启动:
brew services start grafana
登录
使用默认账号密码 : admin/admin 登录

在首页点击添加数据源,Grafana默认支持Prometheus,不需要其他的插件

配置Prometheus数据源:

点击右侧Dashboards,import三个数据源,点击任意一个就可以查看它的dashboard了。

Grafana基本操作
新建Dashboard

新建图表

导入图表

新建数据源



Type 选 Prometheus, 填好URL,如果是Prometheus默认的配置,那么就是
http://localhost:9090