Prometheus & Grafana 的安装

350 阅读2分钟

一、 安装 Go 语言环境

1.1 下载

常用的下载地址有:

  1. studygolang.com/dl
  2. golang.google.cn/dl/

本次安装使用当前最新版:go1.19.3.linux-amd64.tar.gz

使用命令如下:

wget https://studygolang.com/dl/golang/go1.19.3.linux-amd64.tar.gz

1.2 解压

tar -xvf go1.19.3.linux-amd64.tar.gz

1.3 配置环境

编辑 profile 文件

vim /etc/profile

在文件底部追加以下配置:(这里我的 go 的解压地址是:/usr/software/prometheus

export GOROOT=/usr/software/prometheus/go
export PATH=$PATH:$GOROOT/bin

随后保存文件,并使用 source /etc/profile 命令让配置生效。

1.4 验证

执行命令 go -version

控制台有提示信息即可。

二、安装 Prometheus

2.1 下载

在GitHub上下载(下载巨慢,建议用清华镜像):

github.com/prometheus/…

清华大学开源软件镜像:

mirrors.tuna.tsinghua.edu.cn/

本次安装使用当前最新版:

使用命令如下:

wget https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/LatestRelease/prometheus-2.40.1.linux-amd64.tar.gz

2.2 解压&重命名

tar -xvf prometheus-2.40.1.linux-amd64.tar.gz
mv prometheus-2.40.1.linux-amd64 prometheus

解压后,重命名为 prometheus

最终效果如下图:

在这里插入图片描述

2.3 启动

当前的安装目录为:/usr/software/prometheus/prometheus

  1. 非后台启动:
/usr/software/prometheus/prometheus/prometheus --config.file=/usr/software/prometheus/prometheus/prometheus.yml

该种方式,可以使用 ctrl + c 来停止;或者关闭当前连接。

  1. 后台启动:
/usr/software/prometheus/prometheus/prometheus --config.file=/usr/software/prometheus/prometheus/prometheus.yml &

2.4 页面访问

http://localhost:9090/

三、 安装常用的监控 exporter

当前官网提供的有:

prometheus.io/docs/instru…

3.1 监控 Linux (node_exporter)

3.1.1 下载-解压-启动

在官网下载:

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

解压并启动:

# 解压
tar -zxvf node_exporter-1.4.0.linux-amd64.tar.gz
# 后台启动
./node_exporter &

3.1.2 配置指标&监控

配置 prometheus.yml,增加以下 job_name(node_exporter默认端口为9100)

  # linux
  - job_name: "linux"
    static_configs:
      - targets: ["localhost:9100"]
        labels:
          instance: node1

3.1.3 测试

重新启动 prometheus 。随后访问 9090 端口,在页面上查询指标 node_memory_MemTotal_bytes

该指标是本机器的内存大小,单位是字节。当前是 7.8g大小。

而且该页面上能选择使用 Table 、Graph 来展示指标的值。

在这里插入图片描述

3.2 监控 MySql(mysqld_exporter)

3.2.1 下载-解压-启动

在官网下载:

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

解压:

tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz

在解压目录增加 ,my,cnf 文件,并配置如下内容:

[client] 
user=mysqld_exporter 
password=mysqld_exporter_password 

以上配置,需要是你的数据库的用户名、密码。

启动:

nohup /usr/software/prometheus/mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter --config.my-cnf="/usr/software/prometheus/mysqld_exporter-0.14.0.linux-amd64/.my.cnf" &

3.2.2 配置指标&监控

配置 prometheus.yml,增加以下 job_name(mysqld_exporter默认端口为9104)

  # mysql
  - job_name: "mysql"
    static_configs:
      - targets: ["localhost:9104"]
        labels:
          instance: mysql

在这里插入图片描述

3.2.3 测试

重新启动 prometheus 。随后访问 9090 端口,在页面上查询指标 mysql_global_variables_max_connections

该指标是mysql的最大连接数。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bQ3nZvBc-1668186019569)(img\20221111194729.png)]

四、安装 Grafana

4.1 下载

从清华的镜像中下载: mirrors.tuna.tsinghua.edu.cn/grafana/yum…

wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-9.2.3-1.x86_64.rpm

4.2 安装

yum install grafana-9.2.3-1.x86_64.rpm

4.3 设置为服务&启动

/sbin/chkconfig --add grafana-server
# 启动
service grafana-server start

4.4 测试

启动Grafana 后,默认的端口是 3000

初始的用户名、密码是 admin/admin

访问连接:http://localhost:3000/dashboards

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eFzHbZMr-1668186019570)(img\20221112005238.png)]

官方网站提供了一些模板界面,可以从以下位置:

grafana.com/grafana/das…

搜索自己想要的模板,进行导入。

比如选择这个节点监控的模板,它的ID是 1860。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CNqt78XE-1668186019570)(img\20221112005524.png)]

随后回到自己的监控界面,进行导入。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZRh3V10t-1668186019570)(img\20221112005652.png)]

随后就能看到最终效果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2YWAHc4v-1668186019570)(img\20221112005814.png)]