5分钟部署Prometheus+Grafana批量监控Linux服务器

news/2024/9/19 14:34:04 标签: prometheus, grafana

给你送挂来了,宝~~

详细讲解如何在 Linux 服务器上使用 Docker 容器快速部署 Prometheus 和 Grafana 监控系统,同时通过 node_exporter 采集全面的系统性能数据。整个流程涵盖了从环境配置到搭建一个全面监控平台的每个步骤

1,一键安装Node Exporter

Node Exporter 是 Prometheus 生态系统中的一个关键组件,它专门用于收集和导出 Linux 系统的硬件和操作系统指标,如 CPU 使用率、内存利用率、磁盘 IO、网络统计等。这些数据可以帮助你深入了解服务器的性能表现,从而提高系统的监控和管理效率。

该服务所有需要监控的服务器安装,属于数据采集Agent。

下面是一键安装的脚本,脚本设置了国内加速

#!/bin/bash

# 定义变量
URL="https://mirror.ghproxy.com/https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz"
TAR_FILE="node_exporter-1.8.2.linux-amd64.tar.gz"
DIR_NAME="node_exporter-1.8.2.linux-amd64"
LISTEN_PORT="9100"

# 下载文件
echo "Downloading $TAR_FILE..."
wget -c $URL -O $TAR_FILE
if [ $? -ne 0 ]; then
  echo "Error: Failed to download $TAR_FILE."
  exit 1
fi

# 解压文件
echo "Extracting $TAR_FILE..."
tar -zxvf $TAR_FILE
if [ $? -ne 0 ]; then
  echo "Error: Failed to extract $TAR_FILE."
  exit 1
fi

# 进入解压后的目录
echo "Changing directory to $DIR_NAME..."
cd $DIR_NAME
if [ $? -ne 0 ]; then
  echo "Error: Failed to change directory to $DIR_NAME."
  exit 1
fi

# 后台运行 node_exporter
echo "Starting node_exporter on port $LISTEN_PORT..."
nohup ./node_exporter --web.listen-address=":$LISTEN_PORT" > node_exporter.stdout 2>&1 &
if [ $? -ne 0 ]; then
  echo "Error: Failed to start node_exporter."
  exit 1
fi

echo "node_exporter started successfully and is listening on port $LISTEN_PORT."

2,安装prometheus

创建数据存储目录

mkdir /data/prometheus_data && chmod 777 /data/prometheus_data

创建配置文件

将需要监控的节点和添加进配置文件

- job_name: "node_exporter"
    static_configs:
      - targets:
        - "192.168.1.12:9100"
        - "192.168.1.13:9100"
        - "192.168.1.14:9100"
        - "192.168.1.15:9100"
        - "192.168.1.3:9100"
        - "192.168.1.4:9100"
        - "192.168.1.5:9100"
        - "192.168.1.6:9100"
        - "192.168.1.7:9100"

完整的配置文件内容为:

# 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"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node_exporter"
    static_configs:
      - targets:
        - "192.168.1.12:9100"
        - "192.168.1.13:9100"
        - "192.168.1.14:9100"
        - "192.168.1.15:9100"
        - "192.168.1.3:9100"
        - "192.168.1.4:9100"
        - "192.168.1.5:9100"
        - "192.168.1.6:9100"
        - "192.168.1.7:9100"

tips:这里格式一定要对齐,否则可能会启动失败

设置配置文件权限

chmod 777 /etc/prometheus.yml

下载运行Prometheus

下载运行服务:

docker run -d \
  --name=prometheus \
  -p 9090:9090 \
  -v /etc/prometheus.yml:/etc/prometheus/prometheus.yml \
  -v /data/prometheus_data:/prometheus \
  --restart always \
  prom/prometheus

如果拉取不了可以用下面这个:

docker run -d \
  --name=prometheus \
  -p 9090:9090 \
  -v /etc/prometheus.yml:/etc/prometheus/prometheus.yml \
  -v /data/prometheus_data:/prometheus \
  --restart always \
  registry.cn-hangzhou.aliyuncs.com/jast-docker/prometheus:latest

访问:http://localhost:9090 验证是否启动生效

3,安装Grafana

创建数据目录

