在现代IT运维中,系统监控是保障服务稳定运行的关键环节。本文将详细介绍如何在RockyLinux系统上安装和配置Prometheus——一款广受欢迎的开源监控工具Prometheus。无论你是刚接触Linux的新手,还是有一定经验的运维人员,都能通过本教程轻松完成部署。
一、什么是Prometheus?
Prometheus 是一个开源的系统监控和警报工具包,最初由 SoundCloud 开发,现为 CNCF(云原生计算基金会)项目之一。它支持多维数据模型、强大的查询语言 PromQL,并能与 Grafana 等可视化工具无缝集成。
二、准备工作
在开始之前,请确保你有一台已安装好 RockyLinux(建议 8 或 9 版本)的服务器,并具备以下条件:
拥有 sudo 权限的用户 系统已联网 防火墙开放 9090 端口(Prometheus 默认端口)三、安装Prometheus
我们将通过官方二进制包方式安装 Prometheus,这种方式简单且适用于大多数场景。
1. 创建专用用户
出于安全考虑,建议使用非 root 用户运行 Prometheus:
sudo useradd --no-create-home --shell /bin/false prometheus
2. 下载并解压Prometheus
访问 Prometheus 官网 获取最新版本链接,以下以 2.45.0 为例:
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gztar xvfz prometheus-2.45.0.linux-amd64.tar.gz
3. 创建目录并移动文件
sudo mkdir /etc/prometheus /var/lib/prometheussudo cp prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/sudo cp prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/sudo cp -r prometheus-2.45.0.linux-amd64/consoles /etc/prometheussudo cp -r prometheus-2.45.0.linux-amd64/console_libraries /etc/prometheus
4. 设置权限
sudo chown prometheus:prometheus /etc/prometheussudo chown prometheus:prometheus /var/lib/prometheus
四、配置Prometheus
Prometheus 的主配置文件为
prometheus.yml,我们将其放在
/etc/prometheus/目录下。
1. 编写配置文件
sudo tee /etc/prometheus/prometheus.yml <<EOFglobal: scrape_interval: 15sscrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node' static_configs: - targets: ['localhost:9100']EOF
说明:上述配置包含两个监控任务:
prometheus(监控自身)和
node(用于后续 Node Exporter 监控主机指标)。
2. 验证配置文件语法
promtool check config /etc/prometheus/prometheus.yml
如果输出 “Success”,说明配置无误。
五、创建systemd服务
为了方便管理,我们将 Prometheus 注册为系统服务:
sudo tee /etc/systemd/system/prometheus.service <<EOF[Unit]Description=PrometheusWants=network-online.targetAfter=network-online.target[Service]User=prometheusGroup=prometheusType=simpleExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries[Install]WantedBy=multi-user.targetEOF
3. 启动服务
sudo systemctl daemon-reexecsudo systemctl enable prometheussudo systemctl start prometheus
六、开放防火墙端口
sudo firewall-cmd --permanent --add-port=9090/tcpsudo firewall-cmd --reload
七、访问Prometheus Web界面
打开浏览器,访问
http://你的服务器IP:9090,即可看到 Prometheus 的 Web 控制台。你可以在此执行 PromQL 查询、查看目标状态等。
八、(可选)安装Node Exporter监控主机指标
要监控 CPU、内存、磁盘等系统资源,需安装 Node Exporter:
# 下载并安装wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gztar xvfz node_exporter-1.6.1.linux-amd64.tar.gzsudo cp node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/# 创建服务sudo tee /etc/systemd/system/node_exporter.service <<EOF[Unit]Description=Node ExporterWants=network-online.targetAfter=network-online.target[Service]User=prometheusExecStart=/usr/local/bin/node_exporter[Install]WantedBy=default.targetEOFsudo systemctl daemon-reexecsudo systemctl enable node_exportersudo systemctl start node_exporter
重启 Prometheus 后,即可在 Targets 页面看到
node任务处于 UP 状态。
九、总结
通过本教程,你已成功在 RockyLinux 上完成了 Prometheus安装配置,并初步实现了对系统的监控。Prometheus 作为强大的开源监控工具Prometheus,配合 Node Exporter 可全面掌握服务器运行状态。后续还可集成 Alertmanager 实现告警,或对接 Grafana 实现更美观的可视化。
希望这篇关于 RockyLinux Prometheus监控 的详细指南对你有所帮助!如有疑问,欢迎留言交流。
