主机名 | IP地址 |
---|---|
prometheus | 192.168.8.100(vmnet8网络模式) |
node1 | 192.168.8.101(vment8网络模式) |
重新安装一台CentOS7.9图形版虚拟机作为模板机、
关闭防火墙和SELinux
模板机机需要使用 centos7.9的镜像创建
英文安装,时区选择亚洲上海,时间和自己真机保持一致
由于装机大家已经很熟练了,这里不再做展示,如果安装出现问题,可参阅之前笔记
下方罗列本次装机需要注意的事项
[root@localhost ~]# systemctl stop firewalld #停止防火墙
[root@localhost ~]# systemctl disable firewalld #将防火墙设置为开机不自启
[root@localhost ~]# vim /etc/selinux/config
...
7 SELinux=disabled #将SELinux状态改为disabled禁用
...
[root@localhost ~]# poweroff #关闭虚拟机
-O
:表示将数据另存为制定路径模板机操作
[root@localhost ~]# rm -rf /etc/yum.repos.d/*.repo #删除自带的repo文件
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo \ https://mirrors.aliyun.com/repo/Centos-7.repo #下载阿里镜像源
使用模板机链接克隆prometheus、node1
prometheus主机操作(修改主机名及配置IP地址,网卡名不要照抄)
[root@localhost ~]# hostnamectl set-hostname prometheus
[root@prometheus ~]# nmcli connection modify ens33 ipv4.method auto connection.autoconnect yes
[root@prometheus ~]# nmcli connection up ens33
node1主机操作(修改主机名及配置IP地址,网卡名不要照抄)
[root@localhost ~]# hostnamectl set-hostname node1
[root@node1 ~]# nmcli connection modify ens33 ipv4.method auto connection.autoconnect yes
[root@node1 ~]# nmcli connection up ens33
两台主机使用MobaXterm远程连接
将 prometheus_soft.tar.gz 压缩包拷贝至 promethues主机的/root 家目录下
[root@prometheus ~]# tar -xf prometheus_soft.tar.gz
[root@prometheus ~]# cd prometheus_soft/
]# tar -xf prometheus-2.17.2.linux-386.tar.gz
]# mv prometheus-2.17.2.linux-386 /usr/local/prometheus
修改配置文件,让 promethues 自己监控自己
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
....
static_configs:
- targets: ['192.168.8.100:9090'] #修改 IP,指定自己监控自己
检查配置文件是否修改正确(\表示一行写不下,折行写)
[root@prometheus ~]# /usr/local/prometheus/promtool check config \ /usr/local/prometheus/prometheus.yml
SUCCESS: 0 rule files found
默认prometheus启动服务是很复杂的,如果想利用systemd更快速更方便的管理prometheus
服务,就需要编写服务文件,让 systemd 管理
[root@prometheus ~]# vim /usr/lib/systemd/system/prometheus.service #编辑服务配置文件
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data
[Install]
WantedBy=multi-user.target
[root@prometheus ~]# systemctl enable prometheus.service --now #设置为开启自启并立即启动服务
[root@prometheus ~]# systemctl status prometheus.service
[root@prometheus ~]# ss -ntulp | grep 9090 #Prometheus服务监听9090端口
tcp LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=10885,fd=8))
[root@prometheus ~]# firefox http://192.168.8.100:9090
部署被监控端(node1)
[root@node1 ~]# tar -xf prometheus_soft.tar.gz
[root@node1 ~]# cd prometheus_soft/
[root@node1 prometheus_soft]# tar -xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
]# mv node_exporter-1.0.0-rc.0.linux-amd64 /usr/local/node_exporter
[root@node1 prometheus_soft]# ls /usr/local/node_exporter/
LICENSE node_exporter NOTICE
如果想更好更快的管理 node_exporter 导出器服务,需要编写 service 服务文件,让 systemd
进行管理
[root@node1 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl enable node_exporter --now
[root@node1 ~]# systemctl status node_exporter
[root@node1 ~]# ss -utnlp | grep node_exporter
tcp LISTEN 0 128 :::9100 :::*
users:(("node_exporter",pid=11222,fd=3))
prometheus服务端修改监控端服务器配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
......
- job_name: 'node1' #定义监控任务名字,可以任意名称
static_configs:
- targets: ['192.168.8.101:9100'] #被监控端主机和端口
[root@prometheus ~]# systemctl restart prometheus #重启服务,再查看web页面信息
[root@prometheus ~]# cd prometheus_soft/
[root@prometheus prometheus_soft]# ls
.....
grafana-6.7.3-1.x86_64.rpm
[root@prometheus prometheus_soft]# yum -y install grafana-6.7.3-1.x86_64.rpm
[root@prometheus ~]# systemctl enable grafana-server --now
[root@prometheus ~]# systemctl status grafana-server
[root@prometheus ~]# ss -utnlp | grep grafana-server
[root@prometheus ~]# vim /etc/grafana/grafana.ini
304 [auth.anonymous]
306 enabled = true #启用匿名访问
312 org_role = Admin #以管理员的身份登录
[root@prometheus ~]# systemctl restart grafana-server #重启服务
选择Prometheus作为数据源
给数据源进行命名,并设置为默认数据源,然后设置数据源来源地址,最后保存配置
保存成功之后,可以点击齿轮图标,选择 data sources,查看数据源,这样就有了数据的来源:
将 node1 主机搭建成 Mariadb 数据服务器,配置账户和密码
[root@node1 ~]# yum -y install mariadb mariadb-server
[root@node1 ~]# systemctl enable mariadb --now #设置为开启自启,并立即启动服务
[root@node1 ~]# mysql
MariaDB [(none)]> GRANT ALL ON *.* TO tom@'127.0.0.1' IDENTIFIED BY '123';
MariaDB [(none)]> EXIT;
[root@node1 ~]# cd prometheus_soft/
[root@node1 prometheus_soft]# tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
]# mv mysqld_exporter-0.12.1.linux-amd64 /usr/local/mysqld_exporter
[root@node1 prometheus_soft]# ls /usr/local/mysqld_exporter/
LICENSE mysqld_exporter NOTICE
[root@node1 prometheus_soft]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
host=127.0.0.1
port=3306
user=tom
password=123
[root@node1 ~]# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl enable mysqld_exporter --now #设置开机自启,并立即启动服务
[root@node1 ~]# systemctl status mysqld_exporter
[root@node1 ~]# ss -nutlp | grep mysqld_exporter #默认监听9104
tcp LISTEN 0 128 :::9104 :::*
users:(("mysqld_exporter",pid=11807,fd=3))
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
......
- job_name: 'node1_mysql'
static_configs:
- targets: ['192.168.4.11:9104'] #指定 node1 导出器服务端口和地址
[root@prometheus ~]# systemctl restart prometheus #重启服务让配置生效