在Ubuntu上搭建Prometheus + Grafana监控系统

news2024/10/7 16:17:15

1.Prometheus 部署
从官网下载页面找到最新的二进制文件下载

cd ~
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.51.1/prometheus-2.51.1.linux-amd64.tar.gz

将文件解压到指定目录

tar  xf prometheus-2.51.1.linux-amd64.tar.gz -C /usr/local

为了方便配置,可以做软连接

ln -sv /usr/local/prometheus-2.51.1.linux-amd64/ /usr/local/Prometheus

接着修改prometheus的配置文件

在global设置中,定义抓取指标的默认时间间隔。请注意,除非单个导出器自己的设置覆盖全局变量,否则Prometheus会将这些设置应用于每个导出器。
scrape_interval值告诉Prometheus 每15秒从其出口商那里收集指标,这对于大多数出口商而言足够长。
使用scrape_configs指令将Prometheus本身添加到导出器列表中。
使用job_name在标签和图表上标记出口商,因此请务必在此处选择描述性内容。
使用static_configs和targets指令来确定导出器的运行位置。

[root@prometheus ~]# vim /usr/local/Prometheus/prometheus.yml 
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.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']

创建Prometheus的systemd服务

sudo vim /etc/systemd/system/prometheus.service

服务文件告诉systemd,您将Prometheus作为prometheus用户运行,并且配置文件位于/etc/prometheus/prometheus.yml目录中,并将其数据存储在/var/lib/prometheus目录中。配置文件内容如下:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
​
[Service]
# 账户和组设置,可以保证数据安全
# User=prometheus
# Group=prometheus
Type=simple
ExecStart=/usr/local/Prometheus/prometheus \
    --config.file /usr/local/Prometheus/prometheus.yml \
#  --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/usr/local/Prometheus/consoles \
    --web.console.libraries=/usr/local/Prometheus/console_libraries[Install]
WantedBy=multi-user.target

编辑完成后保存退出,要使用新创建的服务,需要重新加载systemd。

sudo systemctl daemon-reload

并可以使用以下命令启动Prometheus:

sudo systemctl start prometheus

要确保Prometheus正在运行,可以检查服务的状态。

sudo systemctl status prometheus

输出结果会告诉您Prometheus的状态,主进程标识符(PID),内存使用等。如果服务的状态不是active,在继续本教程之前重新跟踪上述步骤以解决问题。
在这里插入图片描述
按Q可退出status命令。通过如下URL可以打开prometheus的自带监控界面: localhost:9090,当然端口或IP也可以自己设定。

启用服务以在开机时启动:

sudo systemctl enable prometheus

2.部署Node Exporter (被监控主机需要安装的软件)

各组件默认端口:

node 默认端口:9100
mysql默认端口:9104
redis 默认端口:9121
process默认端口:9256
alertmanager默认端口:9093

要将Prometheus扩展到仅关于自身的指标之外,我们将安装另一个名为Node Exporter的节点导出器。节点导出器提供有关系统的详细信息,包括CPU,磁盘和内存使用情况。 把版本和下载链接更换一下就行了

cd ~
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
sha256um node_exporter-1.1.2.linux-amd64.tar.gz
tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local

接着为Node Exporter创建Systemd服务文件。

sudo vim /etc/systemd/system/node_exporter.service

将以下内容复制上去,编辑完成后保存退出。(注意文件名版本号)

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter-1.1.2.linux-amd64/node_exporter

[Install]
WantedBy=multi-user.target

重新加载systemd以使用新的创建的服务。

sudo systemctl daemon-reload

运行Node Exporter。

sudo systemctl start node_exporter

如果想额外配置mysql、redis之类的,步骤如三、四类似

3.配置Prometheus以刮取节点导出器

因为Prometheus只抓取在其配置文件scrape_configs部分中定义的导出器,所以我们需要为Node Exporter添加一个条目,就像我们为Prometheus本身所做的那样。

修改prometheus的配置文件。

[root@prometheus ~]# vim /usr/local/Prometheus/prometheus.yml 
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.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'
    scrape_interval: 5s

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
 
  - job_name: 'node_exporter'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100'] 

重新启动prometheus并检查运行状态。

sudo systemctl restart prometheus
sudo systemctl status prometheus

4.添加节点进行主机管理
在添加大量主机集群时,一台一台在prometheus.yml中添加,显然不太方便,我们通过编写发现文件,进行批量主机管理。

cd /usr/local/Prometheus
mkdir -p /usr/local/Prometheus/targets/node
sudo vim /usr/local/Prometheus/targets/node/node.yml
# 监控的目标列表
- targets: 
  - '172.16.12.1:9100'
  - '172.16.12.5:9100'
  - '172.16.12.110:9100'
  - '172.16.12.112:9100'
  labels:
    idc: "Temporary RWE Server"      # 备注集群名

修改prometheus配置文件。

vim /usr/local/Prometheus/prometheus.yml

