一、搭建前环境准备
节点介绍
Node | OS | Hostname | IP | Version |
Prometheus & grafana server | centos 7.6 | prometheus | 192.168.66.52 | 2.16.0 |
prometheus node | centos 7.6 | Node1 |
192.168.66.53
| 0.18.1 |
二、部署prometheus
- 在prometheus & grafana server节点部署prometheus服
文章来源(Source):浅时光博客 务。
1.下载&部署
# 官网地址:https://prometheus.io/
[root@prometheus ~]# mkdir -p /opt/soft
[root@prometheus ~]# cd /opt/soft
## 注:如果通过命令行无法下载下来,则可以下载到本地PC然后ftp传至服务器
[root@prometheus soft]# wget https://github.com/prometheus/prometheus/releases/download/v2.16.0/prometheus-2.16.0.linux-amd64.tar.gz
# 部署到/usr/local/目录
# promethus不用编译安装,解压目录中有配置文件与启动文件
[root@prometheus soft]# tar -zxvf prometheus-2.16.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus soft]# cd /usr/local/
[root@prometheus local]# mv prometheus-2.16.0.linux-amd64 prometheus
# 验证
[root@prometheus local]# cd prometheus/
[root@prometheus prometheus]# ./prometheus --version

2. 设置用户
# 添加用户,后期用此账号启动服务
[root@prometheus prometheus]# groupadd prometheus
[root@prometheus prometheus]# useradd -g prometheus -s /sbin/nologin prometheus
# 赋权
[root@prometheus prometheus]# cd
[root@prometheus ~]# chown -R prometheus:prometheus /usr/local/prometheus/
# 创建prometheus运行数据目录
[root@prometheus ~]# mkdir -p /var/lib/prometheus
[root@prometheus ~]# chown -R prometheus:prometheus /var/lib/prometheus/
3.设置开机启动
[root@prometheus ~]# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
# Type设置为notify时,服务会不断重启
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 设置开机启动
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl enable prometheus
[root@prometheus ~]# systemctl start prometheus
4.设置防火墙
- IPTABLES防火墙配置
[root@prometheus ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9090 -j ACCEPT
[root@prometheus ~]# service iptables restart
- firewalld防火墙配置
# 查看目前开放的端口
[root@prometheus ~]# firewall-cmd --zone=public --list-ports
# 开放9090端口
[root@prometheus ~]# firewall-cmd --permanent --zone=public --add-port=9090/tcp #永久添加
# 使其规则生效
[root@prometheus ~]# firewall-cmd --reload
- 关闭SELINUX
[root@prometheus ~]# sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@prometheus ~]# setenforce 0 && getenforce
5.启动并验证
1)查看服务状态
[root@prometheus ~]# systemctl status prometheus
[root@prometheus ~]# ss -tunlp | grep 9090
2)访问页面
- Prometheus自带有简单的UI,http://192.168.66.52:9090

- 在Status菜单下,Configuration,Rule,Targets等,
- Statu–>Configuration展示prometheus.yml的配置,如下:
- Statu–>Targets展示监控具体的监控目标,这里监控目标”linux”暂未设置node_exporter,未scrape数据,如下:

7.绘图
- 访问:http://192.168.66.52:9090/metrics,查看从exporter具体能抓到的数据,如下:

- 访问:http://192.168.66.52:9090,在输入框中任意输入1个exporter能抓取得值,点击”Execute”与”Execute”按钮,即可见相应抓取数据的图形,同时可对时间与unit做调整,如下:

