Prometheus+grafana监控

1.什么是Prometheus

Prometheus是一款监控系统,可以监控传统业务(tomcat,nginx,mysql,redis,elasticsearch),还能够监控云原生业务,比如docker,k8s监控。
Promethues官方链接:https://prometheus.io/docs/introduction/overview/#architecture
推荐阅读:

  1. 云原生CNCF官网: https://landscape.cncf.io/
  2. Prometheus的GitHub地址: https://github.com/prometheus/prometheus
  3. Prometheus的官网地址: https://prometheus.io/

1.1 prometheus架构图

image

2.部署Prometheus server

2.1 手动部署

1)下载解压软件包

[root@Promethues11 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.53.2/prometheus-2.53.2.linux-amd64.tar.gz
[root@Promethues11 ~]# tar xf prometheus-2.53.2.linux-amd64.tar.gz -C /Project/softwares/

2)启动Prometheus serve

[root@Promethues11 ~]# cd /Project/softwares/prometheus-2.53.2.linux-amd64/
[root@Promethues11 prometheus-2.53.2.linux-amd64]# 
[root@Promethues11 prometheus-2.53.2.linux-amd64]# ll
total 261348
drwxr-xr-x 4 1001  127      4096 Aug  9 23:16 ./
drwxr-xr-x 7 root root      4096 Sep  4 09:42 ../
drwxr-xr-x 2 1001  127      4096 Aug  9 23:13 console_libraries/
drwxr-xr-x 2 1001  127      4096 Aug  9 23:13 consoles/
-rw-r--r-- 1 1001  127     11357 Aug  9 23:13 LICENSE
-rw-r--r-- 1 1001  127      3773 Aug  9 23:13 NOTICE
-rwxr-xr-x 1 1001  127 137838575 Aug  9 22:56 prometheus*
-rw-r--r-- 1 1001  127       934 Aug  9 23:13 prometheus.yml
-rwxr-xr-x 1 1001  127 129735160 Aug  9 22:56 promtool*
[root@Promethues11 prometheus-2.53.2.linux-amd64]# ./prometheus 

3)访问Prometheus的WebUI

http://10.0.0.11:9090/targets
【如果需要docker部署,可用梯子去看即可 https://hub.docker.com/r/prom/prometheus/tags】

2.2 自动部署

1)编写Prometheus server一键部署脚本

[root@Promethues11 ~]# ll
....
-rwxr-xr-x  1 root root      2682 Sep  6 03:37 install-prometheus-server.sh*
-rw-r--r--  1 root root 104212702 Sep  6 03:07 prometheus-2.53.2.linux-amd64.tar.gz
....
[root@Promethues11 ~]# ./install-prometheus-server.sh i

3.被监控节点安装node—export

3.1 一键部署node-export

[root@Promethues11 ~]# ll 
-rwxr-xr-x  1 root root      2044 Aug 15 02:35 install-node-exporter.sh*
-rw-r--r-- 1 root root  10676343 Aug 15 02:28 node_exporter-1.8.2.linux-amd64.tar.gz
[root@Promethues11 ~]# ./install-node-exporter.sh i

3.2 访问WebUI验证 【自己服务器的节点公网IP+端口号】

http://10.0.0.11:9100/metrics
http://10.0.0.12:9100/metrics
http://10.0.0.13:9100/metrics

效果如下
image

4.Prometheus监控Linux服务器

4.1 修改配置文件

[root@Promethues11 ~]# vim /oldboyedu/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml 
...
global:
scrape_interval: 3s 
  ...
  scrape_configs:
	  ...
   - job_name: "oldboyedu-elk"
     static_configs:
     - targets: ["10.0.0.11:9100","10.0.0.12:9100","10.0.0.13:9100"]

4.2 检查配置文件语法

[root@Promethues11 ~]# /Project/softwares/prometheus-2.53.2.linux-amd64/promtool  check config /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml 
Checking /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
 SUCCESS: /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml is valid prometheus config file syntax
