Set up Prometheus Server on AlmaLinux 9: Best Monitoring Service

Posted on

Set up Prometheus Server on AlmaLinux 9: Best Monitoring Service

This guide will walk you through the process of how to Set up Prometheus Server on AlmaLinux 9. Prometheus is a powerful open-source monitoring and alerting toolkit specifically designed for microservices architectures and containerized environments. Its strength lies in its flexible query language and real-time alerting capabilities.

Prometheus plays a crucial role in IT monitoring by providing real-time insights into the health and performance of your application program interfaces (APIs) and interconnected services. It can detect unusual traffic patterns that might indicate security breaches or system compromises. Furthermore, Prometheus metrics can be leveraged to track key business indicators, such as top-selling products, customer review trends, and order processing efficiency. This makes Prometheus a valuable asset for both technical and business teams.

The following steps, originally outlined on the Orcacore website, will guide you through the installation and configuration of Prometheus on your AlmaLinux 9 server, enabling you to access the Prometheus server URL dashboard.

Before proceeding, ensure that you are logged in to your AlmaLinux 9 server as a non-root user with sudo privileges and that you have a basic firewall configured. If you haven’t already done so, refer to the guide on Initial Server Setup with AlmaLinux 9 for detailed instructions.

1. Install Prometheus on AlmaLinux 9

Let’s dive into the installation process.

Create a Prometheus system group and user

First, update the local package index using the following command:

sudo dnf update -y

Next, create a dedicated system group and user for Prometheus to enhance security and proper resource management:

# sudo groupadd --system prometheus
# sudo useradd -s /sbin/nologin --system -g prometheus prometheus

Create data & configs directories for Prometheus

Create a directory for Prometheus to store its time-series data. This ensures data persistence and efficient retrieval.

sudo mkdir /var/lib/prometheus

Then, create the necessary configuration directories for Prometheus:

for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done

Download Prometheus

Visit the GitHub Prometheus release page to obtain the latest binary package. Download it to the /tmp/prometheus directory using the following curl command:

# sudo mkdir -p /tmp/prometheus
# sudo cd /tmp/prometheus
# curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -

After the download completes, extract the archive:

sudo tar xvf prometheus*.tar.gz

Navigate to the extracted Prometheus directory:

cd prometheus*/

Move the binary files to the /usr/local/bin/ directory, making them globally accessible:

sudo mv prometheus promtool /usr/local/bin/

Verify the installation by checking the Prometheus version:

prometheus --version
Verify Prometheus installation AlmaLinux 9

2. Configure Prometheus on AlmaLinux 9

Now, let’s configure Prometheus.

Move the Prometheus configuration template to the /etc directory:

sudo mv prometheus.yml /etc/prometheus/prometheus.yml

Move the consoles and console_libraries directories to /etc/prometheus/:

sudo mv consoles/ console_libraries/ /etc/prometheus/

Return to your home directory:

cd $HOME

Create a Prometheus systemd unit file

To manage Prometheus as a system service, create a systemd unit file:

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

Add the following content to the file:

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
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 
  --web.listen-address=0.0.0.0:9090 
  --web.external-url=

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target

Save and close the file.

Set Correct Directory Permissions

Change the ownership and permissions of the directories to the prometheus user and group:

# for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
# for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
# sudo chown -R prometheus:prometheus /var/lib/prometheus/

Start and Enable the Prometheus service

Start and enable the Prometheus service on AlmaLinux 9:

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

Verify that the Prometheus service is active and running:

sudo systemctl status prometheus

Configure Firewall for Prometheus

If your server has a running firewall service, open port 9090 to allow access to the Prometheus web interface:

sudo firewall-cmd --zone=public --permanent --add-port 9090/tcp

Reload the firewall to apply the changes:

sudo firewall-cmd --reload

Access Prometheus Server URL

Access the Prometheus server by navigating to the following URL in your web browser, replacing <server-ip-or-domain-name> with your server’s IP address or domain name:

http://<server-ip-or-domain-name>:9090
Access Prometheus Server URL

You have successfully completed the Set up Prometheus Server on AlmaLinux 9!

Conclusion

Prometheus is a powerful and flexible tool for monitoring modern systems. Its real-time data collection, detailed query capabilities, and alerting features make it essential for maintaining system stability and performance. You have successfully learned to Set up Prometheus Server on AlmaLinux 9.

Here are some additional articles you might find interesting:

Alternative Solutions for Setting Up Prometheus

While the above method provides a clear and concise way to Set up Prometheus Server on AlmaLinux 9, there are alternative approaches that might be more suitable depending on your specific needs and infrastructure. Here are two such alternatives:

1. Using Docker Compose:

