Centos7 安装 Prometheus Grafana Node Exporter

环境准备

1.静态ip(要求能上外网)
2.主机名

s
1
2
3
4
5
6
7
各自配置好主机名 
# hostnamectl set-hostname --static server.cluster.com
三台都互相绑定IP与主机名
# vim /etc/hosts
10.1.1.13 server.cluster.com
10.1.1.14 agent1.cluster.com
10.1.1.15 grafana.cluster.com

3.时间同步(时间同步一定要确认一下)
4.关闭防火墙,selinux

1
2
3
# systemctl stop firewalld 
# systemctl disable firewalld
# iptables -F

Prometheus 安装

1、下载解压

1
2
3
4
[root@szl ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
[root@szl ~]# tar -zxvf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/
[root@szl ~]# mv /usr/local/prometheus-2.27.1.linux-amd64/ /usr/local/prometheus
[root@szl ~]# cd /usr/local/prometheus/

2、创建用户和数据存储目录

1
2
3
[root@szl ~]# useradd  -s /sbin/nologin -M prometheus
[root@szl ~]# mkdir /usr/local/prometheus/data
[root@szl ~]# chown -R prometheus:prometheus /usr/local/prometheus/

3、创建Systemd服务启动prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@szl ~]# vim /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=/usr/local/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target

4、启动

1
2
[root@szl ~]# systemctl start prometheus
[root@szl ~]# systemctl enable prometheus

5、打开 prometheus 的 web 界面
图片
6、正常
图片

Grafana 安装

1、下载与解压

1
2
3
4
[root@szl systemd]# wget https://dl.grafana.com/oss/release/grafana-7.5.7.linux-amd64.tar.gz
[root@szl ~]# tar -zxvf grafana-7.5.7.linux-amd64.tar.gz -C /usr/local/
[root@szl ~]# mv /usr/local/grafana-7.5.7/ /usr/local/grafana
[root@szl ~]# cd /usr/local/grafana/

2、创建用户与数据目录

1
2
3
[root@szl grafana]# useradd -s /sbin/nologin -M grafana
[root@szl grafana]# mkdir /usr/local/grafana/dat
[root@szl grafana]# chown -R grafana:grafana /usr/local/grafana/

3、检查配置文件

1
2
3
4
5
6
[root@szl grafana]# vim /usr/local/grafana/conf/defaults.ini

data = data
logs = data/log
plugins = data/plugins
provisioning = provisioning = conf/provisioning

4、创建Systemd服务启动grafana

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@szl grafana]# vim /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

5、启动

1
2
[root@szl grafana]# systemctl start grafana-server
[root@szl ~]# systemctl enable grafana-server

6、打开界面

http://192.168.2.100:3000/ 账号密码 admin/admin

图片
7、添加数据源
图片
图片
图片
8、添加图表
图片
图片

Node Exporter 安装

与传统的监控zabbix来对比的话,prometheus-server就像是mysql,负责存储数据。只不过这是时序数据库而不是关系型的数据库。数据的收集还需要其他的客户端,在prometheus中叫做exporter。针对不同的服务,有各种各样的exporter,就好比zabbix的zabbix-agent一样。

采集主机的运行指标如CPU, 内存,磁盘等信息。我们可以使用Node Exporter。Node Exporter同样采用Golang编写,并且不存在任何的第三方依赖,只需要下载,解压即可运行。

1、安装

1
2
3
[root@szl]# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
[root@szl ~]# tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@szl local]# mv /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter

2、启动,默认是9100端口

1
[root@szl local]# nohup /usr/local/node_exporter/node_exporter >/dev/null 2>&1 &

3、配置Prometheus,收集node exporter的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@szl local]# vim /usr/local/prometheus/prometheus.yml

# 增加
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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: 'node'
static_configs:
- targets: ['localhost:9100']

3、重启

1
[root@szl local]# systemctl restart prometheus

4、查看 prometheus 界面
图片
5、导入grafana模板,数据展示
图片
输入模板的编号,这里我使用的是9276、8919号模板,如要使用其他的模板,请到grafana的官网去查找 https://grafana.com/dashboards
图片
图片
图片
图片

添加监控linux主机

1、发送node_exporter到监控主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@szl ~]# scp node_exporter-1.1.2.linux-amd64.tar.gz root@192.168.80.8:~
root@192.168.80.8's password:
node_exporter-1.1.2.linux-amd64.tar.gz
[root@szl ~]# ll
总用量 35720
-rw-------. 1 root root 1655 3月 2 14:40 anaconda-ks.cfg
-rw-r--r-- 1 root root 27262558 10月 24 2009 hudson-1.330-1.1.noarch.rpm
-rw-r--r--. 1 root root 612 10月 29 2020 index.html
-rw-r--r--. 1 root root 1686 3月 2 14:49 initial-setup-ks.cfg
-rw-r--r-- 1 root root 49665 4月 23 13:01 javase-jdk8-downloads.html
-rw-r--r-- 1 root root 9246179 5月 27 09:53 node_exporter-1.1.2.linux-amd64.tar.gz
drwxr-xr-x 11 root root 167 4月 27 10:10 repo
[root@szl ~]# tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@szl ~]# mv /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter
[root@szl ~]# nohup /usr/local/node_exporter/node_exporter >/dev/null 2>&1 &
##启动
[root@szl ~]# cd /usr/local/node_exporter/
[root@szl node_exporter]# ./node_exporter

2、修改prometheus.yml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@szl ~]# vim /usr/local/prometheus/prometheus.yml

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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: 'node'
static_configs:
- targets: ['localhost:9100']
- job_name: 'node01' //增加监控主机
static_configs:
- targets: ['192.168.80.8:9100']
[root@szl ~]# systemctl restart prometheus.service

参考地址:https://blog.csdn.net/mshxuyi/article/details/108477145