title: Prometheus+node_exporter+mysqld_exporter+grafana部署与配置 #标题tags: Prometheus #标签
date: 2020-08-26
categories: 监控 # 分类
记录下Prometheus+node_exporter+mysqld_exporter+grafana协同工作的过程。
安装Prometheus
$ wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
$ tar zxf prometheus-2.20.1.linux-amd64.tar.gz -C /opt/
$ ln -sf /opt/prometheus-2.20.1.linux-amd64 /opt/prometheus
$ ln -sf /opt/prometheus-2.20.1.linux-amd64/prometheus /usr/local/bin/
# 配置文件调整
$ sed -i 's/localhost/192.168.20.2/g' /opt/prometheus/prometheus.yml
# 启动Prometheus
prometheus --config.file="/opt/prometheus/prometheus.yml" \
--web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data \
--storage.tsdb.retention.time=30d &
# --config.file: 指定配置文件
# --web.enable-lifecycle: 支持通过http请求重载配置
# --storage.tsdb.path: 指定数据存储目录(默认当前目录的的data目录,若不存在则新建)
# --storage.tsdb.retention.time: 指定数据保留时间(默认15d)
# 确定端口在监听
$ ss -lnput | grep 9090 # 确定端口在监听
tcp LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=16429,fd=11))
浏览器访问
访问Prometheus主机的 9090 端口,点击如下,即可看到监控主机的相关信息(Prometheus默认监控本机):
Prometheus相关操作
Prometheus的启停操作
# 启动Prometheus
$ prometheus --config.file="/opt/prometheus/prometheus.yml" --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention.time=30d & # 启动Prometheus
# --web.enable-lifecycle 表示允许通过web接口重载配置文件
# --storage.tsdb.path=/opt/prometheus/data 指定数据存储目录,默认是当前目录的data目录
# --storage.tsdb.retention.time 指定数据保留时间,默认为15d。
$ pkill prometheus # 停止Prometheus
############ 重载Prometheus的两种方式 ############
# 命令行中给发送hup信号至Prometheus进程
$ kill -HUP $(ps -ef | grep prometheus | grep -v grep | awk '{print $2}')
# 通过web接口重载,启动时需增加选项 --web.enable-lifecycle
$ curl -XPOST http://192.168.20.10:9090/-/reload
# 如果配置文件有误,重启时将会看到类似以下的错误信息
ERRO[0161] Error reloading config: couldn't load configuration (-config.file=prometheus.yml): unknown fields in scrape_config: job_nae source=main.go:146
Prometheus管理接口
健康检查
$ curl http://192.168.20.10:9090/-/healthy
准备检查
$ curl http://192.168.20.10:9090/-/ready
Prometheus is Ready.
重载配置文件
$ curl -XPOST http://192.168.20.10:9090/-/reload
# 需启动时增加 --web.enable-lifecycle 选项。
停止Prometheus
$ curl -XPUT http://192.168.20.10:9090/-/quit
# 或者
$ curl -XPOST http://192.168.20.10:9090/-/quit
# 同样需启动时增加 --web.enable-lifecycle 选项。
安装配置grafana
下载及安装
$ wget https://dl.grafana.com/oss/release/grafana-7.1.4-1.x86_64.rpm
$ yum -y install grafana-7.1.4-1.x86_64.rpm
$ systemctl daemon-reload
$ systemctl enable grafana-server && systemctl start grafana-server
grafana添加Prometheus数据源
访问grafana的3000端口进行登录,默认用户名和密码为: admin/admin。
根据提示修改默认密码
添加数据源
指定Prometheus详细信息
点击测试并保存后,显示如下:
至此,数据源添加成功。
安装node_exporter
node_exporter用于收集主机运行信息,比如CPU、内存、磁盘等资源使用情况。
各种监控exporter,可以去官网下载
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
$ tar zxf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/
$ ln -sf /opt/node_exporter-1.0.1.linux-amd64 /opt/node_exporter
$ /opt/node_exporter/node_exporter &
$ ss -lnput | grep 9100 # 确定端口在监听
tcp LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=7352,fd=3))
安装mysqld_exporter
mysqld_exporter 用于采集mysql相关信息,比如buffer缓冲区大小、运行时长、数据库的QPS、当前连接数等非常详细的信息。
# 安装mysql用于测试
$ yum -y install mariadb*
$ systemctl start mariadb
$ mysql -uroot # 登录到数据库授权监控用户
MariaDB [(none)]> grant select,replication client,process on *.* to 'mysql_monitor'@'192.168.20.3' identified by '123.com';
MariaDB [(none)]> flush privileges;
$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
$ tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt/
$ ln -sf /opt/mysqld_exporter-0.12.1.linux-amd64 /opt/mysqld_exporter
# 写入连接mysql账号信息
$ cat > /opt/mysqld_exporter/.my.cnf << EOF
[client]
host=192.168.20.3
user=mysql_monitor
password=123.com
# socket=/tmp/mysql.sock
EOF
$ /opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/.my.cnf &
$ ss -lnput | grep 9104
tcp LISTEN 0 128 [::]:9104 [::]:* users:(("mysqld_exporter",pid=10201,fd=3))
调整Prometheus获取主机及应用监控数据
$ vim /opt/prometheus/prometheus.yml
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['192.168.20.2:9090']
# 增加以下配置,其中job_name为自定义
- job_name: 'agent1'
static_configs:
- targets: ['192.168.20.3:9100']
- job_name: 'mysql1'
static_configs:
- targets: ['192.168.20.3:9104']
# 重载Prometheus配置文件
$ curl -XPOST http://192.168.20.2:9090/-/reload
确定Prometheus已采集到数据
使用grafana添加图形
添加主机资源使用情况的图形
导入mysqld图形模板
以下操作在grafana所在主机进行操作。
$ git clone https://github.com/percona/grafana-dashboards.git
$ cp -r grafana-dashboards/dashboards/ /var/lib/grafana/
$ cat >> /etc/grafana/grafana.ini << EOF
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
EOF
$ systemctl restart grafana-server # 重启grafana
下载图形模板
访问 https://github.com/percona/grafana-dashboards,下载zip包,如下:
在grafana web界面中导入json文件
将下载后的zip包解压后,找到grafana-dashboards-master\grafana-dashboards-master\dashboards
目录,如下:
granfana 导入所需json文件:
哦吼,报错了,原因是此仪表盘默认寻找的数据源是Prometheus,这也就是我添加数据源时说的那个坑(我添加数据源是指定的数据源名称是Prometheus_data)。
修改数据源名称
修改数据源名称为“Prometheus”,然后点击下方的保存即可。
查看MySQL数据库运行情况如下