Prometheus服务环境搭建

  1. 首先我们需要准备3台服务器:
    初始化服务器
    IP地址、hostName主机名称、绑定Hosts文件、时间同步(非常重要)

服务器名

IP

主机名称

描述

promrtheus服务器

192.168.31.10

promrtheus.jus.cn

被监控(agent)服务器

192.168.31.11

agent.jus.cn

LB、Web01/Web02、MySql01/MySql02…

grafana服务器

192.168.31.12

grafana.jus.cn

运维成像==>数据转换为图形

  1. IP地址设置(针对虚拟机,实体机忽略):
BOOTPROTO=static/none
ipaddr=192.168.31.10
ipaddr=192.168.31.11
ipaddr=192.168.31.12
修改UUID编号的后三位
  1. HOSTNAME设置(可不设置):
hostnamectl set-hostname prometheus.jus.cn
hostnamectl set-hostname agent.jus.cn
hostnamectl set-hostname grafana.jus.cn
  1. /etc/hosts文件(可不设置):
三台都互相绑定IP与主机名
# vim /etc/hosts
192.168.31.10 promrtheus promrtheus.jus.cn
192.168.31.11 agent agent.jus.cn
192.168.31.12 grafana grafana.jus.cn
  1. 时间同步
# mount /dev/sr0 /mnt
# yum install ntpdate -y
# ntpdate cn.ntp.org.cn
  1. 关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
# iptables -F

prometheus安装步骤

  1. 上传prometheus-2.15.2.linux-amd64.tar.gz压缩包到linux系统
  2. 解压并安装软件
解压:
# tar xf prometheus-2.15.2.linux-amd64.tar.gz -C /usr/local/
重命名:
# mv /usr/local/prometheus-2.15.2.linux-amd64 /usr/local/prometheus
  1. 启动prometheus软件
# cd /usr/local/prometheus
# ./prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
# 注:&符号为连接符,代表后台运行,不占用终端窗口
  1. 测试端口占用情况(判断是否真正启动了)
确认端口(9090)
# lsof -i:9090 或者 ss -naltp |grep 9090
注:提示 lsof: 未找到命令,使用yum来安装lsof,命令如下:
# yum install lsof
  1. prometheus软件界面(web)
    通过浏览器访问 http://192.168.31.10:9090/ 就可以访问到prometheus的主界面

    由上图可知,系统默认监控了自己的主机信息,通过http://192.168.31.10:9090/metrics 查看到监控的数据

使用Prometheus监控远程Linux主机

在另一台远程linux主机(被监控端agent)上安装node_exporter组件(这样prometheus就可以接收到其收集系统)
node_exporter下载地址: https://promrtheus.io.doenload/

安装node_exporter
  1. 上传node_exporter-0.18.1.linux-amd64.tar.gz压缩包到linux服务器中(agent被监控端)
  2. 解压安装node_exporter
解压:
# tar xf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/
重命名:
# mv /usr/local/node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter
启动node_exporter
里面就一个启动命令node_exporter,可以直接使用此命令启动
# nohup /usr/local/node_exporter/node_exporter &
扩展:nohup命令:如果把启动node_exporter的终端关闭,那么进程也会随之关闭,nohup命令会解决这个问题

测试端口9100占用情况(判断是否真正启动了)
# lsof -i:9100 或者 ss -naltp |grep 9100

通过浏览器访问 http://192.168.31.11:9100/metrics 就可以查看到node_exporter在被监控端收集的监控信息

Prometheus监控拉取node节点信息

回到Prometheus所在服务器,在Prometheus配置文件配置文件里添加被监控机器的配置段

编辑Prometheus配置文件
# vim /usr/local/prometheus/prometheus.yml
添加如下内容
- job_name: 'agent'  #取一个job名称来代表被监控的机器
    static_configs:
    - targets: ['192.168.31.11:9100']  #这里改成被监控机器的IP,后面端口接9100

改完后再重启Prometheus服务
# pkill prometheus  #杀死Prometheus进程
# lsof -i:9090  #确认端口没有进程占用
# cd /usr/local/prometheus
# ./prometheus --config.file="/usr/local/prometheus/prometheus.yml" &  #重启prometheus服务
# lsof -i:9090  #确认端口,被占用说明重启成功

回到Prometheus的web管理页面(http://192.168.31.10:9090/),点Status–>Targets可以看到多了一台监控目标

prometheus 添加job prometheus自动添加主机_prometheus 添加job