mkdir -p grafana/data

下载运行Grafana

docker run -d -p 3000:3000 --name=grafana \
  --user "$(id -u)" \  
  --volume "$PWD/grafana/data:/var/lib/grafana" \
  grafana/grafana  

上面的如果用不了,用下面的国内镜像

docker run -d -p 3000:3000 --name=grafana \
  --user "$(id -u)" \
  --restart always \
  --volume "$PWD/grafana/data:/var/lib/grafana" \
  registry.cn-hangzhou.aliyuncs.com/jast-docker/grafana:latest

运行完成访问: http://localhost:3000

配置Grafana监控Linux服务器

登录

默认账号密码admin/admin

首次登录后设置密码

添加数据源

选择prometheus

填写prometheus地址

最下方点击保存

导入模板

导入8189模板,官方提供的监控模板

输入名称和数据源导入

监控效果

到此监控已经配置完成,你也可以配置预警值,进行一些告警操作,第一时间发现问题。

稳啦稳啦!!!全都稳啦!!!


http://www.niftyadmin.cn/n/5665707.html

相关文章

自监督的主要学习方法

自监督学习是一种机器学习方法&#xff0c;其中模型从未标注的数据中学习生成标签&#xff0c;通常通过构造预训练任务或预测任务来从数据的内部结构中提取信息。它的核心目标是利用无监督的数据进行学习&#xff0c;从而在下游任务中更好地利用监督信号。自监督学习的主要方法…

53.【C语言】 字符函数和字符串函数(strcmp函数)

7.strcmp函数 *简单使用 cplusplus的介绍 点我跳转 strcmp:string compare 字符串比较 具体讲解见此文 点我跳转 *例题 求下列代码的执行结果 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() {char arr1[20] { &quo…

C++学习, 异常处理

C 提供了异常处理机制&#xff0c;允许程序在运行时检测和处理错误情况。这种机制通过 try、catch 和 throw 关键字来实现。当程序遇到异常情况时&#xff0c;它可以抛出一个异常&#xff0c;然后控制权转移到能够处理该异常的代码块。 基本概念 throw&#xff1a;用于抛出一…

Gitlab runner的简单使用(一)

Gitlab runner的简单使用&#xff08;一&#xff09; 使用 GitLab CI 配置文件在 main 分支提交时触发作业 GitLab CI/CD 提供了一种强大的方式来自动化软件开发过程&#xff0c;包括构建、测试和部署应用程序。在这篇文章中&#xff0c;我们将介绍如何通过 GitLab CI 配置文…

PDF转图片的思路思考

记录时间:2022年9月1日 PDF转图片库的使用和扩展 python有几个开源的免费的处理Pdf的库&#xff0c;甚至有的已经有很完善的功能了。我发挥一下自己的所学&#xff0c;看看能不能把它变为可用的一程序。 首先是了解PDF处理库PyMupdf&#xff0c;这个库得到路径之后普就可以对…

Vue.js 的 Mixins

Vue.js 的 Mixins 是一种非常强大且灵活的功能&#xff0c;它允许你封装可复用的 Vue 组件选项。Mixins 实际上是一种分发 Vue 组件可复用功能的非常灵活的方式。一个 mixin 对象可以包含任意组件选项。当组件使用 mixin 时&#xff0c;所有 mixin 选项将被“混入”该组件本身的…

解决nginx代理SSE接口的响应没有流式返回

目录 现象原来的nginx配置解决 现象 前后端分离的项目&#xff0c;前端访问被nginx反向代理的后端SSE接口&#xff0c;预期是流式返回&#xff0c;但经常是很久不响应&#xff0c;一响应全部结果一下子都返回了。查看后端项目的日志&#xff0c;响应其实是流式产生的。推测是n…

Xilinx系FPGA学习笔记(九)DDR3学习

系列文章目录 文章目录 系列文章目录前言DDR介绍DDR的IP核学习接口信号解析读写流程分析AXI 前言 这里暂时先只介绍一下IP核配置生成和一些接口信号的含义&#xff0c;后续还需要补很多知识点和实际测试应用 DDR介绍 DDR3 已不是当今主流的 DDR 存储器&#xff0c;市场上的 …