Posted in

Unleashing the Power of Monitoring: Installing Prometheus and Grafana on Ubuntu 24.04

Unleashing the Power of Monitoring: Installing Prometheus and Grafana on Ubuntu 24.04
Unleashing the Power of Monitoring: Installing Prometheus and Grafana on Ubuntu 24.04

In today’s dynamic IT environments, robust monitoring is not just a luxury; it’s a necessity. Understanding the health and performance of your servers and applications in real-time is crucial for proactive problem-solving, optimizing resource usage, and ensuring business continuity. This guide will walk you through installing and configuring two of the most popular open-source monitoring tools, Prometheus and Grafana, on your Ubuntu 24.04 server.

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. It collects and stores its metrics as time series data, i.e., metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels. Grafana, on the other hand, is an open-source analytics and interactive visualization web application. It provides beautiful dashboards and graphs for your metrics, allowing you to query, visualize, alert on, and explore your data no matter where it’s stored. Together, they form a powerful monitoring stack.

Before diving into the installation, it’s always a good practice to ensure your server is well-maintained and optimized. You might want to consider steps like [How to Set Up Automatic Security Updates on Ubuntu 24.04] for continuous protection or [How to Set Up a Swap File on Ubuntu 24.04] to enhance system performance, especially on resource-intensive monitoring setups. Ensuring your server has a stable network identity is also crucial for monitoring; learn [How to Configure Static IP Address on Ubuntu 24.04] if your server requires a fixed IP.

**Step 1: Update Your Ubuntu 24.04 System**

First, ensure your system’s package list is up-to-date:

“`bash
sudo apt update
sudo apt upgrade -y
“`

**Step 2: Install Prometheus**

Prometheus is available as a pre-compiled binary. We’ll download it directly.

1. **Create a Prometheus System User:**
It’s best practice to run Prometheus under its own dedicated user.
“`bash
sudo useradd –no-create-home –shell /bin/false prometheus
“`

2. **Create Directories for Prometheus:**
“`bash
sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
“`

3. **Download and Extract Prometheus:**
Check the [Prometheus downloads page](https://prometheus.io/download/) for the latest stable version. Replace `VERSION` with the current version (e.g., `2.47.0`) and `ARCH` with your system architecture (e.g., `linux-amd64`).
“`bash
wget https://github.com/prometheus/prometheus/releases/download/vVERSION/prometheus-VERSION.ARCH.tar.gz
tar xvfz prometheus-VERSION.ARCH.tar.gz
sudo mv prometheus-VERSION.ARCH/prometheus /usr/local/bin/
sudo mv prometheus-VERSION.ARCH/promtool /usr/local/bin/
sudo mv prometheus-VERSION.ARCH/consoles /etc/prometheus
sudo mv prometheus-VERSION.ARCH/console_libraries /etc/prometheus
sudo rm -rf prometheus-VERSION.ARCH prometheus-VERSION.ARCH.tar.gz
“`

4. **Set Ownership and Permissions:**
“`bash
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool
sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
sudo chown -R prometheus:prometheus /var/lib/prometheus
“`

5. **Configure Prometheus:**
Create a Basic configuration file `/etc/prometheus/prometheus.yml`:
“`bash
sudo nano /etc/prometheus/prometheus.yml
“`
Add the following content:
“`yaml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds.

scrape_configs:
– job_name: ‘prometheus’
static_configs:
– targets: [‘localhost:9090’]
– job_name: ‘node_exporter’
static_configs:
– targets: [‘localhost:9100’] # Assuming you’ll install Node Exporter later
“`
Set ownership for the config file:
“`bash
sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml
“`

6. **Create a Systemd Service File for Prometheus:**
“`bash
sudo nano /etc/systemd/system/prometheus.service
“`
Add the following:
“`ini
[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
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
“`

7. **Reload Systemd, Start, and Enable Prometheus:**
“`bash
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
sudo systemctl status prometheus
“`
Verify Prometheus is running by navigating to `http://YOUR_SERVER_IP:9090` in your web browser.

**Step 3: Install Grafana**

1. **Install Necessary Packages:**
“`bash
sudo apt install -y apt-transport-https software-properties-common wget
“`

2. **Add Grafana GPG Key:**
“`bash
sudo mkdir -p /etc/apt/keyrings/
wget -q -O – https://apt.grafana.com/gpg.key | gpg –dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
“`

3. **Add Grafana Repository:**
“`bash
echo “deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main” | sudo tee -a /etc/apt/sources.list.d/grafana.list
“`

4. **Update APT Cache and Install Grafana:**
“`bash
sudo apt update
sudo apt install grafana -y
“`

5. **Start and Enable Grafana:**
“`bash
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
sudo systemctl status grafana-server
“`

6. **Access Grafana Web UI:**
Open your web browser and navigate to `http://YOUR_SERVER_IP:3000`. The default login credentials are `admin` for both username and password. You will be prompted to change the password on your first login.

**Step 4: Integrate Prometheus with Grafana**

1. **Add Prometheus as a Data Source in Grafana:**
* Log in to Grafana.
* Click on the gear icon (Configuration) on the left sidebar, then select `Data sources`.
* Click `Add data source`.
* Search for and select `Prometheus`.
* In the `HTTP` section, set the `URL` to `http://localhost:9090` (or your Prometheus server IP and port).
* Scroll down and click `Save & test`. You should see a message indicating the data source is working.

2. **Import a Sample Prometheus Dashboard (Optional but Recommended):**
* Click on the `+` icon on the left sidebar, then select `Import`.
* You can find many pre-built dashboards on the [Grafana Dashboards](https://grafana.com/grafana/dashboards/) website. For a basic Prometheus overview, try dashboard ID `1860`.
* Enter the ID `1860` in the `Import via grafana.com` field and click `Load`.
* Select your Prometheus data source from the dropdown and click `Import`.
* You should now see a dashboard displaying metrics from your Prometheus server.

For managing background tasks, such as regularly backing up your Prometheus data or scheduling Grafana report generation, understanding [How to Manage Cron Jobs with Crontab on Linux] can be incredibly useful. If you’re considering a more comprehensive real-time monitoring solution or an alternative to complement Prometheus and Grafana, you might also explore [How to Install Netdata for Real-Time Server Monitoring on Linux]. Furthermore, for automated deployment of such complex stacks, consider using configuration management tools. Check out [How to Install Ansible on Ubuntu 24.04 for automation] to streamline your setup process.

With Prometheus collecting your metrics and Grafana providing insightful visualizations, you now have a powerful monitoring solution for your Ubuntu 24.04 server. This setup provides the foundation for understanding your system’s behavior, identifying bottlenecks, and ensuring the reliability of your services. Happy monitoring!

Zac Morgan is a DevOps engineer and system administrator with over a decade of hands-on experience managing Linux and Windows infrastructure. Passionate about automation, cloud technologies, and sharing knowledge with the tech community. When not writing tutorials or configuring servers, you can find Zac exploring new tools, contributing to open-source projects, or helping others solve complex technical challenges.

Leave a Reply

Your email address will not be published. Required fields are marked *