prometheus + Grafana是观测服务器性能比较流行的方案之一
以下是基本功能搭建流程,就四步,非常容易
一、安装 prometheus
i 在 Prometheus + Grafana 的体系架构下,Prometheus 相当于一个注册中心。
1. 下载 prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
加速版:
wget https://mirror.liuyan.wang/download/prometheus/prometheus/prometheus-2.27.1.linux-amd64.tar.gz
2. 解压
tar xvfz prometheus-2.27.1.linux-amd64.tar.gz
cd prometheus-2.27.1.linux-amd64
3. 配置文件瞜一眼
vim prometheus.yml
# 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: ['localhost:9090']
i 最后一行看到prometheus默认对外端口:9090
4. 启动
./prometheus --config.file=prometheus.yml
此时使用9090端口可以在浏览器访问
二、被监控节点安装 node_exporter
node_exporter
装在节点上收集信息,提供给prometheus
,granfna
再从prometheus
获取并展示
1. 下载 node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
加速版:
wget https://mirror.liuyan.wang/download/prometheus/node_exporter/node_exporter-1.1.2.linux-amd64.tar.gz
2. 解压
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
cd node_exporter-1.1.2.linux-amd64
3. 启动
./node_exporter
默认端口9100
此时使用9100端口可以在浏览器访问
4. 修改 prometheus.yml
prometheus和节点端都已部署完毕,现将两者联系到一起
在 prometheus
端的prometheus.yml
中加入被监控节点信息
vim prometheus.yml
拉到最下面,添加一段:
### 被监控节点 ###
- job_name: 'node1'
static_configs:
- targets: ['IP:9100']
这里把IP换成被监控节点IP,修改完后完整配置:
# 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: ['localhost:9090']
### 被监控节点 ###
- job_name: 'node1'
static_configs:
- targets: ['IP:9100']
5. 重启prometheus
#停掉进程后:
./prometheus --config.file=prometheus.yml
至此节点已经把服务器信息提交给prometheus
,接下来安装granfna
并与prometheus
对接
三、安装Grafna
1. 下载 Grafna
wget https://dl.grafana.com/oss/release/grafana-8.4.1.linux-amd64.tar.gz
加速版:
wget https://mirror.liuyan.wang/download/prometheus/granfna/grafana-8.4.1.linux-amd64.tar.gz
2. 解压
tar xvzf grafana-8.0.1.linux-amd64.tar.gz
3. 启动
./bin/grafana-server web
启动后通过IP:3000访问granfna监控平台
w 默认初始用户名密码:
admin
admin
登录后,可以重设密码,也可以不重设
来到主界面
四、配置 Grafna,使其对接prometheus的数据
1. 配置数据源(Data sources)
点击 Add data source
选择数据源为 Prometheus
,点击 Select
,选择类型
Setings标签:
填写 Name 和prometheus的 URL
填写完毕后,点击 Save & test
Dashboards标签:Prometheus Stats
、Prometheus 2.0 Stats
、Grafana metrics
,统统 import
2. 数据源配置完毕,接下来挑一个好看的 Dashboard 模版来展示数据
模板可以使用编号为8919
这个,支持中文,也可以点击官网自行挑选
自定义模板名称,填写数据源类型为Prometheu
s, 就大功告成
最后展示:
Prometheus还可以添加 MySQL监控、MQ监控、Redis监控、告警管理工具等等插件,作为入门文章,这里就不一一展示了,我也不会