添加如下代码

  - job_name: 'nm_mch-app_node'
    file_sd_configs:
      - files: ['/usr/local/Prometheus/targets/node/node.yml']
        refresh_interval: 5s

重新启动prometheus并检查运行状态。

sudo systemctl restart prometheus
sudo systemctl status prometheus

5.使用Grafana创建可视化Dashboard
Prometheus UI提供了快速验证PromQL以及临时可视化支持的能力,而在大多数场景下引入监控系统通常还需要构建可以长期使用的监控数据可视化面板(Dashboard)。这时用户可以考虑使用第三方的可视化工具如Grafana,Grafana是一个开源的可视化平台,并且提供了对Prometheus的完整支持。

可在官网下载最新的二进制文件,tar安装过程参考如下:

wget https://dl.grafana.com/oss/release/grafana-8.0.4.linux-amd64.tar.gz
tar -zxvf grafana-8.0.4.linux-amd64.tar.gz -C /usr/local
ln -sv /usr/local/grafana-8.0.4/ /usr/local/Prometheus_grafana

配置systemd启动Grafana。

sudo vim /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana
After=network-online.target

[Service]
ExecStart=/usr/local/Prometheus_grafana/bin/grafana-server --config=/usr/local/Prometheus_grafana/conf/defaults.ini --homepath=/usr/local/Prometheus_grafana

[Install]
WantedBy=multi-user.target

重载配置

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server

Dashboard通过 https://grafana.com/dashboards 网站,可以找到大量可直接使用的Dashboard。Grafana中所有的Dashboard通过JSON进行共享,下载并且导入这些JSON文件,就可以直接使用这些已经定义好的Dashboard

参考文献

大神博客
Prometheus中文文档

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

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

相关文章

⼿机客户端画K线图流程

优质博文&#xff1a;IT-BLOG-CN 一、什么是K线流程 K线图是一种用于展示金融市场价格走势的图表。它通常由四个关键价格点组成&#xff0c;即开盘价、收盘价、最高价和最低价。K线图的流程可以简单概括为以下几个步骤&#xff1a; 【1】收集数据&#xff1a; 首先&#xff0c…

Oracle 正则表达式

一、Oracle 正则表达式相关函数 (1) regexp_like &#xff1a;同 like 功能相似&#xff08;模糊 匹配&#xff09; (2) regexp_instr &#xff1a;同 instr 功能相似&#xff08;返回字符所在 下标&#xff09; (3) regexp_substr &#xff1a; 同 substr 功能相似&…

虚拟网络设备与Linux网络协议栈

在现代计算环境中&#xff0c;虚拟网络设备在实现灵活的网络配置和隔离方面发挥了至关重要的作用&#x1f527;&#xff0c;特别是在容器化和虚拟化技术广泛应用的今天&#x1f310;。而Linux网络协议栈则是操作系统处理网络通信的核心&#x1f4bb;&#xff0c;它支持广泛的协…

点击上传文件

一、页面样式&#xff1a; &#xff08;1&#xff09;点击前&#xff1a; &#xff08;2&#xff09;点击后&#xff1a; 设计&#xff1a;①自定义elementPlus图标&#xff1b;②使用Tooltip实现鼠标悬浮按钮上出现文字提示&#xff1b;③上传与更换的切换样式&#xff1b;…

全栈开发医疗小程序 SpringBoot2.X + Vue + UniAPP 带源码

看到好多坛友都在求SpringBoot2.X Vue UniAPP&#xff0c;全栈开发医疗小程序 – 带源码课件&#xff0c;我看了一下&#xff0c;要么链接过期&#xff0c;要么课件有压缩密码。特意整理了一份分享给大家&#xff0c;个人认为还是比较全面的。希望对大家有所帮助&#xff01;…

云计算(五)—— OpenStack基础环境配置与API使用

OpenStack基础环境配置与API使用 项目实训一 【实训题目】 使用cURL命令获取实例列表 【实训目的】 理解OpenStack的身份认证和API请求流程。 【实训准备】 &#xff08;1&#xff09;复习OpenStack的认证与API请求流程的相关内容。 &#xff08;2&#xff09;熟悉cURL…

openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint

文章目录 openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint258.1 功能描述258.2 语法格式258.3 示例 openGauss学习笔记-258 openGauss性能调优-使用Plan Hint进行调优-指定子查询不展开的Hint 258.1 功能描述 数据库在对查询进行逻辑…

vulhub中Apache Solr Velocity 注入远程命令执行漏洞复现 (CVE-2019-17558)

Apache Solr 是一个开源的搜索服务器。 在其 5.0.0 到 8.3.1版本中&#xff0c;用户可以注入自定义模板&#xff0c;通过Velocity模板语言执行任意命令。 访问http://your-ip:8983即可查看到一个无需权限的Apache Solr服务。 1.默认情况下params.resource.loader.enabled配置…

OpenCV单通道图像按像素成倍比例放大(无高斯平滑处理)

