Linuxword Global
当前位置: VPS测评 > VPS监控系统,Prometheus+Grafana搭建方案

随着迷恋上各种服务自建,买了几台vps,配置都很低,动不动就出问题,等到使用的时候才发现服务坏掉了,我意识到一个监控服务是必须的。linux监控服务使用过监控宝,后来是nagios,zabbix。但是这两个东西的界面都非常简陋,甚至有些丑。直到一个群友给我推荐了Prometheus+Grafana:

chrome_JIhdkfAGtU-1024x500-1

Grafana是什么

Grafana是一跨平台的开源的可视化分析工具,目前网络架构和应用分析中最流行的时序数据展示工具,主要用于 大规模指标数据的可视化展示。 快速灵活、易用性强的客户端图表,丰富的仪表盘插件,让复杂的数据更加直观。

简单来说,grafana是一个界面,数据通过api接口读取展示

这里用的数据源是Prometheus,当然,其他的监控工具也可以使用grafana作为界面使用,但是相对来说prometheus占用资源更少

Prometheus 是什么

现在最常见的 Docker、Mesos、Kubernetes 容器管理系统中,通常会搭配 Prometheus 进行监控。 

Prometheus [prəˈmiθju:s] 普罗米修斯 Prometheus 基本原理是通过 HTTP 协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供 HTTP 接口就可以接入监控系统,不需要任何 SDK 或者其他的集成过程。这样做非常适合虚拟化环境比如 VM 或者 Docker 。

输出被监控组件信息的 HTTP 接口被叫做 exporter。

目前互联网公司常用的组件大部分都有 exporter 可以直接使用,比如 Varnish、Haproxy、Nginx、MySQL、Linux 系统信息 (包括磁盘、内存、CPU、网络等等),具体支持的源看:https://github.com/prometheus。 

exporter ([ekˈspɔ:tə(r)] 出口商)对比主流的监控工具的操作界面:nagios 监控界面:

339cf31ade92b979a19f71b4f70258fc

zabbix 监控界面

185c730edae1b9e038b38992e9630d68

通过上面的界面,我们可以看出来 Grafana+Prometheus 展示界面更美观 1.1.2 Prometheus 架构图解普罗米修斯(Prometheus)及其一些生态系统组件的整体架构:

9c58a1d185d5ac70ef79b6c1048ef514

搭建过程

主服务器安装Prometheus+Grafana

操作系统:Centos7.6

安装Prometheus 

Prometheus 项目主页:https://github.com/prometheus/prometheus/

根据自己的操作系统选择合适的包

# 查看操作系统版本
cat /etc/os-release 

#NAME="CentOS Linux"
#VERSION="7 (Core)"

yum update
yum install wget

# 下载prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.40.2/prometheus-2.40.2.linux-amd64.tar.gz
tar zxf prometheus-2.40.2.linux-amd64.tar.gz -C /opt
mv /opt/prometheus-2.40.2.linux-amd64 /opt/prometheus

配置开机启动

vim /usr/lib/systemd/system/prometheus.service
# content #
[Unit]
Description=prometheus service
 
[Service]
User=root
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data
 
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
 
[Install]
WantedBy=multi-user.target

启动服务

systemctl daemon-reload
systemctl enable prometheus --now
systemctl status prometheus

开启防火墙端口9090

# 如果防火墙未开启就不用打开端口了#
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --reload

浏览器里访问 http://ip:9090

安装Grafana

设置官方源

vim /etc/yum.repos.d/grafana.repo

