首先安装Grafana,CentOS通过配置YUM仓库并执行yum install grafana -y,Ubuntu则添加GPG密钥和软件源后运行apt update && apt install grafana -y;接着启动服务并设置开机自启,命令为systemctl start grafana-server和systemctl enable grafana-server;然后通过浏览器访问http://服务器IP:3000,使用默认账号admin/admin登录并修改密码;随后添加Prometheus数据源,填写URL如http://localhost:9090,并点击“Save & test”验证连接;最后导入Node Exporter仪表盘模板(如ID 1860),确保Node Exporter已部署且Prometheus配置抓取任务,即可实时展示系统指标。

要在Linux上构建指标可视化平台,Grafana是一个非常流行且功能强大的选择。它支持多种数据源(如Prometheus、InfluxDB、MySQL等),能够实时展示系统性能、应用指标和业务数据。下面介绍如何在Linux系统中部署并配置Grafana,搭建完整的可视化平台。
安装Grafana
以主流Linux发行版CentOS/Ubuntu为例,介绍两种安装方式:
1. CentOS/RHEL 安装方法
添加Grafana的YUM仓库并安装:
- 配置仓库:创建 /etc/yum.repos.d/grafana.repo 文件,写入以下内容
- [grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt - 执行安装命令:yum install grafana -y
2. Ubuntu/Debian 安装方法
使用APT方式安装:
- 添加GPG密钥:wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
- 添加软件源:echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
- 更新并安装:apt update && apt install grafana -y
启动并配置Grafana服务
安装完成后,需要启动服务并设置开机自启:
- 启动Grafana:systemctl start grafana-server
- 设置开机启动:systemctl enable grafana-server
- 查看运行状态:systemctl status grafana-server
Grafana默认监听在 3000端口,可通过浏览器访问 http://服务器IP:3000 进入Web界面。
初始登录与基本配置
首次登录使用默认凭据:
- 用户名:admin
- 密码:admin
登录后会提示修改密码,按要求设置新密码即可。
进入主界面后,下一步是添加数据源。常见的搭配是使用Prometheus采集Linux系统指标:
- 点击左侧“Connections”图标 → “Data sources” → “Add data source”
- 选择 Prometheus
- 填写URL(如 http://localhost:9090)
- 点击“Save & test”确保连接成功
导入仪表盘展示指标
Grafana提供大量预设仪表盘模板,可快速展示CPU、内存、磁盘等系统指标。
例如导入Node Exporter监控面板:
- 访问 https://grafana.com/grafana/dashboards/ 搜索 “Node Exporter”
- 选择合适的ID(如 1860)
- 在Grafana界面点击“+” → “Import”,输入Dashboard ID
- 选择已配置的Prometheus数据源,导入即可
确保你已在目标机器部署Node Exporter,并在Prometheus中配置抓取任务,才能看到实际数据。
基本上就这些。通过Grafana结合Prometheus等组件,可以快速搭建一个功能完善的Linux指标可视化平台,帮助你实时掌握系统运行状态。部署过程不复杂,但需要注意防火墙、服务状态和数据源连通性。









