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端口可以在浏览器访问**
![](https://img.liuyan.wang/upload/202303071938446.png)

# 二、被监控节点安装 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端口可以在浏览器访问**
![](https://img.liuyan.wang/upload/202303071939034.png)

### 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

登录后,可以重设密码,也可以不重设

**来到主界面**
![](https://img.liuyan.wang/upload/202303071939289.png)

# 四、配置 Grafna,使其对接prometheus的数据

### 1. 配置数据源(Data sources)
![](https://img.liuyan.wang/upload/202303071939495.png)
**点击 `Add data source`**
![](https://img.liuyan.wang/upload/202303071940068.png)
**选择数据源为 `Prometheus`,点击 `Select`,选择类型**
![](https://img.liuyan.wang/upload/202303071940213.png)
**Setings标签:**
**填写 Name 和prometheus的 URL**
![](https://img.liuyan.wang/upload/202303071940351.png)
**填写完毕后,点击 Save & test**
![](https://img.liuyan.wang/upload/202303071940507.png)
**Dashboards标签:**
`Prometheus Stats`、`Prometheus 2.0 Stats`、`Grafana metrics`,统统 `import`
![](https://img.liuyan.wang/upload/202303071941033.png)
### 2. 数据源配置完毕,接下来挑一个好看的 Dashboard 模版来展示数据
![](https://img.liuyan.wang/upload/2023030719411910.png)
**模板可以使用编号为`8919`这个,支持中文,也可以点击[官网](https://link.zhihu.com/?target=https%3A//grafana.com/dashboards "官网")自行挑选**

![](https://img.liuyan.wang/upload/2023030719413210.png)

自定义模板名称,填写数据源类型为`Prometheu`s,  就大功告成
![](https://img.liuyan.wang/upload/2023030719414810.png)

最后展示:

![](https://img.liuyan.wang/upload/202303071942021.png)


> Prometheus还可以添加 MySQL监控、MQ监控、Redis监控、告警管理工具等等插件,作为入门文章,这里就不一一展示了,我也不会


![](https://img.liuyan.wang/upload/2023030719421610.png)

最后修改:2024 年 03 月 27 日
如果觉得我的文章对你有用,请随意赞赏