Prometheus+Grafana的安装和入门

news2024/9/23 3:19:03

概念

什么是Prometheus?

Prometheus受启发于Google的Brogmon监控系统(相似kubernetes是从Brog系统演变而来), 从
2012年开始由google工程师Soundclouds使用Go语言开发的开源监控报警系统和时序列数据库
(TSDB)。,并且与2015年早起对外发布早期版本。
2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将
Prometheus纳入其下第二大开源项目。

Prometheus的特点

多维度数据模型。
灵活的查询语言PromQL。
不依赖分布式存储,单个服务器节点是自主的。
通过基于HTTP的pull方式采集时序数据。
可以通过中间网关进行时序列数据推送。
通过服务发现或者静态配置来发现目标服务对象。
支持多种多样的图表和界面展示,比如Grafana等。

Prometheus组件

Prometheus Server 根据配置完成数据采集, 服务发现以及数据存储,提供PromQL查询语言的
支持。
Alertmanager 警告管理器,用来进行报警。
Exporters(探针): 是Prometheus的一类数据采集组件的总称。它负责从目标处搜集数据,并将
其转化为Prometheus支持的格式。与传统的数据采集组件不同的是,它并不向中央服务器发送数
据,而是等待中央服务器主动前来抓取。
Push Gateway 支持临时性Job主动推送指标的中间网关。

Prometheus原理

Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。不需要任何SDK或者其他的集成过程。这样做非常适合做虚拟化环境监控系统,比如VM、Docker、Kubernetes等。输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系统信息(包括磁盘、内存、CPU、网络等等)。

系统说明

三台centos7虚拟机
[root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)

角色主机名ip
Prometheus服务器server192.168.50.162
grafana服务器grafana192.168.50.163
被监控的linux主机agent192.168.50.164

环境准备
1.各自配置好主机名

hostnamectl set-hostname xxx

2.三台都互相绑定IP与主机名

192.168.50.162 server
192.168.50.163 agent
192.168.50.164 grafana

3.配置时间同步

yum install -y chronyd
vim /etc/chronyd.conf
#添加一下内容
server ntp1.aliyun.com iburst

重启服务
···

systemctl restart chronyd
[root@master prometheus]# chronyc sources

^* 120.25.115.20 2 7 40 879 -33us[ -261us] +/- 19ms
···

4.关闭防火墙和selinux

systemctl stop firewalld
systemctl disable firewalld
sed -i '/^SELINUX=/ cSELINUX=disabled' /etc/selinux/config
setenforce 0

Prometheus安装与使用

安装

下载软件
1.前往官网https://prometheus.io/download/,找到对应版本

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

2.解压软件

tar -xf prometheus-2.53.2.linux-amd64.tar.gz
#创建软连接
cd /usr/local
ln -sv prometheus-2.53.2.linux-amd64/ prometheus

3.创建数据目录

mkdir /usr/local/prometheus/data

4.创建服务运行用户

