IT评测·应用市场-qidao123.com

标题: 5分钟部署Prometheus+Grafana批量监控Linux服务器 [打印本页]

作者: 商道如狼道    时间: 2024-9-23 15:10
标题: 5分钟部署Prometheus+Grafana批量监控Linux服务器
给你送挂来了,宝~~
详细讲解如安在 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
创建设置文件
将需要监控的节点和添加进设置文件
  1. - job_name: "node_exporter"
  2.     static_configs:
  3.       - targets:
  4.         - "192.168.1.12:9100"
  5.         - "192.168.1.13:9100"
  6.         - "192.168.1.14:9100"
  7.         - "192.168.1.15:9100"
  8.         - "192.168.1.3:9100"
  9.         - "192.168.1.4:9100"
  10.         - "192.168.1.5:9100"
  11.         - "192.168.1.6:9100"
  12.         - "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模板,官方提供的监控模板

输入名称和数据源导入

监控效果

到此监控已经设置完成,你也可以设置预警值,举行一些告警操作,第一时间发现问题。
稳啦稳啦!!!全都稳啦!!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4