grafana监控linux,使用Prometheus+Grafana搭建监控系统实践

使用Prometheus+Grafana搭建监控系统实践

一.环境

1. 拓扑

552d5382a70c1fc79867cf35d91e05ee.png

2. 节点

Node

OS

Hostname

IP

Remark

prometheus& grafana server

prometheus

172.20.1.211

prometheus node

centos 7.4

node1

172.20.1.212

3. 版本(截止20171130)

二.部署prometheus

在prometheus& grafana server节点部署prometheus服务。

1. 下载&部署

#下载

[root@prometheus src]#cd /usr/local/src/

[root@prometheus src]##部署到/usr/local/目录#promethus不用编译安装,解压目录中有配置文件与启动文件

[root@prometheus src]#tar -zxvf prometheus-2.0.0.linux-amd64.tar.gz -C /usr/local/

[root@prometheus src]#cd /usr/local/

[root@prometheus local]#mv prometheus-2.0.0.linux-amd64/ prometheus/

#验证

[root@prometheus local]#cd prometheus/

[root@prometheus prometheus]#./prometheus --version

9418ee03ac69bf07e5887a899176a4c2.png

2. 配置文件

#解压目录中的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:#监控目标的label(这里的监控目标只是一个metric,而不是指某特定主机,可以在特定主机取多个监控目标),在抓取的每条时间序列表中都会添加此label

- job_name: 'prometheus'

#metrics_path defaults to '/metrics'

#scheme defaults to 'http'.

# 可覆盖全局配置设置的抓取间隔,由15秒重写成5秒。

scrape_interval: 5s#静态指定监控目标,暂不涉及使用一些服务发现机制发现目标

static_configs:- targets: ['localhost:9090']# (opentional)再添加一个label,标识了监控目标的主机

labels:

instance: prometheus

- job_name: 'linux'

scrape_interval: 10s

static_configs:

# 采用node_exporter默认开放的端口

- targets: ['172.20.1.212:9100']

labels:

instance: node1

3. 设置用户

#添加用户,后期用此账号启动服务

[root@prometheus prom etheus]#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/

4. 设置开机启动

[root@prometheus ~]#touch /usr/lib/systemd/system/prometheus.service

[root@prometheus ~]#chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service

[root@prometheus~]#vim /usr/lib/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 enable Prometheus

[root@prometheus ~]#systemctl start prometheus

5. 设置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

6. 启动并验证

1)查看服务状态

[root@prometheus ~]#systemctl status prometheus

edc0feaf1877662395b82b9e7120c044.png

[root@prometheus ~]#netstat -tunlp | grep 9090

8c19f0a564a081ac6a6dcbffd5875fb9.png

2)web ui

15174ada49b5230bb74fab9f09a473db.png

在Status菜单下,Configuration,Rule,Targets等,

Statu-->Configuration展示prometheus.yml的配置,如下:

27e09a386f9b26ea3d5b069be376b594.png

Statu-->Targets展示监控具体的监控目标,这里监控目标"linux"暂未设置node_exporter,未scrape数据,如下:

dd070871f4ea7707ef803ef986ef9b29.png

7. 绘图

981e1601990beff3e439f43a2a36d4b2.png

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

c2560d3f9f138492dc69ce6a6fb6e760.png

三.部署node_exporter

Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,MySQLd等exporter,具体可查看官网。

这里在prometheus node节点部署相关服务。

1. 下载&部署

#下载

[root@node1 ~]#cd /usr/local/src/

[root@node1 src]##部署

[root@node1 src]#tar -zxvf node_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/

[root@node1 src]#cd /usr/local/

[root@node1 local]#mv node_exporter-0.15.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 /usr/lib/systemd/system/node_exporter.service

[Unit]

Description=node_exporter

Documentation=https://prometheus.io/After=network.target

[Service]

Type=simple

User=prometheus

ExecStart=/usr/local/node_exporter/node_exporter

Restart=on-failure

[Install]

WantedBy=multi-user.target

[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

5. 验证

b7402b67b5b862a928663a621dd21c68.png

四.部署grafana

在prometheus& grafana server节点部署grafana服务。

1. 下载&安装

#下载

[root@prometheus ~]#cd /usr/local/src/

[root@prometheus src]##安装

[root@prometheus src]#yum localinstall grafana-4.6.2-1.x86_64.rpm

2. 配置文件

配置文件位于/etc/grafana/grafana.ini,这里暂时保持默认配置即可。

3. 设置开机启动

[root@prometheus src]#systemctl enable grafana-server

[root@prometheus src]#systemctl start grafana-server

4. 设置iptables

#grafana-server默认使用3000端口

[root@prometheus src]#vim /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT

[root@prometheus src]#service iptables restart

5. 添加数据源

1)登陆

1beabe2ad4f7a0051a7f3d5c06720009.png

2)添加数据源

在登陆首页,点击"Add data source"按钮,跳转到添加数据源页面,配置如下:

Name: prometheus

Type: prometheus

URL: http://localhost:9090/

Access: proxy

取消Default的勾选,其余默认,点击"Add",如下:

78057d4c536dafee92495fbc7443bc80.png

在"Dashboards"页签下"import"自带的模版,如下:

92f756608734c65cfd1984c5039704fd.png

6. 导入dashboard

Grafana首页-->左上角图标-->Dashboard-->import

8b37e242743592cedd50b62a6ede422f.png

Upload已下载至本地的json文件(或者使用dashboard id,如这里的405),如下:

ea4f1de91286559b6d298adb78ba4095.png

数据源选择"prometheus",即添加的数据源name,点击"Import"按钮,如下:

4df8bfa4bde5214d718bdbb6ca82c014.png

7. 查看dashboard

Grafana首页-->左上角图标-->Dashboard-->Home,Home下拉列表中可见有已添加的两个dashboard,"Prometheus Stats"与"Node Exporter Server Metrics",选择1个即可,如下:

37459cec6d797c94db5a3ef4344e6cec.png

a0d8aa636b1e9a05e0a3d7537b37edbe.png

31618be7096a31105b7204ea81bef7e1.png

参考文档:

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值