实战:Prometheus+Grafana监控Linux服务器及Springboot项目

news2024/9/20 17:54:40

文章目录

    • 前言
    • 知识积累
      • 什么是Prometheus
      • 什么是Grafana
      • 怎样完成数据采集和监控
    • 环境搭建
      • docker与docker-compose安装
      • docker-compose编写
    • 监控配置
      • grafana配置prometheus数据源
      • grafana配置dashboard
        • Linux Host Metrics监控
        • Spring Boot 监控
    • 写在最后

前言

相信大家都知道一个项目交付生产并不意味着结束,更多的是对线上服务的运维监控。运维监控主要涉及到部署服务器的资源情况,各个子服务的资源情况以及垃圾收集和吞吐量等等,还有故障告警等等功能。当然,作为一个搬砖人也是需要了解全链路的运维监控组件Prometheus。

知识积累

什么是Prometheus

Prometheus 是一个开源的服务监控系统和时间序列数据库。
在这里插入图片描述

特性:
高维度数据模型
自定义查询语言
可视化数据展示
高效的存储策略
易于运维
提供各种客户端开发库
警告和报警
数据导出

什么是Grafana

Grafana是一个跨平台的开源的度量分析和可视化工具,主要用于查询并可视化展示采集的数据。

在这里插入图片描述

Grafana提供了丰富的可视化展示方式,包括快速灵活的客户端图表,拥有不同方式的可视化指标和日志的面板插件以及丰富的仪表盘插件,包括热图、折线图、图表等。

Grafana能够帮助用户快速的查看和编辑dashboard的前端。支持同时连接多种数据源,能够将时序时空数据库(TSDB)数据转换为漂亮的图表。

怎样完成数据采集和监控

通过node-exporter采集linux主要参数信息,Springboot微服务项目提供actuator监控配置。Prometheus server 主动拉取exporter采集的服务器数据提供主要内存、cpu参数展示,主动调用actuator接口拉取各种运行参数进行展示。

为了满足运维人员的各种可视化图表要求,我们再接入Grafana度量平台和可视化分析工具,其各种丰富的插件和仪表盘模板可以尽最大限度的提升监控运维质量。

环境搭建

对于Prometheus+Grafana环境的搭建,我们采用docker容器化进行部署管理。

考虑到我们可以随意更换告警、被监控项目等配置,我们将Grafana、Prometheus主要配置挂载在容器外部。

文件树:
[root@localhost app]# tree
.
├── docker-compose-prometheus.yaml
├── grafana
│ ├── data
│ │ └── grafana
│ └── grafana.ini
└── prometheus
├── app.json
└── prometheus.yml

docker与docker-compose安装

#安装docker社区版
yum install docker-ce
#版本查看
docker version
#docker-compose插件安装
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#可执行权限
chmod +x /usr/local/bin/docker-compose
#版本查看
docker-compose version

docker-compose编写

[root@localhost app]# pwd
/app
[root@localhost app]#

1、创建Prometheus配置文件

可参考 httpspro://metheus.io/docs/prometheus/latest/getting_started

[root@localhost app]# mkdir prometheus
[root@localhost app]# vim prometheus/app.json
[
    {
        "targets": [
            "10.10.18.16:8888"
        ],
        "labels": {
            "instance": "demo",
            "service": "demo-service"
        }
    }
]
[root@localhost app]# vim prometheus/prometheus.yml
global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m

