Prometheus, an open-source, lightweight monitoring system with a powerful alerting mechanism, has gained significant popularity in the IT world. Its flexibility and scalability make it an ideal choice for monitoring system health and performance. In this guide, we will walk you through the step-by-step process of installing and configuring Prometheus on a Linux server.
Prerequisites
Before diving into the installation, ensure that the following prerequisites are met:
- Sudo Privileges: Administrative privileges are required to perform installation and configuration tasks.
- Internet Access: The server must have internet access to download Prometheus binaries.
- Firewall Configuration: Ensure that port
9090is open to access Prometheus’s web interface.
Step 1: Installing Prometheus Binary Files
Update Package Repositories
Before downloading Prometheus, update the package repositories to ensure all packages are up to date:
sudo yum update -yDownload Prometheus Binary
Use the following command to download the latest Prometheus binary:
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.22.0/prometheus-2.22.0.linux-amd64.tar.gzExtract and Rename Files
Extract the downloaded archive and rename the folder for convenience:
tar -xvf prometheus-2.22.0.linux-amd64.tar.gz
mv prometheus-2.22.0.linux-amd64 prometheus-filesStep 2: Setting Up Prometheus User and Directories
Create Prometheus User
Create a dedicated system user for running Prometheus:
sudo useradd --no-create-home --shell /bin/false prometheusCreate Necessary Directories
Set up the required directories for Prometheus configuration and data storage:
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheusSet the appropriate ownership for these directories:
sudo chown prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheusStep 3: Moving Prometheus Binary Files
Move Prometheus Binaries
Move the prometheus and promtool binaries to a system-wide location:
sudo cp prometheus-files/prometheus /usr/local/bin/
sudo cp prometheus-files/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtoolMove Console Files
Copy Prometheus console files to the appropriate directories:
sudo cp -r prometheus-files/consoles /etc/prometheus
sudo cp -r prometheus-files/console_libraries /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_librariesStep 4: Configuring Prometheus
The Prometheus configuration file defines how the application behaves, which metrics to scrape, and how often.
Create prometheus.yml
Create the main configuration file at /etc/prometheus/prometheus.yml:
sudo vi /etc/prometheus/prometheus.ymlAdd the following content:
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']Set ownership of the configuration file:
sudo chown prometheus:prometheus /etc/prometheus/prometheus.ymlStep 5: Creating a Systemd Service for Prometheus
To ensure Prometheus runs as a service, create a systemd unit file.
Create the Service File
Open a new service file for Prometheus:
sudo vi /etc/systemd/system/prometheus.serviceAdd the following content:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/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.targetReload Systemd and Start Prometheus
Reload the systemd manager configuration and start Prometheus:
sudo systemctl daemon-reload
sudo systemctl start prometheusVerify that the service is running:
sudo systemctl status prometheusEnable the service to start on boot:
sudo systemctl enable prometheusStep 6: Accessing the Prometheus Web Interface
Prometheus’s web interface can be accessed via port 9090. Open a web browser and navigate to:
http://<your-server-ip>:9090/graphHere, you can query existing metrics and visualize data. The default configuration scrapes Prometheus’s own metrics.
Step 7: Adding Additional Monitoring Targets
Prometheus can monitor multiple targets. To add more targets, edit the prometheus.yml file:
scrape_configs:
- job_name: 'my-app'
static_configs:
- targets: ['my-app-server:8080']Restart Prometheus to apply changes:
sudo systemctl restart prometheusConclusion
In this guide, we have covered the complete process of installing and configuring Prometheus on a Linux server. Prometheus is a powerful tool for monitoring system health, gathering metrics, and triggering alerts. Its flexibility allows you to monitor various services and applications by simply adjusting the configuration file.
By following these steps, you’ve set up a robust monitoring solution for your systems. Remember to keep Prometheus updated and secure access to its web interface for optimal performance and security.