OpenCV中的resize函数可以对图像做任意比例的放大(/缩小)处理&#xff0c;该处理过程会对图像做高斯模糊化以保证图像在进行放大&#xff08;/缩小&#xff09;后尽可能保留源图像所展现的具体内容&#xff08;消除固定频率插值/采样带来的香农采样信息损失&#xff09;&#x…

7款公司电脑监控软件

7款公司电脑监控软件 研究证明&#xff0c;人们在家办公的效率比在办公室办公的效率低一半&#xff0c;其中原因是缺少监督&#xff0c;即便在公司办公&#xff0c;还存在员工偷闲的时刻&#xff0c;比如聊天、浏览无关网站、看剧、炒股等&#xff0c;企业想提高员工的工作效率…

GFS分布式 文件系统

一、GFS的概念 文件存储分为nfs、lvm、raid 对象存储分为GFS、CEPH、fastDFS&#xff08;分布式文件存储&#xff09;NAS OSS S3 switch OSS 属于阿里云 通过URL 链接 S3属于亚马逊通过URL链接 1.1 GFS简介 开源的分布式文件系统&#xff0c;由存储服务器、客户端…

工地安全监测识别摄像机

工地安全监测识别摄像机是一种在建筑工地和施工现场广泛使用的智能监控设备&#xff0c;主要用于监测施工过程中可能出现的安全隐患和违规行为&#xff0c;以确保工地人员和设备的安全。通过高清摄像头、智能算法和远程监控系统的结合&#xff0c;该摄像机可以实时监测工地各个…

基于支持 GPT 的服务的初创公司

Kafkai&#xff1a;多语言长篇内容生成&#xff0c;AI写作的新趋势 介绍 随着生成式预训练 Transformer (GPT) 的出现&#xff0c;技术世界正在见证范式转变。 这种人工智能驱动的创新不仅仅是一种转瞬即逝的趋势&#xff0c;而是一种趋势。 它已成为科技行业的基石&#xff0c…

使用新版FLIR (FLIR_ADAS_v2) 数据集创建yolo格式数据集(目标检测)

FLIR在2022.1.19发布了新版的FLIR_ADAS_v2&#xff0c;有着更多的类别和数量更丰富的图像。数据集同步注释热图像和无注释RGB图像供参考。本文章主要介绍如何使用FLIR_ADAS_v2中的rgb图像和thermal图像来制作yolo格式数据集。 1.官方数据集下载&#xff1a;FLIR_ADAS_v2数据集…

支持向量机(SVM)白话之个人理解(学习记录)

本文仅有文字理解部分&#xff0c;没有相应的数学公式推导过程&#xff0c;便于新手理解。 一、什么是支持向量机 首先我们看下面这张图&#xff0c;在图中圆形和三角形分别代表不同的数据类型&#xff0c;如何画出一条直线使两者能够显著地区分开来呢&#xff1f; 答案可以多…

PDF锐化

PDF Shaper Ultimate(pdf转图片) 编辑->添加文件->选中一个要处理的pdf 操作->转换->PDF转为图片 ComicEnhancerPro设置(把图片锐化) PDF Shaper Ultimate(图片转pdf) 编辑-添加图片->选中所有锐化处理后的图片 转换->图片转为pdf&#xff08;会把所有图…

通用CI/CD软件平台TeamCity 2024.03发布——支持HashiCorp Vault插件

TeamCity是一个通用的 CI/CD 软件平台&#xff0c;可以实现灵活的工作流、协作和开发做法。我们的解决方案将帮助在您的 DevOps 流程中成功实现持续集成、持续交付和持续部署。 获取TeamCity 2024.03正式版试用(Q技术交流&#xff1a;909157416&#xff09; 具体更新详情如下…

服务注册自治,降低 ASP.NET Core Web API 依赖注入的耦合度和复杂度

前言 在软件的实际开发中&#xff0c;一个软件通常由多个项目组成&#xff0c;这些项目都会直接或者间接被主 ASP.NET Core 项目引用。 这些项目中通常都会用到若干个被注入的服务&#xff0c;因此我们需要在主 ASP.NET Core 项目的 Program.cs 中注册这些服务。这样不仅会增…

ATAM方法架构评估实践

用ATAM方法评估软件体系结构&#xff0c;其工作分为4个基本阶段&#xff0c;即演示、调查和分析、测试和报告ATAM&#xff08;如图1所示&#xff09;。接下来分别就每个阶段的实践进行详细介绍。 图1 ATAM方法的评估实践阶段划分 1.阶段1——演示&#xff08;Presentation&…

esp32上PWM呼吸灯

1、什么是pwm PWM&#xff08;Pulse Width Modulation&#xff09;简称脉宽调制&#xff0c;是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术&#xff0c;广泛应用在测量、通信、工控等方面。 1.1频率 单位时间内PWM方波重复的次数 1.2占空比 一个周期内…