scrape_configs:
  - job_name: spring-boot # springboot项目
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /actuator/prometheus
    scheme: http
    file_sd_configs:
      - files:
          - ./*.json
        refresh_interval: 1m

  - job_name: prometheus   # prometheus
    static_configs:
      - targets: ['prometheus:9090']
        labels:
          instance: prometheus


  - job_name: linux  # 采集node exporter监控数据,即linux
    static_configs:
      - targets: ['node-exporter:9100']
        labels:
          instance: localhost
          

2、创建Grafana配置文件

[root@localhost app]# mkdir -p ./grafana/data/grafana
[root@localhost app]# chmod -R 777 ./grafana/

grafana配置文件下载:
https://github.com/grafana/grafana/blob/main/conf/sample.ini
将下载的配置文件重命名为grafana.ini放在新建的文件夹下面。

修改数据库配置:

[root@localhost app]# vim grafana.ini
#################################### Database ############################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url property.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = 10.10.10.202:6456
name = grafana
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
passw0ord =MyNewPass2021
# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
url =mysql://root:MyNewPass2021@10.10.10.202:6456/grafana


# Max idle conn setting default is 2
max_idle_conn = 2

# Max conn setting default is 0 (mean not set)
max_open_conn =100

# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
conn_max_lifetime = 14400
# Set to true to log the sql calls and execution times.
log_queries =

# For "postgres", use either "disable", "require" or "verify-full"
# For "mysql", use either "true", "false", or "skip-verify".
ssl_mode = disable

# Database drivers may support different transaction isolation levels.
# Currently, only "mysql" driver supports isolation levels.
# If the value is empty - driver's default isolation level is applied.
# For "mysql" use "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ" or "SERIALIZABLE".
isolation_level =

ca_cert_path =
client_key_path =
client_cert_path =
server_cert_name =

# For "sqlite3" only, path relative to data_path setting
path = grafana.db

# For "sqlite3" only. cache mode setting used for connecting to the database
cache_mode = private

# For "sqlite3" only. Enable/disable Write-Ahead Logging, https://sqlite.org/wal.html. Default is false.
wal = false

# For "mysql" only if migrationLocking feature toggle is set. How many seconds to wait before failing to lock the database for the migrations, default is 0.
locking_attempt_timeout_sec = 0

# For "sqlite" only. How many times to retry query in case of database is locked failures. Default is 0 (disabled).
query_retries = 0

# For "sqlite" only. How many times to retry transaction in case of database is locked failures. Default is 5.
transaction_retries = 5

# Set to true to add metrics and tracing for database queries.
instrument_queries = false
#################################### SMTP / Emailing #####################
[smtp]
enabled = false
host = localhost:25
user =
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password =
cert_file =
key_file =
skip_verify = false
from_address = admin@grafana.localhost
from_name = Grafana
ehlo_identity =
startTLS_policy =

[emails]
welcome_email_on_sign_up = false
templates_pattern = emails/*.html
content_types = text/html

3、docker-compose

[root@localhost app]# vim docker-compose-prometheus.yaml
version: "3"
networks: # 网桥
  prometheus:
    ipam:
      driver: default
      config:
        - subnet: "172.22.0.0/24"

services:
  prometheus: # prometheus
    image: registry.cn-hangzhou.aliyuncs.com/senfel/prometheus:v2.34.0
    container_name: prometheus
    restart: unless-stopped
    volumes:
      - ./prometheus/app.json:/etc/prometheus/app.json
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command: "--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus"
    ports:
      - "9090:9090"
    depends_on:
      - node-exporter
    networks:
      prometheus:
        ipv4_address: 172.22.0.11

  node-exporter:  # 采集服务器层面的运行指标
    image: registry.cn-hangzhou.aliyuncs.com/senfel/node-exporter:v1.3.1
    container_name: prometheus-node-exporter
    restart: unless-stopped
    volumes:
      - /proc:/host/proc"
      - /sys:/host/sys"
    ports:
      - "9100:9100"
    networks:
      prometheus:
        ipv4_address: 172.22.0.22

  grafana:  # 用于UI展示
    image: registry.cn-hangzhou.aliyuncs.com/senfel/grafana:8.0.0
    container_name: prometheus-grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - ./grafana/data/grafana:/var/lib/grafana
      - /etc/localtime:/etc/localtime
      - ./grafana/plugins:/var/lib/grafana/plugins
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
    environment:
      GF_EXPLORE_ENABLED: "true"
      GF_SECURITY_ADMIN_PASSWORD: "admin"
      GF_INSTALL_PLUGINS: "grafana-clock-panel,grafana-simple-json-datasource,alexanderzobnin-zabbix-app"
    depends_on:
      - prometheus
    networks:
      prometheus:
        ipv4_address: 172.22.0.33
           

4、部署Prometheus+Grafana+Exporter容器

[root@localhost app]# docker-compose -f docker-compose-prometheus.yaml up -d

prometheus-node-exporter is up-to-date
Recreating prometheus … done
Recreating prometheus-grafana … done

[root@localhost app]# docker ps

在这里插入图片描述

浏览器访问 http://10.10.22.91:3000/login
在这里插入图片描述

监控配置

grafana配置prometheus数据源

1、点击 设置-data source 增加一个数据源
在这里插入图片描述

2、选择 prometheus 并配置路径

在这里插入图片描述

grafana配置dashboard

Spring Boot 2.1 Statistics:https://grafana.com/grafana/dashboards/10280
JVM (Micrometer):https://grafana.com/grafana/dashboards/4701
Linux Hosts Metrics: https://grafana.com/grafana/dashboards/10180-kds-linux-hosts/

Linux Host Metrics监控

1、点击侧边栏 增加 import
在这里插入图片描述

2、输入模板ID 10180
在这里插入图片描述

3、点击load加载
在这里插入图片描述

4、点击import导入
在这里插入图片描述在这里插入图片描述

如图所示基本涵盖服务器主要参数监控信息。

Spring Boot 监控

由于prometheus主动拉群我们项目指标,故需要暴露监控端口,且需要对项目进行改造。

Spring Boot增加监控配置
1、引入pom依赖

org.springframework.boot
spring-boot-starter-actuator


io.micrometer
micrometer-registry-prometheus

2、增加application配置

management:
  endpoints:
    web:
      exposure:
        include: health,prometheus

3、测试监控信息
启动项目
postman请求 10.10.18.16:8888/actuator/prometheus
在这里插入图片描述

导入Spring Boot 2.1 Statistics
1、点击侧边栏导入按钮-输入模板ID 10280
在这里插入图片描述

2、点击load加载仪表盘模板
在这里插入图片描述

3、点击import导入即可
在这里插入图片描述在这里插入图片描述

如图所示:基本涵盖所有的项目监控信息。

写在最后

本篇实战博文主要讲解了用docker-compose编排Prometheus、node-exporter、Grafana实现监控环境搭建,并叙述了Linux系统服务器指标监控,Springboot项目暴露指标接口进行全链路指标数据监控。当然,采用Grafana提供的Linux、Springboot监控仪表盘模板完全可以直接投入生产。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/824744.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

@ConfigurationProperties

目录 ConfigurationProperties 自定义bean使用注入 第三方bean注入 EnableConfigurationProperties ConfigurationProperties 当想需要获取到配置文件数据时,除了可以用 Spring 自带的 Value 注解外,SpringBoot 还提供了一种更加方便的方式&#xff1…

零基础C#编写上位机如何入门?

学习C#基础语法和.NET框架,掌握基本编程概念和语法,例如数据类型、类、对象、继承、多态、异常处理等。学习WinForm窗体应用程序开发技术,掌握窗体应用程序的设计和开发,例如控件的使用、事件驱动编程、窗体的布局与设计等。学习数…

结构重参数化详解。(bn+conv)与(conv+bn)的融合

结构重参数化详解。(bnconv)与(convbn)的融合 原理哪种情况不能合并?带bn的合并结构(conv bn)(bn conv)需要注意:pytorch代码 原理 如何理解结构重参数化&…

低代码是什么意思?

低代码是什么意思? 简单而言,低代码(Low-code)是一种软件开发和应用开发方法论,旨在帮助开发人员快速构建应用软件,同时减少繁琐的手动编码工作。该方法论注重简化开发流程,通过可视化建模和少…

【eNSP】Telnet远程登录

Telnet远程登录 eNSP软件TelnetTelnet远程登录-路由连接关闭防火墙eNSP根据图1画图路线配置路由端口IP配置路由R1改名配置接口IP 配置路由R2 配置R2的远程登录设置登录用户授权级别退出登录超时时间 Telnet测试 eNSP软件 eNSP(Enterprise Network Simulation Platform)是一款由…

RocketMQ 5.x延时消息源码分析(不包含时间轮)

这里是weihubeats,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党 RocketMQ版本 5.1.0 背景 首先说明本次源码分析仅分析时间轮之前的延时消息设计 现在的RocketMQ已经支持基于时间轮的任意级别延时消息 延时消息基础知识 默认…

2023财务ERP-业财一体化ERP系统功能应用,实现河北企业报表专业管理|亿发

信息技术迅速发展,传统的人工管理会导致财务与业务数据割裂,繁琐的数据处理和低效的财务管理已经不能满足河北省企业的发展需要,需要通过智能化手段持续推动河北企业的组织和管理变革。而在数字化时代,业财一体化建设与应用正逐渐…

PySpark-RDD编程入门

文章目录 2.PySpark——RDD编程入门2.1 程序执行入口SparkContext对象2.2 RDD的创建2.2.1 并行化创建2.2.2 获取RDD分区数2.2.3 读取文件创建 2.3 RDD算子2.4 常用Transformation算子2.4.1 map算子2.4.2 flatMap算子2.4.3 reduceByKey算子2.4.4 WordCount回顾2.4.5 groupBy算子…

Connection is read-only. Queries leading to data modification are not allowe解决

场景:用动态代理类调用本类中的事务方法后,又调用其他service的方法时,会抛此异常 解决方法:事务注解放在本类方法上,让处于同一事务下即可 初步认为是动态代理类影响了事务aop。

vsphere之vmotion精华 虚拟机迁移

概念 vmotion是vsphere高级功能的基础,DRS、HA、FT等功能都依赖于vmotion。简单来说,vmotion指虚拟机漂移。比如有一台VM原来在exsi1上运行,我们可以通过vmotion技术把VM移动到exsi2上。 那为什么要用到vmotion呢?显而易见&…

隔断玻璃内部的雕花工艺有哪些特点

隔断玻璃内部的雕花工艺具有以下几个特点: 1. 精细细腻:隔断玻璃内部的雕花工艺通常需要经过精细的雕刻和打磨,以展现出细腻的纹理和图案。因此,这种工艺在细节上非常令人印象深刻。 2. 独特个性:隔断玻璃内部的雕花工…

华中科技大学成功验证LK-99,美国实验室证实常温常压超导理论可行!

原创 | 文 BFT机器人 上周,全球物理学界迎来了一则令人震惊的消息:一支韩国科学家团队宣布他们发现了全球首个室温超导材料。这种材料被称为“改性铅磷灰石晶体结构(下称LK-99,一种掺杂铜的铅磷灰石)”。这项研究引起…

炒股杠杆途乐证券;股票买入卖出时间规则?

股票买入卖出时刻规则是指出资者在股票商场上进行生意交易时需求遵循的一系列时刻规定。正确的买入和卖出时刻能够协助出资者最大化出资回报,一起降低风险。但是,在股票商场上,生意时刻的挑选是一个复杂的问题,需求从多个角度剖析…

angr学习-入门篇

前言: 资源链接:GitHub - jakespringer/angr_ctf(题库仓库,里面有个讲解angr的PPT,里面有官方的题解很详细) GitHub - Hustcw/Angr_Tutorial_For_CTF: angr tutorial for ctf 安装: 关于angr…

JAVA学习(十)

1. 线程上下文切换 巧妙地利用了时间片轮转的方式, CPU 给每个任务都服务一定的时间,然后把当前任务的状态保存 下来,在加载下一任务的状态后,继续服务下一任务,任务的状态保存及再加载, 这段过程就叫做 上下文切换。时间片轮转的…

【Linux】线程同步 -- 条件变量 | 生产者消费者模型 | 自旋锁 |读写锁

初识生产者消费者模型同步条件变量初步使用 POSIX信号量其他常见的各种锁自旋锁读写锁 初识生产者消费者模型 举一个例子: 学生去超市消费的时候,与厂家生产的时候,两者互不相冲突。 生产的过程与消费的过程 – 解耦 临时的保存产品的场所(…

APP外包开发的android开发框架

Android的开发框架有很多,每个框架的特点不同,选择哪种框架取决于特定的开发需求和项目目标。今天和大家分享这方面的知识,以下是一些比较常见且重要的开发框架及其特点,希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c…

【JavaEE】深入了解Spring中Bean的可见范围(作用域)以及前世今生(生命周期)

【JavaEE】Spring的开发要点总结(4) 文章目录 【JavaEE】Spring的开发要点总结(4)1. Bean的作用域1.1 一个例子感受作用域的存在1.2 通过例子说明作用域的定义1.3 六种不同的作用域1.3.1 singleton单例模式(默认作用域…

Shell脚本学习-应用启动脚本

利用系统函数模拟实现应用脚本启动的特殊颜色效果: /server/scripts/start_nginx.sh {start|stop|restart},用if语句实现。 [rootvm1 scripts]# cat start_nginx.sh #!/bin/bash. /etc/init.d/functionsUSAGE(){echo "USAGE: $0 {start|stop|resta…