这节课我们来学习二进制安装prometheus,然后使用Node Exporter来采集主机信息,由prometheus server来定时向node exporter拉取数据并使用Grafana进行图形化的展示。
备注:本次实验设计软件见下百度云链接;
链接:https://pan.baidu.com/s/1YIlCx4V1mwdFYOshh38u3Q
提取码:g44c
1、安装prometheus server
Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖。用户只需要下载对应平台的二进制包,解压并且添加基本的配置即可正常使用Prometheus Server。
1.1、下载安装包
prometheus的官网https://prometheus.io/
安装包下载链接https://prometheus.io/download/
截止目前(2020年11月30日)最新的二进制包版本是[prometheus-2.23.0.linux-amd64.tar.gz],链接如下:
https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
1.2、解压
#本次次prometheus server主机名设置为:prometheus,其余2个target节点主机名如下:
hostnamectl --static set-hostname prometheus
exec bash
hostnamectl --static set-hostname node1
exec bash
hostnamectl --static set-hostname node2
exec bash
另建议:关闭防火墙/NetwokManager/selinux
systemctl stop firewalld#如果故关闭防火墙的话,其9090端口将无法访问;
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
setenforce 0
sed -i s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config
[root@prometheus ~]# mv prometheus-2.23.0.linux-amd64.tar.gz /usr/local/
[root@prometheus ~]# cd /usr/local/
[root@prometheus local]# tar xf prometheus-2.23.0.linux-amd64.tar.gz
[root@prometheus local]# ls
bin games lib libexec prometheus-2.22.0.linux-amd64.tar.gz share
etc include lib64 prometheus-2.22.0.linux-amd64 sbin src
[root@prometheus local]# mv prometheus-2.23.0.linux-amd64 prometheus
1.3、配置说明
解压后在prometheus的安装目录/usr/local/prometheus里面包含默认地prometheus.yml配置文件。
1.4、配置prometheus
[root@prometheus local]#cat /usr/local/prometheus/prometheus.yml
1.4.1、创建prometheus的用户
为了安全,尽量使用普通用户来启动prometheus服务;
[root@prometheus prometheus]# useradd -s /sbin/nologin -M prometheus
1.4.2、更改数据存储目录
prometheus的数据默认会存放在应用所在目录下,这里我们修改为/data/prometheus
[root@prometheus prometheus]# mkdir /data/prometheus -p
[root@prometheus prometheus]# chown -R prometheus:prometheus /usr/local/prometheus
[root@prometheus prometheus]# chown -R prometheus:prometheus /data/prometheus/
1.5、创建systemd启动脚本
prometheus的启动很简单,只需要直接启动解压目录的二进制文件prometheus,然后再加上一些参数即可。
[root@prometheus prometheus]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure
User=prometheus
[Install]
WantedBy=multi-user.target
不要忘记执行
[root@prometheus prometheus]# systemctl daemon-reload
1.6、启动prometheus并设置为开机自启动
[root@prometheus prometheus]# systemctl start prometheus
[root@prometheus prometheus]# systemctl enable prometheus
或者
[root@prometheus prometheus]#systemctl enable --now prometheus
[root@prometheus prometheus]# systemctl status prometheus
[root@prometheus prometheus]# ss -tnl
1.7、访问prometheus
我们看到有9090端口,然后直接以IP:Port的方式来访问
http://10.10.10.138:9090
还可以查看到当前正在被监控的主机,这里只有我们当前部署的这台。如下所示:
点击Status—>Targets可以看到
从UP状态可以看的出来节点是正常的。
2、安装node exporter
node exporter的作用:收集操作系统的基本系统, 例如cpu, 内存, 硬盘空间等基本信息, 并对外提供api接口,用于prometheus查询存储。
2.1、下载安装包
node exporter最新的版本是1.0.1。下载链接:https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
2.2、解压
[root@node1 ~]# mv node_exporter-1.0.1.linux-amd64.tar.gz /usr/local
[root@node1 ~]# cd /usr/local
[root@node1 local]# tar xf node_exporter-1.0.1.linux-amd64.tar.gz
[root@node1 local]# mv node_exporter-1.0.1.linux-amd64 node_exporter
2.3、启动node exporter
node exporter无需修改配置文件,安装上去之后可以直接启动。
node exporter的二进制启动文件在/usr/local/node_exporter目录下面。这里我们直接以nohup的方式来启动这个服务。
[root@node1 local]# nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
[root@node1 local]# ss -tnl | grep 9100
LISTEN 0 128 [::]:9100 [::]😗
node exporter默认地端口是9100,只要能看到端口,说明服务启动是没有问题的。
#接下来按照相同的方法把第二个节点完成配置;
2.4、加入开机启动
node exporter直接写在Linux系统启动脚本/etc/rc.local文件里面即可。当然,最后赋予x执行权限
[root@prometheus local]# vim /etc/rc.local
#!/bin/bash
touch /var/lock/subsys/local #默认就存在的
nohup /usr/local/node_exporter/node_exporter > /dev/null 2>&1 &
[root@prometheus local]# chmod +x /etc/rc.local
//在相应的2个target节点上执行以上操作;
2.5、配置prometheus抓取数据
node exporter启动后暴露出来9100端口,是为了让prometheus server去抓取这个主机上面的日志。所以我们需要在prometheus中配置一下这个接口。
编辑prometheus.yml文件,增加后面的3行数据。
例如:
- job_name: 'node132'
static_configs:
- targets: ['192.168.50.132:9100']
#本次添加数据如下:
root@prometheus local]#vim /usr/local/prometheus/prometheus.yml
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'node1'
static_configs:
- targets: ['10.10.10.135:9100']
- job_name: 'node2'
static_configs:
- targets: ['10.10.10.139:9100']
然后重启prometheus,再次打开prometheus的web页面,即可看到现在所监控的主机。
systemctl restart prometheus
3、安装Grafana
grafana和prometheus我们都安装在一台机器上面。
3.1、下载安装包
grafana的官网:https://grafana.com/
最新版本grafana-7.2.1,下载链接https://dl.grafana.com/oss/release/grafana-7.2.1.linux-amd64.tar.gz
3.2、解压
这里我们依然安装到/usr/local目录下
[root@prometheus ~]# mv grafana-7.2.1.linux-amd64.tar.gz /usr/local
[root@prometheus ~]# cd /usr/local
[root@prometheus local]# tar xf grafana-7.2.1.linux-amd64.tar.gz
[root@prometheus local]# mv grafana-7.2.1 grafana
3.3、配置grafana
3.3.1、创建grafana用户
useradd -s /sbin/nologin -M grafana
3.3.2、创建数据存放目录
[root@prometheus local]# mkdir /data/grafana
[root@prometheus local]# chown -R grafana:grafana /usr/local/grafana
[root@prometheus local]# chown -R grafana:grafana /data/grafana/
3.4、修改配置文件
grafana的配置文件为/usr/local/grafana/conf/defaults.ini文件,配置为上面新建的数据目录。
配置文件里面参数比较多,我们大概修改下面4个参数即可,后期需要了再另改其他参数。
vim /usr/local/grafana/conf/defaults.ini
data = /data/grafana/data
logs = /data/grafana/log
plugins = /data/grafana/plugins
provisioning = /data/grafana/conf/provisioning
3.5、创建systemd启动脚本
grafana启动的时候指定一下启动的安装家目录。
[root@prometheus ~]# vim /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana
After=network.target
[Service]
User=grafana
Group=grafana
Type=notify
ExecStart=/usr/local/grafana/bin/grafana-server -homepath /usr/local/grafana
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
记得daemon-reload一下
[root@prometheus ~]# systemctl daemon-reload
3.6、启动grafana并设置为开机自启动
[root@prometheus ~]# systemctl enable --now grafana-server
3.7、访问grafana
grafana的默认端口是3000,访问的地址是IP:3000;
可以通过ss -tnlp命令查看服务是否有正常启动;
我这里访问ip为:http://10.10.10.138:3000
默认地账号密码是admin/admin,登录之后会强制修改密码
4、grafana连接prometheus
接下来我们把grafana和prometheus关联起来,也就是在grafana中添加数据源。
4.1、添加数据源
在数据源这里选择第一个prometheus
点击save &test就可以了。
4.2、导入自带的图表
按照上面的指导添加数据源之后,我们就可以针对这些数据来绘制图表了。
grafana最人性化的一点就是拥有大量的图表模板,我们只需要导入模板即可,从而省去了大量的制作图表的时间。
目前我们的prometheus还没有什么监控数据,只有prometheus本身的数据,我们看下这些prometheus本身数据图表是怎样的。
在添加数据源的位置上,右边的选项有个Dashboards的菜单选项,我们点击进去,然后导入prometheus2.0.
接下来就可以去查看这个图表了。
至此,实验搭建结束。
送上女神一张美图,晚安!