当前位置:首页 > Ubuntu > 正文

Ubuntu监控自动化部署(手把手教你用脚本一键搭建系统监控平台)

在现代运维工作中,Ubuntu监控自动化部署已成为保障服务器稳定运行的关键手段。本文将带你从零开始,使用 Shell 脚本自动部署一套轻量级但功能强大的监控系统,包含 Prometheus、Node Exporter 和 Grafana,让你轻松实现服务器健康检查与可视化。

Ubuntu监控自动化部署(手把手教你用脚本一键搭建系统监控平台) Ubuntu监控自动化部署 系统监控脚本 自动部署Prometheus 服务器健康检查 第1张

为什么需要自动化部署?

手动安装 Prometheus、Node Exporter 和 Grafana 不仅耗时,还容易出错。通过编写一个部署脚本,我们可以实现:

  • 一键安装所有监控组件
  • 自动配置服务开机自启
  • 统一管理配置文件
  • 适用于多台 Ubuntu 服务器批量部署

准备工作

确保你的 Ubuntu 系统(建议 20.04 或 22.04)满足以下条件:

  • 拥有 sudo 权限
  • 已安装 curl、wget、tar 等基础工具
  • 网络畅通(用于下载组件)

编写自动化部署脚本

我们将创建一个名为 deploy_monitor.sh 的脚本,它会自动完成以下任务:

  1. 安装 Node Exporter(用于采集系统指标)
  2. 安装 Prometheus(用于存储和查询指标)
  3. 安装 Grafana(用于数据可视化)

以下是完整的脚本内容:

#!/bin/bash# Ubuntu监控自动化部署脚本# 支持 Ubuntu 20.04 / 22.04set -e  # 遇错即停# 定义版本PROMETHEUS_VERSION="2.47.1"NODE_EXPORTER_VERSION="1.7.0"GRAFANA_VERSION="10.1.5"# 创建工作目录echo "[+] 创建监控部署目录..."mkdir -p /opt/monitoringcd /opt/monitoring# 安装 Node Exporterecho "[+] 安装 Node Exporter..."wget https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gztar xvfz node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gzmv node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter /usr/local/bin/rm -rf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64*# 创建 Node Exporter systemd 服务cat > /etc/systemd/system/node_exporter.service << EOF[Unit]Description=Node ExporterAfter=network.target[Service]User=rootExecStart=/usr/local/bin/node_exporterRestart=always[Install]WantedBy=multi-user.targetEOFsystemctl daemon-reloadsystemctl enable --now node_exporter# 安装 Prometheusecho "[+] 安装 Prometheus..."wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gztar xvfz prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gzmv prometheus-${PROMETHEUS_VERSION}.linux-amd64 /opt/prometheus# 简单配置 Prometheuscat > /opt/prometheus/prometheus.yml << EOFglobal:  scrape_interval: 15sscrape_configs:  - job_name: 'node'    static_configs:      - targets: ['localhost:9100']EOF# 创建 Prometheus systemd 服务cat > /etc/systemd/system/prometheus.service << EOF[Unit]Description=PrometheusAfter=network.target[Service]User=rootExecStart=/opt/prometheus/prometheus \  --config.file=/opt/prometheus/prometheus.yml \  --storage.tsdb.path=/opt/prometheus/dataRestart=always[Install]WantedBy=multi-user.targetEOFsystemctl daemon-reloadsystemctl enable --now prometheus# 安装 Grafanaecho "[+] 安装 Grafana..."apt-get updateapt-get install -y apt-transport-https software-properties-common wgetgpg_key_url="https://apt.grafana.com/gpg.key"wget -q -O - $gpg_key_url | gpg --dearmor | tee /usr/share/keyrings/grafana.gpg > /dev/nullecho "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://apt.grafana.com stable main" | tee /etc/apt/sources.list.d/grafana.listapt-get updateapt-get install -y grafanasystemctl enable --now grafana-server# 完成提示echo ""echo "✅ Ubuntu监控自动化部署完成!"echo "- Node Exporter: http://$(hostname -I | awk '{print $1}'):9100/metrics"echo "- Prometheus:     http://$(hostname -I | awk '{print $1}'):9090"echo "- Grafana:        http://$(hostname -I | awk '{print $1}'):3000 (默认账号 admin/admin)"echo ""

如何运行脚本?

将上述代码保存为 deploy_monitor.sh,然后在终端执行以下命令:

chmod +x deploy_monitor.shsudo ./deploy_monitor.sh

脚本运行完成后,你将获得一个完整的监控栈。首次登录 Grafana 时,系统会提示你修改默认密码。

验证部署结果

打开浏览器,依次访问以下地址:

  • Node Exporter:查看原始指标(端口 9100)
  • Prometheus:查询和存储指标(端口 9090)
  • Grafana:创建仪表盘(端口 3000)

在 Grafana 中添加 Prometheus 作为数据源(URL 填 http://localhost:9090),然后导入官方 Node Exporter Dashboard(ID: 1860),即可看到漂亮的服务器性能图表。

结语

通过这个简单的脚本,你已经实现了 自动部署Prometheus 监控体系的目标。这套方案不仅适用于个人项目,也适合中小企业的生产环境。后续你可以根据需求扩展告警规则(Alertmanager)、增加更多 Exporter(如 MySQL、Redis)等。

掌握 系统监控脚本 编写能力,是迈向 DevOps 自动化的重要一步。希望这篇教程能帮助你快速上手 Ubuntu 监控自动化部署!