文章目录

  • 一、概述
  • 二、安装Prometheus
  • 1.安装node_exporter
  • 2.安装Prometheus
  • 三、安装Grafana展示监控
  • 监控进程


一、概述

Prometheus 介绍

Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。随着发展,越来越多公司和组织接受采用Prometheus,社区也十分活跃,他们便将它独立成开源项目,并且有公司来运作。google SRE的书内也曾提到跟他们BorgMon监控系统相似的实现是Prometheus。现在最常见的Kubernetes容器管理系统中,通常会搭配Prometheus进行监控。

Prometheus 的优点

  1. 非常少的外部依赖,安装使用超简单
  2. 已经有非常多的系统集成 例如:docker HAProxy Nginx JMX等等
  3. 服务自动化发现
  4. 直接集成到代码
  5. 设计思想是按照分布式、微服务架构来实现的

Prometheus 的特性

  1. 自定义多维度的数据模型
  2. 非常高效的存储 平均一个采样数据占 ~3.5 bytes左右,320万的时间序列,每30秒采样,保持60天,消耗磁盘大概228G。
  3. 强大的查询语句
  4. 轻松实现数据可视化

Grafana介绍

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。它主要有以下几个特点:

展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;

数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;

通知提醒:4.0之后的添加了报警功能,可以以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;

混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;

二、安装Prometheus

1.安装node_exporter

源码地址:https://github.com/prometheus/node_exporter

在下载安装Prometheus之前我们先安装node_exporter插件,用于提供服务器监控的指标(比如:CPU、内存、磁盘、磁盘读写速率等指标),是一个非常常用的Prometheus Client插件。

下载

wget -c https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

解压

tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

后台运行

nohup node_exporter-0.18.1.linux-amd64/node_exporter  > node_exporter-0.18.1.linux-amd64/node_exporter.stdout 2>&1 &

2.安装Prometheus

下载地址:https://prometheus.io/download/

下载版本号为2.32.1,也可以根据自己需要下载其他版本

wget -c https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz

解压

tar -zxvf prometheus-2.32.1.linux-amd64.tar.gz

配置
prometheus.yml配置文件中追加node_exporter的job,监控本机服务器

[monitor@r-wb-15 prometheus-2.32.1.linux-amd64]$ pwd
/home/monitor/prometheus-2.32.1.linux-amd64
[monitor@r-wb-15 prometheus-2.32.1.linux-amd64]$ ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

prometheus.yml新增

- job_name: "node_exporter"
    static_configs:
      - targets: ["192.168.60.15:9100"]

注:如果需要监控多台服务器指标,则只需要在其他服务器上安装node_exporter即可,不需要安装prometheus。参考配置如下:

- job_name: 'node_exporter'
    static_configs:
        - '192.168.20.165:9100'
        - '192.168.20.166:9100'
        - '192.168.20.167:9100'

启动服务

nohup ./prometheus > prometheus.log &

访问prometheus

http://192.168.60.15:9090/graph

出现如下页面说明prometheus启动成功

Grafana Prometheus 服务安装部署(Linux服务器监控)_运维

三、安装Grafana展示监控

下载地址: https://grafana.com/grafana/download

下载版本:8.3.4

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.3.4-1.x86_64.rpm

安装

sudo yum install grafana-enterprise-8.3.4-1.x86_64.rpm

安装目录在/usr/share/grafana下载饼图插件

grafana-cli plugins install grafana-piechart-panel

注:安装在/var/lib/grafana/plugins目录下

启动Grafana

systemctl start grafana-server 
 systemctl enable grafana-server

访问Grafana

http://192.168.60.15:3000/login,默认账号密码admin/admin

Grafana Prometheus 服务安装部署(Linux服务器监控)_linux_02


添加数据源

Grafana Prometheus 服务安装部署(Linux服务器监控)_数据源_03


Grafana Prometheus 服务安装部署(Linux服务器监控)_kubernetes_04


Grafana Prometheus 服务安装部署(Linux服务器监控)_java_05

保存

Grafana Prometheus 服务安装部署(Linux服务器监控)_java_06


保存成功后再配置页面可以看到我们配置的数据源

Grafana Prometheus 服务安装部署(Linux服务器监控)_运维_07


导入node_exporter对应的仪表盘

Grafana Prometheus 服务安装部署(Linux服务器监控)_linux_08


Grafana Prometheus 服务安装部署(Linux服务器监控)_kubernetes_09


Grafana Prometheus 服务安装部署(Linux服务器监控)_数据源_10


导入成功后查看我们服务器监控

Grafana Prometheus 服务安装部署(Linux服务器监控)_linux_11


注:关于Granafa仪表盘ID可参考:

https://grafana.com/grafana/dashboards