[root@Promethues11 ~]# 

4.3 重新加载配置

[root@Promethues11 ~]# curl -X POST http://10.0.0.11:9090/-/reload

4.4 访问WebUI验证

http://10.0.0.11:9090/targets

4.5 由于检查语法和重新加载的语法有点长可以配置相关语法【可选】

[root@Promethues11 ~]# vim ~/.bashrc 
...
 alias yy='egrep -v "^.*#|^$"'
 alias check='/Project/softwares/prometheus-2.53.2.linux-amd64/promtool  check config /Project/softwares/prometheus-2.53.2.linux    -amd64/prometheus.yml' 
 alias rr='curl -X POST http://10.0.0.11:9090/-/reload' 
...
[root@Promethues11 ~]# 
[root@Promethues11 ~]# source ~/.bashrc
[root@Promethues11 ~]# 
[root@Promethues11 ~]# check 
Checking /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
 SUCCESS: /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml is valid prometheus config file syntax

[root@Promethues11 ~]# 
[root@Promethues11 ~]# rr
[root@Promethues11 ~]# 

5.Prometheus metrics type

prometheus监控中采集过来的数据统一称为Metrics数据,其并不是代表具体的数据格式,而是一种统计度量计算单位。当我们需要为某个系统或者某个服务做监控时,就需要使用到metrics。
prometheus支持的metrics包括但不限于以下几种数据类型:

  1. guage:最简单的度量指标,只是一个简单的返回值,或者叫瞬时状态;比如说统计硬盘,内存等使用情况。
  2. couter:就是一个计数器,从数据量0开始累积计算,在理想情况下,只能是永远的增长,不会降低(有特殊情况,比如粉丝量);比如统计1小时,1天,1周,1一个月的用户访问量,这就是一个累加的操作。
  3. histograms:是统计数据的分布情况,比如最小值,最大值,中间值,中位数等,代表的是一种近似百分比估算数值;通过histograms可以分别统计处在一个时间段(1s,2s,5s,10s)内nginx访问用户的响应时间。
  4. summary:summary是histograms的扩展类型,主要弥补histograms不足。

6.Prometheus常用的函数

  1. increase函数:用来针对counter数据类型,截取其中一段时间总的增量。
  2. sum函数:加和的作用。
  3. by函数:将数据进行分组,类似于MySQL的"GROUP BY"。
  4. rate函数:它的功能是按照设置的时间段,取counter在这个时间段中平均每秒的增量。
  5. topk函数:取前几位的最高值,实际使用的时候一般会用该函数进行瞬时报警,而不是为了观察曲线图。
  6. count函数:把数值符合条件的,输出数目进行累加加和

7.grafana环境部署

7.1 grafana概述

一个款图形化的工具,可以查询zabbix,es,Prometheus等多种数据源进行展示。

7.2安装grafana

#安装grafana依赖包
[root@Promethues11 ~]#apt-get install -y adduser libfontconfig1 musl
#下载grafana
[root@Promethues11 ~]#wget https://dl.grafana.com/enterprise/release/grafana-enterprise_9.5.21_amd64.deb
#安装grafana
[root@Promethues11 ~]# dpkg -i grafana-enterprise_9.5.21_amd64.deb
#配置开机自启动
[root@Promethues11 ~]# systemctl enable --now grafana-server 
[root@Promethues11 ~]#
[root@Promethues11 ~]# ss -ntl | grep 3000
LISTEN 0      4096                    *:3000             *:*          
[root@Promethues11 ~]# 

7.3 访问grafana的WebUI 【自己服务器的公网IP+端口号】

http://10.0.0.11:3000/

7.4 配置Prometheus数据源并展示Linux的监控指标

1)配置数据源

image

2)导入模板1860

image

3)数据展示

image

posted @ 2024-10-13 15:31  Nolen_H  阅读(105)  评论(0)    收藏  举报