当前位置:首页 > 系统教程 > 正文

Ubuntu系统安装Prometheus与Grafana监控平台(附Shell脚本一键部署教程)

Ubuntu系统安装Prometheus与Grafana监控平台(附Shell脚本一键部署教程)

详细步骤从零开始,小白也能轻松上手

Ubuntu系统安装Prometheus与Grafana监控平台(附Shell脚本一键部署教程) Ubuntu Prometheus Grafana 一键部署 第1张

一、介绍

在本教程中,我们将详细介绍如何在Ubuntu系统上安装和配置PrometheusGrafana,这两个开源工具是构建监控系统的强大组合。此外,我们还将提供一个一键部署的Shell脚本,让您能够快速搭建监控平台。无论您是初学者还是有经验的管理员,本教程都将引导您完成整个过程。

二、准备工作

确保您拥有一台运行Ubuntu系统的服务器(建议Ubuntu 18.04或更高版本),并具有sudo权限。首先,更新系统包列表以获取最新软件信息。

      sudo apt updatesudo apt upgrade -y    

这个步骤确保系统环境最新,为安装PrometheusGrafana打下基础。

三、安装Prometheus

1. 下载Prometheus:访问官方下载页面获取最新版本,或使用以下命令下载版本2.30.3(请根据最新版本调整)。

      wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz    

2. 解压文件:使用tar命令解压下载的压缩包。

      tar xvf prometheus-2.30.3.linux-amd64.tar.gz    

3. 移动文件:将解压后的目录移动到系统目录,例如/usr/local/prometheus。

      sudo mv prometheus-2.30.3.linux-amd64 /usr/local/prometheus    

4. 创建用户和配置:为安全起见,创建一个专用用户来运行Prometheus,并设置权限。这部分将在一键部署脚本中自动化处理。

四、安装Grafana

1. 添加Grafana仓库并安装:Grafana提供了APT仓库,便于在Ubuntu上安装。

      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 -y grafana    

2. 启动Grafana服务:安装完成后,启动并启用服务以开机自启。

      sudo systemctl start grafana-serversudo systemctl enable grafana-server    

现在,Grafana已经运行在端口3000上,您可以通过浏览器访问。

五、配置Prometheus数据源

登录Grafana(默认账号admin/admin,密码可在首次登录后更改),在数据源中添加Prometheus。URL设置为http://localhost:9090,然后保存。这样,Grafana就能从Prometheus获取监控数据了。

六、一键部署Shell脚本

为了简化过程,我们提供了一个一键部署Shell脚本,自动化所有安装和配置步骤。将以下脚本保存为install_monitoring.sh,并运行bash install_monitoring.sh。

      #!/bin/bash# 更新系统sudo apt updatesudo apt upgrade -y# 安装PrometheusPROM_VERSION="2.30.3"wget 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 /usr/local/prometheussudo useradd --no-create-home --shell /bin/false prometheussudo chown -R prometheus:prometheus /usr/local/prometheus# 创建Prometheus服务文件sudo cat > /etc/systemd/system/prometheus.service << EOF[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusType=simpleExecStart=/usr/local/prometheus/prometheus \n    --config.file /usr/local/prometheus/prometheus.yml \n    --storage.tsdb.path /usr/local/prometheus/data[Install]WantedBy=multi-user.targetEOFsudo systemctl daemon-reloadsudo systemctl start prometheussudo systemctl enable prometheus# 安装Grafanasudo 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 -y grafanasudo systemctl start grafana-serversudo systemctl enable grafana-serverecho "安装完成!Prometheus运行在9090端口,Grafana运行在3000端口。"    

运行脚本后,Ubuntu系统上的PrometheusGrafana监控平台将自动搭建完成。您可以直接使用Grafana创建仪表盘,实现一键部署的便捷体验。

七、总结

通过本教程,您学会了在Ubuntu系统上安装和配置PrometheusGrafana监控平台。使用提供的一键部署脚本,您可以快速搭建一个功能强大的监控系统,实时监控服务器和应用程序性能。希望这个教程对您有帮助,祝您搭建顺利!