useradd prometheus -M -s /sbin/nologin
修改权限
chown -R prometheus.prometheus /usr/local/prometheus/*

5.创建服务运行脚本

vim /usr/lib/systemd/system/prometheus.service
[Unit]
cription=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus-2.53.2.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.53.2.linux-amd64/prometheus.yml --storage.tsdb.path=/usr/local/prometheus-2.53.2.linux-amd64/prometheus-2.53.2.linux-amd64/data
Restart=on-failure
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target

6.启动服务

systemctl daemon-reload
systemctl enable --now prometheus.service
检查服务
systemctl is-active prometheus.service
active
systemctl is-enabled prometheus.service
enabled
检查端口
[root@master prometheus]# netstat -lnupt | grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      77034/prometheus  

3.web界面
访问ip:9090即可进入web页面
在这里插入图片描述
在这里插入图片描述
默认只监视当前一台主机
此时点击http://master:9090/metrics会显示无法访问
在这里插入图片描述
将master改为主机ip即可

在这里插入图片描述

通过http://服务器IP:9090/metrics可以查看到监控的数据

[root@master prometheus]# curl http://192.168.50.162:9090/metrics
# HELP go_gc_cycles_automatic_gc_cycles_total Count of completed GC cycles generated by the Go runtime.
# TYPE go_gc_cycles_automatic_gc_cycles_total counter
go_gc_cycles_automatic_gc_cycles_total 11
# HELP go_gc_cycles_forced_gc_cycles_total Count of completed GC cycles forced by the application.
# TYPE go_gc_cycles_forced_gc_cycles_total counter
go_gc_cycles_forced_gc_cycles_total 0
# HELP go_gc_cycles_total_gc_cycles_total Count of all completed GC cycles.
# TYPE go_gc_cycles_total_gc_cycles_total counter
go_gc_cycles_total_gc_cycles_total 11
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.6722e-05
go_gc_duration_seconds{quantile="0.25"} 5.7989e-05
go_gc_duration_seconds{quantile="0.5"} 8.8906e-05
go_gc_duration_seconds{quantile="0.75"} 0.000240731
go_gc_duration_seconds{quantile="1"} 0.000904538
go_gc_duration_seconds_sum 0.002091826
go_gc_duration_seconds_count 11
# HELP go_gc_gogc_percent Heap size target percentage configured by the user, otherwise 100. This value is set by the GOGC environment variable, and the runtime/debug.SetGCPercent function.
# TYPE go_gc_gogc_percent gauge
go_gc_gogc_percent 75

查询数据
在这里插入图片描述

监控案例练习

1.监控linux主机

使用Node Exporter采集主机运行数据
在Prometheus的架构设计中, Prometheus Server并不直接服务监控特定的目标, 其主要任务负责数据的收集,存储并且对外提供数据查询支持。 因此为了能够能够监控到某些东西, 如主机的CPU使用率, 我们需要使用到Exporter。 Prometheus周期性的从Exporter暴露的HTTP服务地址( 通常
是/metrics) 拉取监控样本数据。
为了能够采集到主机的运行指标如CPU, 内存, 磁盘等信息。 可以使用Node Exporter。
Node Exporter同样采用Golang编写, 并且不存在任何的第三方依赖, 只需要下载, 解压即可运行。
在这里插入图片描述
在官网中找到,复制下载链接,在被监控的linux主机上下载Node Exporter

[root@node2 local]# wget -c https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

解压:

[root@node2 local]# tar -xf node_exporter-1.8.2.linux-amd64.tar.gz 
[root@node2 local]# ll
total 10428
drwxr-xr-x. 2 root root        6 Apr 10  2018 bin
drwxr-xr-x. 2 root root        6 Apr 10  2018 etc
drwxr-xr-x. 2 root root        6 Apr 10  2018 games
drwxr-xr-x. 2 root root        6 Apr 10  2018 include
drwxr-xr-x. 2 root root        6 Apr 10  2018 lib
drwxr-xr-x. 2 root root        6 Apr 10  2018 lib64
drwxr-xr-x. 2 root root        6 Apr 10  2018 libexec
drwxr-xr-x. 2 1001 1002       56 Jul 14 04:58 node_exporter-1.8.2.linux-amd64
-rw-r--r--. 1 root root 10676343 Jul 14 04:58 node_exporter-1.8.2.linux-amd64.tar.gz
drwxr-xr-x. 2 root root        6 Apr 10  2018 sbin
drwxr-xr-x. 5 root root       49 Aug 10 04:08 share
drwxr-xr-x. 2 root root        6 Apr 10  2018 src

创建服务运行用户

useradd prometheus -M -s /sbin/nologin

修改权限

chown -R prometheus.prometheus /usr/local/node_exporter-1.8.2.linux-amd64/*

添加系统服务

vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter-1.8.2.linux-amd64/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动服务:

systemctl daemon-reload
systemctl start node_exporter.service
[root@node2 local]# systemctl is-active node_exporter.service 
active
[root@node2 local]# netstat -lnupt | grep 9100
tcp6       0      0 :::9100                 :::*                    LISTEN      85999/node_exporter 

验证:
此时浏览器访问 http://被监控端IP:9100/metrics 就可以查看到node_exporter在被监控端收集的监控信息
在这里插入图片描述
要想在prumetheus server端的web界面中看到被监控端linux主机的数据,还要在Prometheus主配置文件中加入一下配置

[root@master prometheus]# vim /usr/local/prometheus/prometheus.yml
  - job_name: 'agent1'
    static_configs:
      - targets: ['192.168.50.164:9100']

在这里插入图片描述
重启服务

[root@master prometheus]# systemctl daemon-reload
[root@master prometheus]#  systemctl restart prometheus.service

在访问web页面即可看到被监控端主机
在这里插入图片描述

2.监控远程Mysql

依旧是去官网下载mysql_exporter组件
在这里插入图片描述
下载组件:

wget -c https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

解压组件:

tar -xf mysqld_exporter-0.15.1.linux-amd64

安装mariadb数据库,授权账户

yum install -y mariadb-server
systemctl enable --now mariadb

mysql -e "grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123'"

注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb获取数据,而是prometheus
服务器找mysql_exporter,mysql_exporter再找mariadb。所以这个localhost是指的
mysql_exporter的IP

创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名和密码要对应)

vim /usr/local/mysqld_exporter/.my.cnf

[client]
user=mysql_monitor
password=123

添加系统服务

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

[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter-0.15.1.linux-amd64/.my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动服务

[root@node2 local]# chown -R prometheus.prometheus /usr/local/mysqld_exporter/*
[root@node2 local]# systemctl daemon-reload
[root@node2 local]# systemctl start mysql_exporter.service
[root@node2 local]# netstat -lnupt | grep 9104
tcp6 0 0 :::9104 :::* LISTEN

验证:
通过浏览器访问http://被监控端IP:9104/metrics就可以查看到mysql_exporter在被监控端收集的监控信息 配置文件中可更改端口
在这里插入图片描述

修改Prometheus配置:

添加如下配置:
[root@master prometheus]# vim /usr/local/prometheus/prometheus.yml
- job_name: 'agent1_mariadb'
static_configs:
- targets: ['192.168.50.164:9104']
重启prometheus
[root@master prometheus]# systemctl restart prometheus.service

在这里插入图片描述

Prometheus web管理界面查看:
在这里插入图片描述

Grafana安装与使用

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展
示,并及时通知。它主要有以下六大特点:
1、展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;
2、数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
3、通知提醒:以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;
4、混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;
5、注释:使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记;
6、过滤器:Ad-hoc过滤器允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

安装

在grafana主机下载软件包
在这里插入图片描述

 yum install -y https://dl.grafana.com/oss/release/grafana-11.2.0-1.x86_64.rpm

由于下载速度过慢,因此去阿里镜像仓库下载
增加repo源:

sudo vim /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://mirrors.aliyun.com/grafana/yum/rpm
repo_gpgcheck=0
enabled=1
gpgcheck=0


##再执行
sudo yum makecache
sudo yum install grafana

如果再不行,就直接:

yum install -y https://mirrors.aliyun.com/grafana/yum/rpm/Packages/grafana-10.0.11-1.x86_64.rpm

启动服务:

[root@node1 local]# systemctl enable --now grafana-server
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@node1 local]# netstat -lnupt | grep 3000
tcp6       0      0 :::3000                 :::*                    LISTEN      17688/grafana   

使用grafana-cli 安装插件

# grafana-cli plugins list-remote 列出所有可用的插件
# grafana-cli plugins install grafana-worldmap-panel 安装世界地图插件
# grafana-cli plugins install grafana-clock-panel 安装时间插件
# grafana-cli plugins install grafana-piechart-panel 安装圆饼插件
# grafana-cli plugins ls 列出已经安装的插件
安装完毕需要重启服务
# systemctl restart grafana-server

·

使用Grafana连接Prometheus

1.登录web界面
通过浏览器访问 http:// grafana服务器IP:3000就到了登录界面,使用默认的admin用户,admin密码就可以登陆了 192.168.50.163:3000
第一次需要修改admin密码
在这里插入图片描述
2.添加数据源
在这里插入图片描述
点击Prometheus
在这里插入图片描述
填入Prometheus端的主机ip:9090 192.168.50.162:9090
在这里插入图片描述
为数据源挑选展示的模版

点击中间那个load,标错了

选择数据源之后,点击import
在这里插入图片描述
效果展示:
在这里插入图片描述

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

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

相关文章

2017年系统架构师案例分析试题一

目录 案例 【题目】 【问题 1】(12 分) 【问题 2】(13 分) 答案 【问题 1】答案 【问题 2】答案 相关推荐 案例 阅读以下关于软件架构评估的叙述,在答题纸上回答问题 1 和问题 2。 【题目】 某单位为了建设健全的公路桥梁养护管理档案,拟开发一套公…

USB PHY—— PHY 基础

芯片厂商开发了一些 USB PHY 芯片,可以把 DP、DM上的差模信号转成共模信号。 USB PHY 负责最底层的信号转换,作用类似于网口的 PHY。 USB 信号传输前,需要通过 PHY 把 USB 控制器的数字信号转成线缆上的模拟信号。USB 控制器和 PHY 之间的总…

中国严肃游戏开发的最佳实践

严肃游戏产业在中国迅速发展,将娱乐与教育、培训和宣传活动融为一体。旨在实现特定学习成果或行为改变的严肃游戏在从企业培训到医疗保健和教育的各个领域越来越受欢迎。然而,为中国市场开发成功的严肃游戏需要深入了解当地文化、用户偏好和技术趋势。以…

函数栈帧的创建和销毁(VS2022)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 目录 前言 一、前面的困惑 二、什么是函数栈帧 三、关于函数栈帧的基础知识 1.栈 2.寄存器 2.1 什么是寄存器 2.2 相关的寄存器 2.3 相关汇编命令 2.4 预备知识 四、解析函数…

盘点4款可以免费使用的高效ai PPT制作工具。

平时我们自己制作一个PPT还是需要比较长的时间的,从构思内容,到制作主题和逻辑框架,然后是挑选模板、排版配色等,过程比较繁琐且费时。但是,现在出现了很多的AIPPT制作工具,能够快速的帮助用户生成一个完整…

来自工业界的知识库 RAG(六),独特的 RAG 框架 dsRAG 核心亮点解读

背景介绍 在前面介绍了较多的开源 RAG 框架,比如主打 Rerank 的 QAnything, 主打精细文件解析的 RagFlow, 主打模块化灵活组合的 GoMate。这些库的设计除了少量的独特之处外,相似的部分很多。 最近有注意到一款另类的 RAG 框架 dsRAG,使用了…

openGauss在龙芯平台部署的实践

服务器环境 系统信息 NAME"Loongnix-Server Linux" VERSION"8" ID"loongnix-server" ID_LIKE"rhel fedora centos" VERSION_ID"8" PLATFORM_ID"platform:lns8" PRETTY_NAME"Loongnix-Server Linux 8"…

第四十篇-TeslaP40+Ollama+Ollama-WebUI(自编译)

本文介绍用自己编辑ollama-webui,链接本地ollama 环境 系统:CentOS-7 CPU: 14C28T 内存:32G 显卡:Tesla P40 24G 驱动: 535 CUDA: 12.2 Ollama: 0.3.0本地ollama 参考 [第二十四篇-Ollama-在线安装](https://blog.csdn.net/hai4321/articl…

2024软考:一场与“难”共舞的奇妙冒险,你值得拥有!

在这个时代,如果说有什么考试能让IT界的勇士们闻风丧胆,又爱又恨,那软考绝对能C位出道,成为众多技术大佬心中的“白月光”与“朱砂痣”。随着岁月悠悠,2024年的软考似乎又悄悄地在难度上动了点小心思,让人不…

vue设置水印

水印图例 1.新建Watermark.js 文件 const watermark {}const setWatermark (text, sourceBody) > {const id Math.random() * 10000 - Math.random() * 10000 / Math.random() * 10000if (document.getElementById(id) ! null) {document.body.removeChild(document.getE…

阿里云服务器开放端口的完整版图文教程

原文:阿里云服务器开放端口完整版教程:https://www.yundashi168.com/488.html 笔者近期开发完成的服务端程序部署在阿里云的ECS云服务器上面,一些应用程序配置文件需要设置监听的端口(如Tomcat的8080、443端口等)&…

萤石云 移动端demo指南

再来一篇,这次是萤石开放平台移动端demo的使用指南 一、Demo使用指南 从官网下载demo,下载地址demo首页如下: 填入对应参数 输入框是否必填解释服务器区域是国内选择Asia-China,海外选择对应的区域。选择后ApiUrl和WebUrl会自动…

餐饮行业eHR人力资源管理系统应该如何选择?

数字化转型与增长成为餐饮企业品牌竞争的创新壁垒,越来越多的餐饮企业(门店)依托数字化工具和手段,覆盖从内部组织到外部的数字化升级,包括员工管理、营销、客户管理,以及采购供应链等各环节的数字化运营。…

Netty从入门到超神-NIO 三大核心(selector,channel,buffer)(二)

前言 上一篇文章认识了一下Java的三大IO,这一章节我们详细了解一下NIO的工作原理以及三大核心Selector,Channel,Buffer并尝试来做一些小案例。 Java NIO 模型 Java NIO有三个核心的组件: selector 选择器 , channel 通道 , buf…

SpringBoot日常:Spring之@PostConstruct解析

简介 spring的Bean在创建的时候会进行初始化,而初始化过程会解析出PostConstruct注解的方法,并反射调用该方法。 PostConstruct 的使用和特点 只有一个非静态方法能使用此注解;被注解的方法不得有任何参数;被注解的方法返回值必…

Marin说PCB之TP测试的Layout设计要求

提及到TP点这个器件想必诸位道友们肯定不会陌生吧,我们的单板在量产之前都是需要做很多测试的,一般在产品研发的A版本和B版本的时候都是需要在单板上加上这个器件的。小编我最近在做一个改板,项目组为了降本增效,把单板的尺寸缩小…

Git 忽略已经提交的文件

对于未提交过的文件直接用ignore文件即可,不再赘述 对于已经提交过的文件,但是实际上不需要的,可以用git rm --cached命令 比如下图这个 .vsconfig被我误提交了或者忘了在ignore里添加了 但是我实际上不想要这个文件,那么在项目根目录打开git bash ,输入 git rm --cached .vsc…

LMDeploy 量化部署

创建环境和模型 conda create -n lmdeploy python3.10 -y conda activate lmdeploy conda install pytorch2.1.2 torchvision0.16.2 torchaudio2.1.2 pytorch-cuda12.1 -c pytorch -c nvidia -y pip install timm1.0.8 openai1.40.3 lmdeploy[all]0.5.3 mkdir /root/models …

[海思3403] 初始配置

虚拟机和板卡桥接 首先将虚拟机设置为桥接模式 板卡用网线和PC机连接,PC机用VMware打开Ubuntu虚拟机 点击虚拟网络编辑器,点击更改设置

爬取数据时,如何避免违法问题

目录 如何判断一个网站是否有明确禁止爬取数据? 如何处理爬取到的个人隐私数据以符合数据保护法规? 在爬取数据时,如何避免给目标网站带来过多的流量压力? 思维导图 在爬取数据时,避免违法问题的关键在于确保遵守相…