Building RPM Packages for Prometheus Node Exporter Binaries on RHEL Systems
Standardizing binary distributions in to native system packages reduces manual overhead and simplifies lifecycle management. This workflow demonstrates packaging the Prometheus node_exporter executable for Rocky Linux 9, replacing tedious extraction steps with automated installation procedures.
Environment: OS: Rocky Linux 9.2 Binary Asset: node_exporter-1.7.0.linux-amd64.tar.gz
Prerequisites
Install the necessary development utilities to generate RPM artifacts.
dnf install rpm-build rpmdevtools -y
Directory Initialization
Run the setup command to create the standard build tree within your user home directory.
rpmdev-setuptree
The resulting hierarchy includes directories for source files, specifications, and built binaries:
~/rpmbuild/
├── SOURCES # Source archives and patches
├── SPECS # Package definition files
└── RPMS # Compiled binary packages
Asset Preparation
Move the compiled archive into the sources directory. Create a corresponding systemd unit file to ensure service integration upon installation.
~/rpmbuild/SOURCES/
├── node_exporter-1.7.0.linux-amd64.tar.gz
└── exporter.service
Systemd Unit Definition (exporter.service):
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
Specification File Creation
Define the package logic in ~/rpmbuild/SPECS/node_exporter.spec. This text-based recipe controls how files are staged, installed, and verified.
Name: node_exporter
Version: 1.7.0
Release: 1%{?dist}
Summary: Hardware metrics collector
License: ASL 2.0
URL: https://prometheus.io/
Source0: %{name}-%{version}.linux-amd64.tar.gz
Requires: libc.so.6
%description
Provides hardware and OS metrics exposed by *NIX kernels.
%prep
# Extract archive quietly to build directory
%setup -q -n %{name}-%{version}.linux-amd64
%install
# Copy binary to execution path
mkdir -p %{buildroot}/usr/local/bin
install -p -m 755 node_exporter %{buildroot}/usr/local/bin/node_exporter
# Configure systemd service
mkdir -p %{buildroot}/usr/lib/systemd/system
install -m 644 %{_sourcedir}/exporter.service %{buildroot}/usr/lib/systemd/system/exporter.service
%files
/usr/local/bin/node_exporter
/usr/lib/systemd/system/exporter.service
%changelog
* Mon Apr 20 2024 System Admin - 1.7.0-1
- Initial RPM build for RHEL-based systems
Build Execution
Compile the specification into a binary RPM using the -ba flag (build all).
rpmbuild -ba ~/rpmbuild/SPECS/node_exporter.spec
Deployment
Locate the ganerated RPM in RPMS/x86_64. Deploy using the distribution manager.
dnf install ~/rpmbuild/RPMS/x86_64/node_exporter-1.7.0-1.el9.x86_64.rpm
systemctl start node_exporter
Networked environments automatically fetch required library dependencies. For disconnected workstations, pre-resolve these requirements before transfer.
Offline Dependency Handling
Inspect the package without installing to reveal missing libraries:
dnf install node_exporter-1.7.0-1.el9.x86_64.rpm --show-duplicates
Collect dependencies into a temporary repository folder:
dnf download --resolve --downloaddir=/repo/packages node_exporter-1.7.0-1.el9.x86_64.rpm
Transfer both the package and dependencies to the target host. Execute a local transaction from the directory containing all RPM files:
cd /repo/packages
dnf localinstall ./*.rpm
Alternatively, mount this directory as a custom repository to enable direct installs from the local cache.
Key Specification Directives
- Metadata: Fields like
VersionandReleasedefine identity. - Sources:
Source0references the input archive. - Staging:
%prepunpacks data;%installplaces files into the roootfs for packaging. - Manifest:
%filesexplicitly declares included artifacts. - Logging:
%changelogtracks version history.