项目实战 Prometheus环境搭建
阅读原文时间:2023年07月08日阅读:1

项目摘要:

本文是搭建一套prometheus环境的教程。

前期准备:准备三台虚拟机,本文以centos7为例。

项目具体实施:
分别进入每台虚拟机设置hostname:
# hostnamectl set-hostname prometheus.demo

# hostnamectl set-hostname agent.demo

# hostnamectl set-hostname grafana.demo

在每台虚拟机的/etc/hosts文件中加入如下内容(ip改成真实的ip地址):
ip agent.demo

ip grafana.demo

ip prometheus.demo

每台虚拟机同步时间(前两条命令不一定需要):
# mount /dev/sr0/mnt

# yum install ntpdate -y

# ntpdate -u cn.pool.ntp.org

每台禁用SELINUX
# vim /etc/selinux/config

将SELINUX=enforcing 修改为:SELINUX=disabled

每台关闭防火墙
# systemctl stop firewalld

# systemctl disable firewalld

# iptables -F

安装prometheus(登录到prometheus主机)
下载后,解压就能用,不需要编译。

下载:
# wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz

解压、移动/重命名:
# tar xf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local
# mv /usr/local/prometheus-2.5.0.linux-amd64/ /usr/local/prometheus

# mkdir /etc/prometheus

# cp /usr/local/prometheus/prometheus.yml /etc/prometheus/

启动:
# /usr/local/prometheus/prometheus --config.file=" /etc/prometheus/

/prometheus.yml" &

解释:&:表示后台运行

默认端口是9090

查看端口占用情况:

# lsof -i:9090 或者 ss -naltp | grep 9090

此时,浏览器输入http://ip:9090即可访问到prometheus的主界面:

安装node_exporter监控远程Linux主机(登录到agent机器)
下载:
# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz

解压:# tar xf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
重命名: # mv /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter
启动:# nohup /usr/local/node_exporter/node_exporter &
查看启动情况:# lsof -i:9100
打开浏览器输入:http://ip:9100/metrics即可看到监控信息。
将该监控添加到prometheus监控软件,登录到prometheus机器。
修改prometheus.yml

# vim /etc/prometheus/prometheus.yml

末尾添加如下内容:

- job_name: 'agent'

static_configs:

- targets: ['ip:9100']

# ps -aux | grep prome

# kill -HUP 进程号

去控制台查看。(选择statusàtargets查看)

在agent机器添加一个mysql监控
下载:
# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz

解压:tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/重命名:# mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/ /usr/local/mysqld_exporter
安装mysql数据库:
参考:https://www.jianshu.com/p/fe476f511485

创建一个mysql配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)

# vim /usr/local/mysqld_exporter/.my.cnf

[client]

user=用户名

password=密码

# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &

确认端口(9104)

# lsof -i:9104

浏览器输入:http://ip:9104/metrics即可看到监控信息。

将该监控添加到prometheus监控软件,登录到prometheus机器。

编辑如下内容:

# vim /etc/prometheus/prometheus.yml

末尾添加如下内容:

- job_name: 'agent_mysql'

static_configs:

- targets: ['ip:9104']

# kill -HUP 进程号

再次去浏览器页面观察是否添加成功。

安装grafana
下载:# wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm
安装:# rpm -ivh grafana-5.3.4-1.x86_64.rpm
启动:
# systemctl start grafana-server

# systemctl enable grafana-server

检查端口: # lsof -i:3000
打开浏览器输入:http://ip:3000查看是否成功运行。
至此,环境搭建完成。