在当今的IT运维中,Ubuntu监控是不可或缺的一环。Prometheus作为领先的开源监控系统,配合Grafana强大的可视化能力,能够帮助你全面掌握系统状态。本文将带你一步步完成Prometheus安装和Grafana可视化配置,并提供一个一键部署脚本,实现自动化搭建。
首先,访问Prometheus官网下载最新版本,或使用wget命令:
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gztar xvf prometheus-2.45.0.linux-amd64.tar.gzsudo mv prometheus-2.45.0.linux-amd64 /opt/prometheus
然后,创建prometheus用户并设置权限:
sudo useradd --no-create-home --shell /bin/false prometheussudo chown -R prometheus:prometheus /opt/prometheus
接下来,配置Prometheus作为系统服务。创建/etc/systemd/system/prometheus.service文件,内容如下:
[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusType=simpleExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data[Install]WantedBy=multi-user.target
启动Prometheus:
sudo systemctl daemon-reloadsudo systemctl enable prometheussudo systemctl start prometheus
Grafana提供可视化界面,可以从官方仓库安装:
sudo apt-get install -y software-properties-commonsudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -sudo apt-get updatesudo apt-get install grafana
启动Grafana服务:
sudo systemctl enable grafana-serversudo systemctl start grafana-server
此时,你可以通过浏览器访问http://服务器IP:3000,默认用户名/密码为admin/admin。
为了方便,我们编写一个shell脚本,将上述步骤自动化。脚本内容如下:
#!/bin/bash# Ubuntu系统一键安装Prometheus+Grafanaset -e# 定义变量PROM_VERSION="2.45.0"GRAFANA_REPO="deb https://packages.grafana.com/oss/deb stable main"# 更新系统sudo apt-get update# 安装依赖sudo apt-get install -y wget curl software-properties-common# 下载并安装Prometheuswget https://github.com/prometheus/prometheus/releases/download/v${PROM_VERSION}/prometheus-${PROM_VERSION}.linux-amd64.tar.gztar xvf prometheus-${PROM_VERSION}.linux-amd64.tar.gzsudo mv prometheus-${PROM_VERSION}.linux-amd64 /opt/prometheussudo useradd --no-create-home --shell /bin/false prometheus || truesudo chown -R prometheus:prometheus /opt/prometheus# 创建systemd服务cat << EOF | sudo tee /etc/systemd/system/prometheus.service[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusType=simpleExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data[Install]WantedBy=multi-user.targetEOFsudo systemctl daemon-reloadsudo systemctl enable prometheussudo systemctl start prometheus# 安装Grafanasudo add-apt-repository -y "$GRAFANA_REPO"wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -sudo apt-get updatesudo apt-get install -y grafanasudo systemctl enable grafana-serversudo systemctl start grafana-serverecho "安装完成!Prometheus运行在端口9090,Grafana运行在端口3000。" 将以上脚本保存为install.sh,赋予执行权限并运行:
chmod +x install.shsudo ./install.sh
安装完成后,检查服务状态:
sudo systemctl status prometheussudo systemctl status grafana-server
访问Prometheus UI:http://你的IP:9090,访问Grafana:http://你的IP:3000,登录后添加Prometheus数据源,即可开始监控你的Ubuntu系统。
总结:通过本文的详细步骤或一键部署脚本,你可以在Ubuntu上快速搭建Prometheus+Grafana监控栈,实现对系统和应用的全面监控。关键词:Ubuntu监控、Prometheus安装、Grafana可视化、一键部署脚本。
本文由主机测评网于2026-02-21发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/20260226322.html