一个详细的教程,帮助小白在Ubuntu系统上轻松部署监控系统,使用Prometheus和Grafana实现服务器性能监控。
在当今的IT运维中,监控系统至关重要,它能帮助我们实时了解服务器状态、性能指标和故障预警。Prometheus是一个开源的监控和警报工具包,而Grafana则是一个强大的数据可视化平台。本文将指导您在Ubuntu系统上安装和配置这两个工具,并提供一个一键部署的Shell脚本,简化整个过程。
以下是手动安装的详细步骤,如果您想快速部署,可以直接跳到“一键部署Shell脚本”部分。
首先,更新系统软件包列表并升级现有软件,以确保系统最新。这是安装任何软件前的标准操作。
sudo apt updatesudo apt upgrade -y Prometheus是一个流行的监控工具,用于收集和存储时间序列数据。我们将从官方GitHub发布页面下载并安装它。
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gztar xvfz prometheus-2.30.3.linux-amd64.tar.gzsudo mv prometheus-2.30.3.linux-amd64 /opt/prometheus 接下来,创建一个系统服务来管理Prometheus,以便它可以随系统启动。
sudo tee /etc/systemd/system/prometheus.service << EOF[Unit]Description=PrometheusAfter=network.target[Service]User=prometheusGroup=prometheusExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.ymlRestart=always[Install]WantedBy=multi-user.targetEOF 创建专用用户并设置权限:
sudo useradd --no-create-home --shell /bin/false prometheussudo chown -R prometheus:prometheus /opt/prometheus Grafana是一个数据可视化工具,可以将Prometheus收集的数据以图表形式展示。我们将通过官方仓库安装它。
sudo apt 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 updatesudo apt install -y grafana 启动Prometheus和Grafana服务,并设置它们开机自启。
sudo systemctl daemon-reloadsudo systemctl start prometheussudo systemctl enable prometheussudo systemctl start grafana-serversudo systemctl enable grafana-server 验证服务状态:
sudo systemctl status prometheussudo systemctl status grafana-server 如果服务运行正常,您可以通过浏览器访问Prometheus(http://服务器IP:9090)和Grafana(http://服务器IP:3000)。默认Grafana登录用户名和密码都是admin,首次登录后会提示修改密码。
为了简化安装过程,我编写了一个Shell脚本,可以自动完成所有步骤。只需在Ubuntu系统上运行此脚本即可。
#!/bin/bashsudo apt updatesudo apt upgrade -ywget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gztar xvfz prometheus-2.30.3.linux-amd64.tar.gzsudo mv prometheus-2.30.3.linux-amd64 /opt/prometheussudo tee /etc/systemd/system/prometheus.service << EOF[Unit]Description=PrometheusAfter=network.target[Service]User=prometheusGroup=prometheusExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.ymlRestart=always[Install]WantedBy=multi-user.targetEOFsudo useradd --no-create-home --shell /bin/false prometheussudo chown -R prometheus:prometheus /opt/prometheussudo apt 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 updatesudo apt install -y grafanasudo systemctl daemon-reloadsudo systemctl start prometheussudo systemctl enable prometheussudo systemctl start grafana-serversudo systemctl enable grafana-serverecho "Prometheus安装完成,访问地址:http://localhost:9090"echo "Grafana安装完成,访问地址:http://localhost:3000"echo "默认Grafana用户名:admin,密码:admin" 使用方法:将上述脚本保存为install_prometheus_grafana.sh,然后赋予执行权限并运行:
chmod +x install_prometheus_grafana.shsudo ./install_prometheus_grafana.sh 安装完成后,您需要配置Prometheus来监控目标(例如本地节点或远程服务器),并在Grafana中添加Prometheus作为数据源。具体操作可参考官方文档,但基本步骤是:
/opt/prometheus/prometheus.yml中定义监控目标。通过这个监控系统,您可以实时跟踪Ubuntu服务器的性能指标,如CPU使用率、内存消耗和磁盘空间,从而提升运维效率。
本文详细介绍了在Ubuntu系统上安装Prometheus和Grafana的步骤,并提供了一个一键部署Shell脚本,即使是新手也能轻松完成。这两个工具的结合为服务器监控提供了强大的解决方案。如果您遇到问题,请检查服务状态日志(使用sudo journalctl -u prometheus或sudo journalctl -u grafana-server),或参考官方社区。祝您部署顺利!
本文由主机测评网于2026-01-06发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:http://www.vpshk.cn/20260115471.html