Prometheus + Grafana 在 CentOS 中搭建可视化性能监控平台 Prometheus 部署 Prometheus + Grafana 附录:问题

Prometheus

Prometheus ,由谷歌研发的一款开源的监控软件。

Prometheus 是一款基于时序数据库的开源监控告警系统,通过安装在远程机器上的 exporter 收集数据并通过HTTP协议从远程的机器上获取数据,最后存储在本地的时序数据库上。

Node exporter 是 Prometheus 最常用的一种 exporter,主要负责收集 Linux 的 /proc 以及 /sys 目录下的系统文件获取操作系统运行状态。

另外还有一些其他的 exporter 分别用来监控不同的应用程序:

  • reids exporter:通过Reids命令行获取指标
  • mysql exporter:通过读取数据库监控表获取MySQL的性能数据

这些获取到的监控数据,再通过 Grafana 实现可视化、图形化、图表化的信息展示。

这样,我们在做性能测试、生产服务器监控的时候,就不用 Linux 命令,而且更直观。

本篇文章演示如何在一台 Centos 服务器上部署 Prometheus。

部署 Prometheus + Grafana

在 Centos 上部署 Prometheus + Grafana 包含以下 3 个大的步骤:

  1. 安装 Prometheus,并部署成服务
  2. 安装 Node exporter, 并部署成服务
  3. 安装 Grafana

安装 Prometheus

创建 Prometheus 用户

$ useradd -m -s /bin/false prometheus

创建配置目录

$ mkdir /etc/prometheus

$ mkdir /var/lib/prometheus

$ chown prometheus:prometheus /var/lib/prometheus/

下载最新版的 prometheus

  1. 通过 wget 命令下载

    # 切换到home目录,将文件下载在这里
    $ cd ~
    
    # 如果没有 wget 命令则先安装
    $ dnf install wget -y
    
    # 如果已经有了 wget 命令
    $ wget https://github.com/prometheus/prometheus/releases/download/v2.39.1/prometheus-2.39.1.linux-amd64.tar.gz
    