Docker Compose allows you to define and manage multi-container Docker applications. Using Docker Compose to deploy Prometheus simplifies the process of setting up Prometheus along with other related services (like Grafana). This approach promotes reproducibility and portability.

Explanation:

  • Containerization: Docker encapsulates Prometheus and its dependencies into a container, isolating it from the host system and ensuring consistent behavior across different environments.
  • Orchestration: Docker Compose orchestrates the deployment and management of multiple containers, making it easy to define dependencies and configurations for Prometheus and other services.
  • Version Control: The docker-compose.yml file acts as infrastructure-as-code, allowing you to version control your Prometheus deployment configuration.

Code Example (docker-compose.yml):

version: "3.8"
services:
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    restart: always

volumes:
  prometheus_data:

Steps to Deploy:

  1. Create a docker-compose.yml file with the above content.
  2. Create a prometheus.yml configuration file (can start with the default, but customize it later).
  3. Run docker-compose up -d in the directory containing the docker-compose.yml file.

This will start the Prometheus container in detached mode. You can access the Prometheus web UI at http://localhost:9090 (or your server’s IP address). This method greatly simplifies Set up Prometheus Server on AlmaLinux 9.

2. Using Ansible for Automated Deployment:

Ansible is an automation engine that can be used to provision and configure infrastructure. Using Ansible, you can create a playbook to automate the entire process of installing and configuring Prometheus on AlmaLinux 9. This approach is ideal for managing multiple Prometheus instances across a large infrastructure.

Explanation:

  • Idempotency: Ansible playbooks are idempotent, meaning they can be run multiple times without changing the system state if the desired state is already achieved.
  • Scalability: Ansible allows you to easily deploy and manage Prometheus on multiple servers simultaneously.
  • Configuration Management: Ansible provides a centralized way to manage the configuration of Prometheus and other services.

Code Example (Ansible Playbook – prometheus_install.yml):

---
- hosts: prometheus_servers
  become: true
  tasks:
    - name: Update package cache
      dnf:
        update_cache: yes

    - name: Install required packages
      dnf:
        name:
          - wget
          - tar
        state: present

    - name: Create Prometheus group
      group:
        name: prometheus
        system: yes
        state: present

    - name: Create Prometheus user
      user:
        name: prometheus
        system: yes
        group: prometheus
        shell: /sbin/nologin
        createhome: no

    - name: Create Prometheus directories
      file:
        path: "{{ item }}"
        state: directory
        owner: prometheus
        group: prometheus
        mode: "0775"
      loop:
        - /var/lib/prometheus
        - /etc/prometheus
        - /etc/prometheus/rules
        - /etc/prometheus/rules.d
        - /etc/prometheus/files_sd

    - name: Download Prometheus
      get_url:
        url: "https://github.com/prometheus/prometheus/releases/download/v2.46.0/prometheus-2.46.0.linux-amd64.tar.gz" # Replace with the latest version
        dest: /tmp/prometheus.tar.gz

    - name: Extract Prometheus
      unarchive:
        src: /tmp/prometheus.tar.gz
        dest: /tmp
        creates: /tmp/prometheus-2.46.0.linux-amd64 # Adjust based on version

    - name: Copy Prometheus binaries
      copy:
        src: "/tmp/prometheus-2.46.0.linux-amd64/{{ item }}"  # Adjust based on version
        dest: /usr/local/bin/
        remote_src: yes
        mode: "0755"
      loop:
        - prometheus
        - promtool

    - name: Copy Prometheus configuration file
      copy:
        src: prometheus.yml  # Ensure this file exists in the same directory as the playbook
        dest: /etc/prometheus/prometheus.yml
        owner: prometheus
        group: prometheus
        mode: "0644"

    - name: Create Prometheus systemd service file
      copy:
        dest: /etc/systemd/system/prometheus.service
        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
          Restart=on-failure

          [Install]
          WantedBy=multi-user.target

    - name: Reload systemd daemon
      systemd:
        daemon_reload: yes

    - name: Enable and start Prometheus service
      systemd:
        name: prometheus
        enabled: yes
        state: started

    - name: Open Prometheus port in firewall
      firewalld:
        port: 9090/tcp
        permanent: yes
        state: enabled
        immediate: yes

Steps to Deploy:

  1. Install Ansible on your control machine.
  2. Create an inventory file (hosts) listing the Prometheus servers.
  3. Create the prometheus_install.yml playbook.
  4. Create a prometheus.yml configuration file (can start with the default, but customize it later).
  5. Run the playbook: ansible-playbook -i hosts prometheus_install.yml.

Remember to adjust the Prometheus version number in the playbook to the latest available. This approach offers a highly automated and scalable way to Set up Prometheus Server on AlmaLinux 9.