Installation of Prometheus and Grafana

ยท

3 min read

Installation of Prometheus and Grafana

Introduction

Monitoring and visualizing metrics are crucial for maintaining the health and performance of any application infrastructure. Prometheus and Grafana are popular tools for this purpose. Prometheus is an open-source systems monitoring and alerting toolkit, while Grafana is an open-source platform for monitoring and observability that supports querying, visualizing, alerting, and understanding metrics.

This article will guide you through installing and setting up Prometheus and Grafana on a Linux system using systemd for managing the services.

Prerequisites ๐Ÿ“‹

  1. A Linux-based system

  2. Root or sudo access to the system.

  3. Basic knowledge of the Linux command line.

Step 1: Install Prometheus ๐Ÿ› ๏ธ

1.1. Download Prometheus ๐Ÿ“ฅ

First, download the latest version of Prometheus from the official website or use the following command to download it directly:

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz

1.2. Extract the Archive ๐Ÿ“‚

Extract the downloaded tarball:

tar -xvzf prometheus-2.39.0.linux-amd64.tar.gz

1.3. Move Prometheus Binaries ๐Ÿ—‚๏ธ

Move the extracted files to the appropriate directories:

sudo mv prometheus-2.53.0.linux-amd64 /usr/local/bin/prometheus
sudo mv prometheus-2.53.0.linux-amd64/promtool /usr/local/bin/
sudo mv prometheus-2.53.0.linux-amd64/prometheus.yml /etc/prometheus/
sudo mv prometheus-2.53.0.linux-amd64/consoles /etc/prometheus/
sudo mv prometheus-2.53.0.linux-amd64/console_libraries /etc/prometheus/

1.4. Create a Prometheus User ๐Ÿ‘ค

Create a system user for Prometheus:

sudo useradd --no-create-home --shell /bin/false prometheus

Change ownership of Prometheus files to the Prometheus user:

sudo chown -R prometheus:prometheus /usr/local/bin/prometheus
sudo chown -R prometheus:prometheus /usr/local/bin/promtool
sudo chown -R prometheus:prometheus /etc/prometheus/

Step 2: Install Grafana ๐Ÿ“Š

2.1. Add Grafana Repository ๐Ÿ“œ

Add the Grafana APT repository to your system:

sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

2.2. Install Grafana ๐Ÿ’พ

Update the package list and install Grafana:

sudo apt-get update
sudo apt-get install -y grafana

Step 3: Create Systemd Service Files โš™๏ธ

3.1. Prometheus Systemd Service

Create a systemd service file for Prometheus:

sudo nano /etc/systemd/system/prometheus.service

Add the following content:

[Unit]
Description=Prometheus Monitoring
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/data

[Install]
WantedBy=multi-user.target

Save and close the file.

3.2. Grafana Systemd Service

The Grafana installation already provides a systemd service file. Enable the Grafana service:

sudo systemctl enable grafana-server

Step 4: Start and Enable Services ๐Ÿš€

4.1. Start Prometheus

Reload systemd to apply the changes and start Prometheus:

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

Check the status to ensure Prometheus is running:

sudo systemctl status prometheus

4.2. Start Grafana

Start the Grafana service:

sudo systemctl start grafana-server
sudo systemctl enable grafana-server

Check the status to ensure Grafana is running:

sudo systemctl status grafana-server

Step 5: Access Prometheus and Grafana ๐ŸŒ

5.1. Access Prometheus

Prometheus should now be accessible via your web browser at http://<your-server-ip>:9090.

5.2. Access Grafana

Grafana should be accessible at http://<your-server-ip>:3000. The default login credentials are:

  • Username: admin

  • Password: admin

You will be prompted to change the password upon first login.

Step 6: Configure Grafana to Use Prometheus ๐ŸŽ›๏ธ

  1. Login to Grafana: Open your browser and go to http://<your-server-ip>:3000.

  2. Add Data Source:

    • Go to Configuration > Data Sources.

    • Click Add data source.

    • Select Prometheus.

    • In the HTTP section, set the URL to http://localhost:9090.

    • Click Save & Test to verify the connection.

  3. Create Dashboards:

    • Go to Dashboards > Manage > New Dashboard.

    • Add panels and use Prometheus queries to visualize your metrics.

Conclusion

Setting up Prometheus and Grafana using systemd ensures that these services start automatically on boot and are managed efficiently. Prometheus helps you monitor your applications and infrastructure, while Grafana provides powerful visualization capabilities. By following this guide, you have installed and configured both Prometheus and Grafana, laying the foundation for robust monitoring and observability in your environment. ๐ŸŒŸ

ย