三、部署node_exporter
Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,mysqld等exporter,具体可查看官网。
- 这里在prometheus node节点部署相关服务。
1.下载&部署
# 下载
[root@node1 ~]# cd /soft
[root@node1 soft]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
# 部署
[root@node1 soft]# tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/
[root@node1 soft]# cd /usr/local
[root@node1 local]# mv node_exporter-0.18.1.linux-amd64 node_exporter
2.设置用户
[root@node1 ~]# groupadd prometheus
[root@node1 ~]# useradd -g prometheus -s /sbin/nologin prometheus
[root@node1 ~]# chown -R prometheus:prometheus /usr/local/node_exporter/
3.设置开机启动
[root@node1 ~]# vim /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable node_exporter
[root@node1 ~]# systemctl start node_exporter
4.设置防火墙
- IPTABLES防火墙配置
# 官方node_exporter默认使用9100端口
[root@node1 ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9100 -j ACCEPT
[root@node1 ~]# service iptables restart
- centos7默认开启firewalld
# 查看目前开放的端口
[root@node1 ~]# firewall-cmd --zone=public --list-ports
# 开放9100端口
[root@node1 ~]# firewall-cmd --permanent --zone=public --add-port=9100/tcp #永久添加
# 使其规则生效
[root@node1 ~]# firewall-cmd --reload
# 再次查看端口
[root@node1 ~]# firewall-cmd --zone=public --list-ports
# 检查服务状态
[root@node1 ~]# systemctl status node_exporter
[root@node1 ~]# ss -tnlp | grep 9100
5.Prometheus添加该主机
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
# 简单验证,主要配置采用默认文件配置
[root@prometheus prometheus]# vim prometheus.yml
# 全局配置
global:
scrape_interval: 15s # 设置抓取(pull)时间间隔,默认是1m
evaluation_interval: 15s # 设置rules评估时间间隔,默认是1m
# scrape_timeout is set to the global default (10s).
# 告警管理配置,暂未使用,默认配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 加载rules,并根据设置的时间间隔定期评估,暂未使用,默认配置
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取(pull),即监控目标配置
# 默认只有主机本身的监控配置
scrape_configs:
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
#新增
- job_name: 'linux'
static_configs:
- targets: ['192.168.66.53:9100'] #被监控端服务器ip
labels:
instance: node1
# 监控目标的label(这里的监控目标只是一个metric,而不是指某特定主机,可以在特定主机取多个监控目标),在抓取的每条时间序列表中都会添加此label
- 注意:job_name定义的组名必须不能相同

- 重启prometheus服务
[root@prometheus ~]# systemctl restart prometheus
6、验证
- 访问:http://192.168.66.52:9090,可见node1主机已经可被监控,如下:

四、部署grafana
- 在prometheus& grafana server节点部署grafana服务。
1.下载&安装
# 下载,可以下载好之后上传至服务器
[root@prometheus ~]# cd /soft
[root@prometheus soft]# wget https://dl.grafana.com/oss/release/grafana-6.6.0-1.x86_64.rpm
# 安装本地rpm包 localinstall
[root@prometheus soft]# yum localinstall grafana-6.6.0-1.x86_64.rpm
2.配置文件
- 配置文件位于/etc/grafana/grafana.ini,这里暂时保持默认配置即可。
3.设置开机启动
[root@prometheus soft]# systemctl enable grafana-server
[root@prometheus soft]# systemctl start grafana-server
#查看服务状态
[root@prometheus soft]# systemctl status grafana-server
[root@prometheus soft]# ss -tnlp | grep 3000
4.设置防火墙
- grafana-server默认使用3000端口;
- 如是IPT
文章来源(Source):浅时光博客 ABLES,则需要配置放通3000端口;
[root@prometheus soft]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT
[root@prometheus soft]# service iptables restart
- centos7默认开启firewalld
# 查看目前开放的端口
[root@prometheus soft]# firewall-cmd --zone=public --list-ports
# 开放3000端口
[root@prometheus soft]# firewall-cmd --permanent --zone=public --add-port=3000/tcp #永久添加
# 使其规则生效
[root@prometheus soft]# firewall-cmd --reload
# 再次查看端口
[root@prometheus soft]# firewall-cmd --zone=public --list-ports
5.添加数据源
1)登陆
- 访问:http://192.168.66.52:3000,默认账号/密码:admin/admin
2)添加数据源
- 在登陆首页,点击
Add data source
按钮,跳转到添加数据源页面,配置如下:
- Name: Prometheus
- URL: http://localhost:9090/ #prometheus服务器所在地址,如果跟grafana在同一台主机就填写localhost
- 其余默认,点击”Save & Test”,如下:

- 在
Dashboards
页签下import
自带的模版,如下:
6.导入dashboard
- 从grafana官网下载相关dashboaed到本地,如:https://grafana.com/
- Grafana首页–>页面顶部–>Dashboards–>页面左边Filter by-选择仪表盘prometheus
- 上传已下载至本地的json文件到Grafana服务器[192.168.66.52:3000](或者使用
文章来源(Source):浅时光博客 dashboard id
),如下:

- 数据源选择
prometheus
,即添加的数据源name,点击Import
按钮,如下:
7. 安装饼图的插件
#使用新的grafana-cli工具从命令行安装piechart-panel:
[root@prometheus ~]# grafana-cli plugins install grafana-piechart-panel

- 该插件将安装到您的grafana插件目录中; 如果安装了grafana软件包,则默认在
/var/lib/grafana/plugins
#重启grafana
[root@prometheus ~]# systemctl restart grafana-server
- 这样dashboard中的饼图就可以正常展示出来了
8.查看dashboard
- Grafana首页–>左上角图标–> Home,Home列表中可见有已添加的两个dashboard,” Node Exporter 0.16 0.17 for Prometheus 监控展示看板”与”Node dashboard Copy”,选择1个即可,如下:
五、 Grafana配合zabbix展示
1、安装zabbix插件,在Grafana服务器
- 官网插件地址
- 注:Zabbix插件需要Grafana3.0以上版本才可以支持
[root@prometheus ~]# grafana-cli plugins install alexanderzobnin-zabbix-app
2、添加数据源
- 根据安装的zabbix选择对于的版本
- 点击Save & Test保存
3、创建数据源

- 首先选择数据源:zabbix
- 选择zabbix内的主机组
- 选择主机
- 选择应用集
- 选择监控项
- 给这一条数据添加一个名称(将显示在图表下方,方便观看者辨识每条曲线代表什么意思)
- 如果需要添加多条曲线点击ADD Query,然后循环2-6即可
- 完成后点击右边的白色叉叉即可展现出图表,如下图:
必须 注册 为本站用户, 登录 后才可以发表评论!