在现代运维工作中,Ubuntu监控自动化部署已成为保障服务器稳定运行的关键手段。本文将带你从零开始,使用 Shell 脚本自动部署一套轻量级但功能强大的监控系统,包含 Prometheus、Node Exporter 和 Grafana,让你轻松实现服务器健康检查与可视化。
手动安装 Prometheus、Node Exporter 和 Grafana 不仅耗时,还容易出错。通过编写一个部署脚本,我们可以实现:
确保你的 Ubuntu 系统(建议 20.04 或 22.04)满足以下条件:
我们将创建一个名为 deploy_monitor.sh 的脚本,它会自动完成以下任务:
以下是完整的脚本内容:
#!/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 时,系统会提示你修改默认密码。
打开浏览器,依次访问以下地址:
在 Grafana 中添加 Prometheus 作为数据源(URL 填 http://localhost:9090),然后导入官方 Node Exporter Dashboard(ID: 1860),即可看到漂亮的服务器性能图表。
通过这个简单的脚本,你已经实现了 自动部署Prometheus 监控体系的目标。这套方案不仅适用于个人项目,也适合中小企业的生产环境。后续你可以根据需求扩展告警规则(Alertmanager)、增加更多 Exporter(如 MySQL、Redis)等。
掌握 系统监控脚本 编写能力,是迈向 DevOps 自动化的重要一步。希望这篇教程能帮助你快速上手 Ubuntu 监控自动化部署!
本文由主机测评网于2025-12-05发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025123104.html