由于文章存在时效性,如果你要下最新的,请直接前往Prometheus 下载

  1. 你也可以直接下载 linux 版本

    链接:https://pan.baidu.com/s/1SIipv7igCfYoMWtmHJX8jA?pwd=iccg
    提取码:iccg

     通过 sftp 上传到 Centos。推荐使用 `mobaxterm`作为 SSH 客户端远程连接,非常方便。[mobaxterm官网](https://mobaxterm.mobatek.net/)
    

解压文件

$ tar -zxpvf prometheus-2.39.1.linux-amd64.tar.gz

# 复制 prometheus 和 promtool 到 /usr/local/bin 路径
$ cd prometheus-2.39.1.linux-amd64

$ cp prometheus  /usr/local/bin

$ cp promtool  /usr/local/bin

# 将 prometheus.yml 复制到 /etc/prometheus/ 路径
$ cp prometheus.yml /etc/prometheus/

开放 Prometheus 端口

$ firewall-cmd --add-port=9090/tcp --permanent

$ firewall-cmd --reload

创建 Prometheus 服务

创建服务文件,以便以服务方式运行 Prometheus

$ vi /etc/systemd/system/prometheus.service

将下面的内容拷贝进去

[Unit]

Description=Prometheus Time Series Collection and Processing Server

Wants=network-online.target

After=network-online.target

 
[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries


[Install]

WantedBy=multi-user.target

{% note warning flat %}
注意检查一下开头和结束的内容,拷贝可能会造成内容丢失。
{% endnote %}

启动服务

# 重新加载
$ systemctl daemon-reload

# 启动 Prometheus 服务
$ systemctl start prometheus

# 设置 Prometheus 开机启动
$ systemctl enable prometheus

# 验证 Prometheu s正在运行
$ systemctl status prometheus

# 查看端口 9090
netstat -tunlp |grep 9090

在浏览器打开 http://server-ip:9090, server-ip 改为你 centos 的 ip。你将会看到如下界面。

安装 Node Exporter

node exoorter 用于收集 linux 系统的 CPU、内存、网络等信息。

创建用户

$ useradd -m -s /bin/false node_exporter

下载解压

  1. 下载
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz

{% note warning flat %}

由于文章存在时效性,需要最新版本,请前往node exoorter 下载

{% endnote %}

  1. 解压并复制到 /usr/local/bin
$ tar -zxpvf node_exporter-1.4.0.linux-amd64.tar.gz

# 复制到 /usr/local/bin
$ cp node_exporter-1.4.0.linux-amd64/node_exporter /usr/local/bin

# 修改目录权限
$ chown node_exporter:node_exporter /usr/local/bin/node_exporter

以服务方式运行

创建 service 配置文件

$ vi /etc/systemd/system/node_exporter.service

复制以下内容

[Unit]

Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
 

[Service]

User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

 
[Install]

WantedBy=multi-user.target

{% note warning flat %}
注意检查一下开头和结束的内容,拷贝可能会造成内容丢失。
{% endnote %}

启动服务

# 重新加载
$ systemctl daemon-reload

# 启动 Prometheus 服务
$ systemctl start node_exporter

# 设置 Prometheus 开机启动
$ systemctl enable node_exporter

# 验证 Prometheu s正在运行
$ systemctl status node_exporter

# 查看端口 9090
$ netstat -tunlp |grep 9100

开放 9100 端口

$ firewall-cmd --add-port=9100/tcp  --permanent
$ firewall-cmd --reload

修改 Prometheus 配置,加入 node_exporter

$ vi /etc/prometheus/prometheus.yml

添加如下内容

- job_name: 'node_exporter'

   static_configs:

   - targets: ['localhost:9100']

完整的 prometheus.yml

# 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: 15s  # scrape_timeout is set to the global default (10s).

# 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: 'node_exporter'

    static_configs:

    - targets: ['localhost:9100']

重启 Prometheus 服务

$ systemctl restart prometheus

在浏览器打开http://server-ip:9100/metrics,会看到如下界面:

安装 Grafana

下载安装

进入 Grafana 的 [下载页面](Download Grafana | Grafana Labs)

选择你的服务器版本:

这里我们选择 CentOS 的命令:

$ wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.2.2-1.x86_64.rpm
$ sudo yum install grafana-enterprise-9.2.2-1.x86_64.rpm

启动 Grafana 服务

# 启动 Grafana 服务
$ systemctl start grafana-server

# 设为开机启动
$ systemctl enable grafana-server

开放3000端口

$ firewall-cmd --add-port=3000/tcp  --permanent
$ firewall-cmd --reload

访问地址 访问地址:http://server-ip:3000

默认的账号密码是 admin/admin

配置 Prometheus

进入 Grafana 后,添加数据源:

<img src="https://teststation.oss-cn-chengdu.aliyuncs.com/blog/image-20221030205020232.png" alt="配置Prometheus" />

配置显示模板

最终就能看到配置后的结果。

后续进入的话,可以从菜单>Dashboards进入:

附录:问题

如何修改 Grafana 的默认端口

如果你已经部署了其他程序,占用了3000端口,那么你可能需要修改一下 Grafana的 默认端口。

  1. 修改 Grafana 的配置文件
$ vi /etc/grafana/grafana.ini
  1. 保存退出后,重新启动 Grafana 服务:
$ systemctl start grafana-server

Prometheus没有数据

按照步骤安装之后,发现 Prometheus 没有数据,大概率是服务器时间设置问题。

查看服务器上的日期:

$ timedatectl

<img src="https://teststation.oss-cn-chengdu.aliyuncs.com/blog/image-20221030211857035.png" alt="服务器时间" style="zoom: 67%;" />

如果服务器时间与你手机的时间不一致,则需要同步一下时间:

# ntp 同步时间
$ ntpdate ntp1.aliyun.com

# 如果 ntpdate 命令不存在,需要安装一下
$ yum install ntp

查看 Grafana 中 Prometheus 配置的时间:

如果你已经是 Asia/Shanghai就不需要修改。确保 Prometheus 的时区与服务器的时区一致,且时间是正确的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章