一、新建虚拟机
本次安装镜像文件:CentOS-7-x86_64-DVD-1810
二、软件版本
prometheus-2.13.0.linux-amd64.tar.gz
grafana-6.4.2.linux-amd64.tar.gz
node_exporter-0.18.1.linux-amd64.tar.gz
二、更换镜像源
1.备份(针对所有CentOS可用,备份文件在当前路径下)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2.安装wget
yum install -y wget
3.下载CentOS 7的repo文件
本人使用的阿里云第二个地址
1.阿里云源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.网易云源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
4.更新镜像源
1.#清除缓存
yum clean all
2.#生成缓存
yum makecache
3.更新yum源,等待更新完毕即可。
yum -y update(执行加载完后会卡顿一会)
4.yum clean all
5.yum makecache
5.安装vim编辑器(可以不安装)
之前用的是vi,现在采用vim
yum install -y vim
四、部署prometheus
1. 安装prometheus
Prometheus 是一款基于时序数据库的开源监控告警系统
1.教程地址
https://www.cnblogs.com/auguse/articles/14078915.html
2.下载安装并解压
方法一:
1.镜像源替换完后,通过wget下载
#下载
wget https://github.com/prometheus/prometheus/releases/download/v2.13.0/prometheus-2.13.0.linux-amd64.tar.gz
#解压
tar -xf prometheus-2.13.0.linux-amd64.tar.gz
#移动
mv prometheus-2.13.0.linux-amd64 /usr/local/
#创建软连接
ln -s /usr/local/prometheus-2.13.0.linux-amd64/ /usr/local/prometheus
方法二:
换了镜像源后下载还是太慢,采用tar包解压方式
1.上传tar包到tmp目录
2.进入tmp目录解压
tar -zxvf prometheus-2.13.0.linux-amd64.tar.gz
3.移动目录
mv prometheus-2.13.0.linux-amd64 /usr/local/
4.创建软连接
ln -s /usr/local/prometheus-2.13.0.linux-amd64/ /usr/local/prometheus
3.配置说明
此步骤不执行,仅观看
解压后当前目录会包含默认的Prometheus配置文件promethes.yml,下面配置文件做下简略的解析:
# 全局配置
global:
scrape_interval: 15s # 设置抓取间隔,默认为1分钟
evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
# scrape_timeout #默认抓取超时,默认为10s
# Alertmanager相关配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置列表
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
4.创建prometheus的用户及数据存储目录
为了安全,我们使用普通用户来启动prometheus服务。作为一个时序型的数据库产品,prometheus的数据默认会存放在应用所在目录下,我们需要修改为 /data/prometheus下。
useradd -s /sbin/nologin -M prometheus
mkdir /data/prometheus -p
#修改目录属主
chown -R prometheus:prometheus /usr/local/prometheus/
chown -R prometheus:prometheus /data/prometheus/
5.创建Systemd服务启动prometheus
prometheus的启动很简单,只需要直接启动解压目录的二进制文件prometheus即可,但是为了更加方便对prometheus进行管理,这里使用systemd来启停prometheus
vi /etc/systemd/system/prometheus.service
输入上面指令后,将下面的内容粘贴进去,保存退出
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
备注:在service文件里面,我们定义了启动的命令,定义了数据存储在/data/prometheus路径下,否则默认会在prometheus二进制的目录的data下。
#启动 prometheus
systemctl start prometheus
#查看状态
systemctl status prometheus
#设置开机自启
systemctl enable prometheus
6.打开prometheus的web页面
- prometheus安装好会默认启动9090端口,
- 通过下面地址访问,会发现进不去,需要将9090端口开放,再次访问
http://172.16.3.3:9090/graph
172.16.3.3为虚拟机地址
7.开启9090端口
# 查询端口是否开放
firewall-cmd --query-port=9090/tcp
# 开放9090端口
firewall-cmd --permanent --add-port=9090/tcp
#更新防火墙规则
firewall-cmd --reload
8.再次访问prometheus的web页面
http://172.16.3.3:9090/graph
2. 安装Grafana
虽然说prometheus能展示一些图表,但对比Grafana还是比较弱的。接下来我们需要在同一个服务器上安装Grafana服务,用来展示prometheus收集到的数据。
1.下载安装并解压
1.上传tar包到tmp目录
2.进入tmp目录解压
tar -zxvf grafana-6.4.2.linux-amd64.tar.gz
3.移动目录
mv grafana-6.4.2 /usr/local/
4.创建软连接
ln -s /usr/local/grafana-6.4.2/ /usr/local/grafana
2.创建grafana用户及数据存放目录
useradd -s /sbin/nologin -M grafana
mkdir /data/grafana
##修改目录属主
chown -R grafana:grafana /usr/local/grafana/
chown -R grafana:grafana /data/grafana/
3. 修改配置文件
修改 /usr/local/grafana/conf/defaults.ini 文件,配置为上面新建的数据目录。
vi /usr/local/grafana/conf/defaults.ini
输入上面指令后,找到相同的内容,将下面的内容与原来的内容进行替换
data = /data/grafana/data
logs = /data/grafana/log
plugins = /data/grafana/plugins
provisioning = /data/grafana/conf/provisioning
4.把grafana-server添加到systemd中
新增 grafana-server.service 文件,使用systemd来管理grafana服务
vi /etc/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
Restart=on-failure
[Install]
WantedBy=multi-user.target
#启动grafana-server
systemctl start grafana-server
#查看状态
systemctl status grafana-server
#设置开机自启
systemctl enable grafana-server
5.打开grafana的web页面
-
grafana安装完成,默认会启动3000端口
-
使用浏览器打开下面地址,会发现进不去,需要开放3000端口
http://172.16.3.3:3000
6.开启3000端口
# 查询端口是否开放
firewall-cmd --query-port=3000/tcp
# 开放3000端口
firewall-cmd --permanent --add-port=3000/tcp
#更新防火墙规则
firewall-cmd --reload
7.再次访问grafana的web页面
http://172.16.3.3:3000/login
账号: admin
密码: admin
登录进去会提示修改密码,按提示操作就行,修改密码为“123456”
8.添加数据源
- grafana虽然已经安装好了,但是这个时候还没有数据,没办法作图。下面我们把grafana和prometheus关联起来,也就是在grafana中添加添加数据源。
- 在配置页面点击添加数据源,然后选择prometheus,输入prometheus服务的参数即可。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lly7e1bt-1616054383504)(https://tuchuang2259.oss-cn-beijing.aliyuncs.com/img/%E4%BF%AE%E6%94%B9%E5%8F%82%E6%95%B0%EF%BC%8C%E4%BF%9D%E5%AD%98.jpg)]
9.添加自带的示例图表
- 按照上面的指导添加数据源之后,我们就可以针对这些数据来绘制图表了。grafana最人性化的一点就是拥有大量的图表模板,我们只需要导入模板即可,从而省去了大量的制作图表的时间。
目前我们的prometheus还没有什么监控数据,只有prometheus本身的数据,我们看下这些prometheus本身数据图表是怎样的。 - 在添加数据源的位置上,右边的选项有个Dashboards的菜单选项,我们点击进去,然后导入prometheus2.0.
最后我们在左上角的位置上选择这个图表查看下,是不是很酷炫呢。这是个简单的示例,后续我们使用node-exporter来收集主机的性能信息,然后在grafana中展示。
3. 使用Node Exporter采集主机运行数据
与传统的监控zabbix来对比的话,prometheus-server就像是mysql,负责存储数据。只不过这是时序数据库而不是关系型的数据库。数据的收集还需要其他的客户端,在prometheus中叫做exporter。针对不同的服务,有各种各样的exporter,就好比zabbix的zabbix-agent一样。
这里为了能够采集到主机的运行指标如CPU, 内存,磁盘等信息。我们可以使用Node Exporter。Node Exporter同样采用Golang编写,并且不存在任何的第三方依赖,只需要下载,解压即可运行。可以从https://prometheus.io/download/获取最新的node exporter版本的二进制包。
1.下载安装并解压
1.上传tar包到tmp目录
2.进入tmp目录解压
tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz
3.创建目录
mkdir -p /usr/local/prometheus_exporter
4.移动目录
mv node_exporter-0.18.1.linux-amd64 /usr/local/prometheus_exporter/
5.创建软连接
cd /usr/local/prometheus_exporter/
ln -s node_exporter-0.18.1.linux-amd64/ node_exporter
2.启动node exporter
-
直接打开node_exporter的可执行文件即可启动 node export,默认会启动9100端口。
-
建议使用nohup来启动
-
nohup命令表示在后台运行
#前台运行
/usr/local/prometheus_exporter/node_exporter/node_exporter
#后台运行
nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &
3.设置开机启动
在此目录下写上需要开机启动的命令:etc/rc.d/rc.local
1.先给文件拥有者给 执行权限 x
chmod +x /etc/rc.d/rc.local
2.进入rc.local 在touch开头的下一行,加上下面的命令
vi /etc/rc.d/rc.local
3.在最后一行加入下面命令
nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &
4.配置Prometheus,收集node exporter的数据
- node exporter启动后也暴露了9100端口,并没有把数据传到prometheus,我们还需要在prometheus中配置,让prometheus去pull这个接口的数据。
- 编辑prometheus.yml文件,将原有内容删除,将下面内容粘贴
vi /usr/local/prometheus-2.13.0.linux-amd64/prometheus.yml
# 全局配置
global:
scrape_interval: 15s # 设置抓取间隔,默认为1分钟
evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
# scrape_timeout #默认抓取超时,默认为10s
# Alertmanager相关配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置列表
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
#采集chenxin exporter监控数据
- job_name: 'chenxin'
static_configs:
- targets: ['localhost:9100']
6.重启
打开prometheus页面查看是不是有对应的数据了。
systemctl restart prometheus
systemctl restart grafana-server
7.重新打开网页,进行下面操作
http://172.16.3.3:9090/graph
8.注意
如果上图的 State不是UP,那么请关闭虚拟机,重新开启虚拟机,重新输入网站打开网页,稍等片刻再次刷新
9.导入grafana模板,数据展示
http://172.16.3.3:3000
在导入界面,我们输入模板的编号,这里我使用的是9276号模板,如要使用其他的模板,请到grafana的官网去查找 https://grafana.com/grafana/dashboards
10.开放9100端口
如果别人想监控你,那么你就需要开放9100端口
# 查询端口是否开放
firewall-cmd --query-port=9100/tcp
# 开放9100端口
firewall-cmd --permanent --add-port=9100/tcp
#更新防火墙规则
firewall-cmd --reload
4. 监控对方服务器
注意事项
- 保证对方服务器有部署Node Exporter
- 保证对方开放了node exporter的9100端口
- 保证对方服务器不能处于被别人监控状态
- 配置文件的job_name后面的值不能的别的job_name相同
- 重启prometheus和grafana后重新输入网站打开,再次刷新,如果还不行重启虚拟机
更改配置文件
进入配置文件,末尾添加需要监控对方服务器的信息,最后保存退出,重启,重新打开网页刷新
vi /usr/local/prometheus-2.13.0.linux-amd64/prometheus.yml
#采集柳皓友数据
- job_name: 'liu'
static_configs:
- targets: ['172.16.3.15:9100']
#重启prometheus
systemctl restart prometheus
#重启grafana
systemctl restart grafana-server
打开网页,监控对方数据
http://172.16.3.3:3000/