[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt

如果是国内服务器最好是设置中文源

vim /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm
repo_gpgcheck=0
enabled=1
gpgcheck=0

安装

yum makecache -y
yum install grafana -y
systemctl daemon-reload
systemctl enable grafana-server --now
systemctl status grafana-server

开启防火墙端口3000

# 如果防火墙未开启就不用打开端口了#
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload

浏览器里访问 http://ip:3000

默认用户名密码都是admin,初次登录会要求修改默认的登录密码:

grafana-login-3-1-1024x503-1
Grafana
grafana-login-home-1-1024x502-1
Grafana

添加Prometheus数据源

点击主界面的“Add your first data source”并选择Prometheus:

grafana-add-first-source-1-1024x503-1
grafana-select-1-1024x505-1
Prometheus+Grafana

Dashboards页面选择“Prometheus 2.0 Stats”进行Import:

prometheus-dashboard-1-1024x286-1

Settings页面填写普罗米修斯地址并保存:

prometheus-settings-1-1024x625-1

切换到我们刚才添加的“Prometheus 2.0 Stats”即可看到整个监控页面:

prometheus-dashboard-2.0-2-1024x505-1

安装配置 node_exporter

这是被监控端需要安装的,因为主服务器本身也要监控,所以也要安装

项目主页:https://github.com/prometheus/node_exporter

wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
tar xf node_exporter-1.4.0.linux-amd64.tar.gz -C /opt/

mv /opt/node_exporter-1.4.0.linux-amd64/ /opt/node_exporter

创建 node_exporter 系统服务启动文件

vim /usr/lib/systemd/system/node_exporter.service

#Prometheus Node Exporter Upstart script
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=root
ExecStart=/opt/node_exporter/node_exporter

[Install]
WantedBy=default.target

启动node_exporter

systemctl daemon-reload
systemctl enable node_exporter --now
systemctl status node_exporter
# 服务启动后可以用 http://localhost:9100/metrics 
# 如果防火墙未开启就不用打开端口了#
firewall-cmd --zone=public --add-port=9100/tcp --permanent
firewall-cmd --reload

浏览器里访问 http://ip:9100/metrics 测试 node_exporter 是否获取到节点的监控指标。

Prometheus配置文件添加监控项:

vim /opt/prometheus/prometheus.yml

# 务必注意格式,多少个空格就是多少个空格,不然会报错。

  - job_name: 'linux-master'
    static_configs:
    - targets: ['localhost:9100']
      labels:
        instance: linux-master

重启prometheus

systemctl reload prometheus.service

回到Grafana的主界面,grafana导入画好的dashboard:

下载json文件:https://picture-bed-iuskye.oss-cn-beijing.aliyuncs.com/prometheus/node-exporter_rev5.json

其他的exporter:

https://cloud.tencent.com/document/product/1416/56041

 

ne-manage-1
ne-import-1
ne-upload-1

修改名字,选择我们前文创建好的数据源,点击导入即可:

ne-rename1-1

下面这个提示是grafana缺少相关显示需要用到的插件piechart,grafana的默认插件目录是/var/lib/grafana/plugins,可以将下载好的插件解压到这个目录,重启grafana即可

安装插件

grafana-cli plugins install grafana-piechart-panel
grafana-cli plugins install digiapulssi-breadcrumb-panel
grafana-cli plugins install grafana-polystat-panel
 
systemctl restart grafana-server

查看已安装插件:

/usr/sbin/grafana-cli plugins ls
installed plugins:
grafana-piechart-panel @ 1.3.3

再刷新grafana页面,即可看到我们刚才设置好的node监控:

ne-success-1-1024x502-1

被监控主机:

其他被监控vps只需要安装node_exporter

打开防火墙,允许服务器访问Prometheus 端口

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="Pro主机的ip" port protocol="tcp" port="9090" accept"
firewall-cmd --reload

去grafana添加数据源

同上

报警设置

邮箱告警

ttelegram告警

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) 打赏
一分也是爱

支付宝扫一扫打赏

微信扫一扫打赏

上一篇:

下一篇:

相关推荐

博客简介

本站CDN采用VmShell免费提供离中国大陆最近的香港CMI高速网络做支撑,ToToTel打造全球最快速的边沿网络支撑服务,具体详情请见 :https://vmshell.com/ 以及 https://tototel.com/,网站所有的文件和内容禁止大陆网站搬迁复制,谢谢,VPS营销投稿邮箱: admin@linuxxword.com,我们免费帮大家发布,不收取任何费用,请提供完整测试文稿!

精彩评论

友情链接

他们同样是一群网虫,却不是每天泡在网上游走在淘宝和网游之间、刷着本来就快要透支的信用卡。他们或许没有踏出国门一步,但同学却不局限在一国一校,而是遍及全球!申请交换友链

站点统计

  • 文章总数: 2343 篇
  • 草稿数目: 12 篇
  • 分类数目: 6 个
  • 独立页面: 0 个
  • 评论总数: 2 条
  • 链接总数: 0 个
  • 标签总数: 6116 个
  • 注册用户: 139 人
  • 访问总量: 8,652,443 次
  • 最近更新: 2024年4月28日