centos7安装prometheus

一.环境准备

服务器ip
Prometneus服务器192.168.159.160
被监控服务器192.168.159.161
grafana服务器192.168.159.162

1. 静态ip(要求能上外网)

2. 主机名
各自配置好主机名 
# hostnamectl set-hostname master
# hostnamectl set-hostname node1
# hostnamectl set-hostname node2
三台都互相绑定IP与主机名 
# vim /etc/hosts            
192.168.159.160  master
192.168.159.161 node1
192.168.159.162  node2

echo "192.168.159.160 master
192.168.159.161  node1
192.168.159.162  node2">>/etc/hosts

3. 时间同步(时间同步一定要确认一下)
[root@master ~]# yum install -y  ntpdate && ntpdate time.windows.com

4. 关闭防火墙,selinux
# systemctl stop firewalld 
# systemctl disable firewalld 
# iptables -F

二.安装prometheus 

1.从 https://prometheus.io/download/ 下载相应版本,安装到服务器上
官网提供的是二进制版,解压就能用,不需要编译,下面是安装包下载地址

链接:https://pan.baidu.com/s/1A1e1UDMU5cMt6eKfq3IsEg
提取码:r435

上传prometheus-2.5.0.linux-amd64.tar.gz

root@master ~]# ls
prometheus-2.5.0.linux-amd64.tar.gz

[root@master ~]# tar -zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# mv /usr/local/prometheus-2.5.0.linux-amd64/  /usr/local/prometheus
[root@master local]# cd prometheus/
[root@master prometheus]# ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

直接配置文件启动

/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

[root@master ~]# netstat -lntp | grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      19328/prometheus    

2.prometheus界面

通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面

默认只监控了本机一台,点Status --》点Targets --》可以看到只监控了本机

3.主机数据显示

通过http://服务器IP:9090/metrics可以查看到监控的数据

4.在web主界面可以通过关键字查询监控项

三.远程监控node1节点

① 在远程linux主机(被监控端agent1)上安装node_exporter组件
下载地址: https://prometheus.io/download/

上传node_exporter-1.8.2.linux-amd64.tar.gz

[root@node1 ~]# tar -zxvf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
node_exporter-0.16.0.linux-amd64/
node_exporter-0.16.0.linux-amd64/LICENSE
node_exporter-0.16.0.linux-amd64/node_exporter
node_exporter-0.16.0.linux-amd64/NOTICE
[root@node1 ~]# mv /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter

[root@node1 ~]# cd /usr/local/node_exporter

里面就一个启动命令node_exporter,可以直接使用此命令启动

如果把启动node_exporter的终端给关闭,那么进程也会随之关闭。nohup命令会帮你解决这个问题

nohup ./node_exporter &

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

http://192.168.10.65:9100/metrics

③ 回到prometheus服务器的配置文件里添加被监控机器的配置段  在主配置文件最后加上下面三行

[root@master prometheus]# vim /usr/local/prometheus/prometheus.yml
  - job_name: 'node1'     # 取一个job名称来代 表被监控的机器  
    static_configs:       
    - targets: ['192.168.159.161:9100']     # 这里改成被监控机器 的IP,后面端口接9100

改完配置文件后,重启服务

[root@master prometheus]# ps -ef | grep prometheus
root     20436 17886  0 16:00 pts/0    00:00:01 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
root     23037 17886  0 16:33 pts/0    00:00:00 grep --color=auto prometheus
[root@master prometheus]# kill -9 20436
[root@master prometheus]# ps -ef | grep prometheus
root     23069 17886  0 16:34 pts/0    00:00:00 grep --color=auto prometheus

#重启prometheus
[root@master prometheus]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@master prometheus]# netstat -lntp |grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      23106/prometheus    

④ 回到web管理界面 --》点Status --》点Targets --》可以看到多了一台监 控目标

四.远程监控node2 mysql

① 在被管理机node2上安装mysqld_exporter组件

下载地址: https://prometheus.io/download/

上传mysqld_exporter-0.15.1.linux-amd64.tar.gz

[root@node2 ~]# tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/
[root@node2 ~]# mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/ /usr/local/mysqld_exporter
[root@node2 ~]# ls /usr/local/mysqld_exporter/
LICENSE  mysqld_exporter  NOTICE

#安装mariadb数据库,并授权
[root@node2 mysqld_exporter]#  yum install mariadb\* -y
[root@node2 mysqld_exporter]# systemctl start mariadb
[root@node2 mysqld_exporter]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@node2 mysqld_exporter]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123';

(注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb。所以这个localhost是指的mysql_exporter的IP)
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit


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

[root@node2 mysqld_exporter]# vim /usr/local/mysqld_exporter/.my.cnf
[client] 
user=mysql_monitor 
password=123

#启动mysqld_exporter
[root@node2 mysqld_exporter]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &

#查看启动端口(9104)

② 回到prometheus服务器的配置文件里添加被监控的mariadb的配置段

[root@master ~]# vim /usr/local/prometheus/prometheus.yml
  - job_name: 'node2-mysql'
    static_configs:
    - targets: ['192.168.159.162:9104']
    
    
#改完配置文件后,重启服务
[root@master ~]# pkill prometheus
[root@master prometheus]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
#查看端口(9090)

③ 回到web管理界面 --》点Status --》点Targets --》可以看到监控mariadb了

mysql_global_status_threads_connected

五.Grafana可视化图形工具

yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.3-1.x86_64.rpm

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值