Install Prometheus Server on AlmaLinux 8 From CLI with Easy Steps

Posted on

Install Prometheus Server on AlmaLinux 8 From CLI with Easy Steps

Install Prometheus Server on AlmaLinux 8 From CLI with Easy Steps

This tutorial intends to teach you to Install Prometheus Server on AlmaLinux 8 From CLI. Also, you can use any RHEL 8 distro such as Rocky Linux 8 or Centos 8. Prometheus is used to monitor microservices and containers that provide flexible queries and real-time notifications.

In this guide on the Orcacore website, you will learn to download the latest Prometheus and install it on your AlmaLinux server step by step. This walkthrough provides clear and concise instructions to help you get your monitoring system up and running quickly. Monitoring is a crucial aspect of managing any server or application, and Prometheus offers a powerful and flexible solution.

To set up the Prometheus server on AlmaLinux, you need to have access to your server as a non-root user with sudo privileges and set up a basic firewall. For this purpose, you can check the Initial Server Setup with AlmaLinux 8. Ensuring you have these prerequisites in place will streamline the installation process and prevent common errors.

Now follow the steps below to complete your Prometheus Server setup. Learning how to Install Prometheus Server on AlmaLinux 8 From CLI has never been easier!

Step 1 – Create Prometheus System Group and User on AlmaLinux 8

First, you need to run the system update with the following command:

sudo dnf update -y

Then, you must create a system group and user for Prometheus. To do this, you can run the following commands in your Linux terminal:

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

These commands ensure that Prometheus runs under a dedicated user account with limited privileges, enhancing the security of your system.

Step 2 – Create Prometheus Data and Config Directories

At this point, you need to create a data directory for Prometheus to store its data and create a config directory for it. To do this, run the following commands:

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

The data directory will hold the time-series database, which stores the metrics collected by Prometheus. The config directory contains the prometheus.yml file, which defines the configuration of the Prometheus server.

Step 3 – Download the Latest Prometheus Binary Package from GitHub

Now you need to visit the GitHub Prometheus release page and download the latest binary package under /tmp/prometheus directory with 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 -

When your download is completed, extract it by using the command below:

sudo tar xvf prometheus*.tar.gz

Next, switch to your Prometheus directory:

cd prometheus*/

Move the binary files to /usr/local/bin/ directory:

sudo mv prometheus promtool /usr/local/bin/

Verify your Prometheus installation by checking its version on AlmaLinux 8:

prometheus --version
**Output**
prometheus, version 2.44.0 (branch: HEAD, revision: 1ac5131f698ebc60f13fe2727f89b115a41f6558)
  build user:       root@739e81
  build date:       20230514-06:18:11
  go version:       go1.20.4
  platform:         linux/amd64
  tags:             netgo,builtinassets,stringlabels

Confirming the version ensures that the installation was successful and that you have the expected version of Prometheus.

Step 4 – Prometheus Configuration on AlmaLinux 8

At this point, you need to move the Prometheus configuration template to the /etc directory with the following command:

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

Also, move consoles and console_libraries to the /etc/prometheus directory:

sudo mv consoles/ console_libraries/ /etc/prometheus/

Then, switch back to your home directory:

cd $HOME

The prometheus.yml file is the central configuration file for Prometheus, defining targets to scrape, rules for alerting, and other important settings.

Step 5 – Create Prometheus Systemd Unit File

To manage the Prometheus service with systemd, you need to create a systemd unit file for Prometheus. To do this, you can use your favorite text editor, we use the vi editor:

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

When you are done, save and close the file.

This systemd unit file allows you to easily start, stop, and manage the Prometheus service using systemctl.

Set Correct Prometheus Directory Permissions

At this point, you need to change the ownership of these directories to Prometheus user and group by using the following commands:

# 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/

Setting the correct permissions ensures that the Prometheus process has the necessary access to read and write data to these directories.

How To Start Prometheus Server?

Now you can start and enable Prometheus service on AlmaLinux 8 with the following commands:

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

Verify your Prometheus service is active and running on AlmaLinux 8:

sudo systemctl status prometheus
**Output**
● prometheus.service - Prometheus
   Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor pres>
   Active: **active** (**running**) since Wed 2023-06-07 07:07:06 EDT; 16s ago
     Docs: https://prometheus.io/docs/introduction/overview/
 Main PID: 48384 (prometheus)
    Tasks: 7 (limit: 23668)
   Memory: 17.3M
   CGroup: /system.slice/prometheus.service
           └─48384 /usr/local/bin/prometheus --config.file=/etc/prometheus/prom>
...

A status of "active (running)" confirms that Prometheus is successfully running as a service.

Step 6 – Configure Firewall For Prometheus

At this point, you have an active firewall, you must allow Prometheus port which is 9090 through the firewall. To do this, you can run the command below:

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

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Opening port 9090 in the firewall allows you to access the Prometheus web interface from other machines on your network.

Step 7 – How To Access Prometheus Server?

Now you can confirm that you can connect to port 9090 by accessing the Prometheus server IP address or domain name in your web browser:

http://<server-ip-or-domain-name>:9090
Prometheus server almalinux 8
Prometheus Server Setup

That’s it, you are done. With Prometheus, you can monitor application metrics like throughput (TPS) and response times. Also, you can use the Node exporter to monitor host hardware and kernel metrics. Successfully Install Prometheus Server on AlmaLinux 8 From CLI using this method.

Conclusion

Prometheus is a reliable tool for collecting and processing metrics from machines and applications. At this point, you have learned to download the latest Prometheus and install it on your AlmaLinux server step by step. You can use this instruction for any RHEL 8 distro like Rocky Linux 8. This knowledge will allow you to properly monitor your server and applications.

Hope you enjoy it. Please subscribe to us on Facebook and Twitter.

Also, you may like to read the following guides:

Discuss dnf vs yum

Python execute shell command

Alternative Installation Methods for Prometheus on AlmaLinux 8

While the above method demonstrates a manual installation of Prometheus, there are alternative approaches that can simplify the process or integrate with existing infrastructure. Here are two alternative ways to Install Prometheus Server on AlmaLinux 8 From CLI:

1. Using Docker Compose

Docker Compose allows you to define and manage multi-container Docker applications. This is a very useful method to Install Prometheus Server on AlmaLinux 8 From CLI. Using Docker Compose to deploy Prometheus offers several advantages, including simplified configuration, isolation from the host system, and easy reproducibility.

Explanation:

Docker Compose uses a docker-compose.yml file to define the services that make up your application. In this case, we’ll define a Prometheus service, specifying the image to use, the ports to expose, and the volumes to mount.

Code Example (docker-compose.yml):

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

volumes:
  prometheus_data:

Steps:

  1. Install Docker and Docker Compose: If you don’t have Docker and Docker Compose installed, you can install them using the following commands:

    sudo dnf install docker -y
    sudo systemctl start docker
    sudo systemctl enable docker
    
    sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
  2. Create a docker-compose.yml file: Create a file named docker-compose.yml in a directory of your choice and paste the above content into it.

  3. Create a prometheus.yml file: Create a prometheus.yml file in the same directory as your docker-compose.yml file. This file will contain your Prometheus configuration. A basic example could be:

    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']
  4. Start the Prometheus container: Navigate to the directory containing your docker-compose.yml file and run the following command:

    docker-compose up -d

This will download the Prometheus image and start the container in detached mode. You can then access the Prometheus web interface at http://<server-ip-or-domain-name>:9090.

2. Using Ansible

Ansible is an automation tool that allows you to define infrastructure as code. This is another very useful method to Install Prometheus Server on AlmaLinux 8 From CLI. Using Ansible to deploy Prometheus enables repeatable, consistent deployments across multiple servers.

Explanation:

An Ansible playbook defines the steps required to install and configure Prometheus on a target server. This playbook can be executed repeatedly to ensure that the server is always in the desired state.

Code Example (prometheus_install.yml):

---
- hosts: prometheus_server
  become: true
  tasks:
    - name: Create Prometheus group
      group:
        name: prometheus
        system: yes
        state: present

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

    - name: Create directories
      file:
        path: "{{ item }}"
        state: directory
        owner: prometheus
        group: prometheus
        mode: "0755"
      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.44.0/prometheus-2.44.0.linux-amd64.tar.gz" # Replace with latest version
        dest: /tmp/prometheus.tar.gz

    - name: Extract Prometheus
      unarchive:
        src: /tmp/prometheus.tar.gz
        dest: /tmp
        creates: /tmp/prometheus-2.44.0.linux-amd64

    - name: Copy Prometheus binaries
      copy:
        src: "/tmp/prometheus-2.44.0.linux-amd64/{{ item }}"
        dest: /usr/local/bin/
        owner: prometheus
        group: prometheus
        mode: "0755"
        remote_src: yes
      loop:
        - prometheus
        - promtool

    - name: Copy Prometheus configuration
      copy:
        src: files/prometheus.yml
        dest: /etc/prometheus/prometheus.yml
        owner: prometheus
        group: prometheus
        mode: "0644"

    - name: Create systemd unit file
      copy:
        src: files/prometheus.service
        dest: /etc/systemd/system/prometheus.service
        owner: root
        group: root
        mode: "0644"
      notify: Restart Prometheus

    - name: Reload systemd
      systemd:
        name: prometheus
        daemon_reload: yes
        state: restarted
        enabled: yes

  handlers:
    - name: Restart Prometheus
      systemd:
        name: prometheus
        state: restarted

Steps:

  1. Install Ansible: If you don’t have Ansible installed, you can install it using the following command:

    sudo dnf install ansible -y
  2. Create an Ansible inventory file: Create an inventory file (e.g., hosts) that lists the target server(s) where you want to install Prometheus.

    [prometheus_server]
    your_server_ip ansible_user=your_user ansible_ssh_private_key_file=~/.ssh/id_rsa

    Replace your_server_ip, your_user, and ~/.ssh/id_rsa with your server’s IP address, your username, and the path to your SSH private key, respectively.

  3. Create prometheus.yml and prometheus.service files: Create a directory named files in the same directory as your playbook. Place the prometheus.yml configuration file and the prometheus.service systemd unit file (similar to the content in the original article) inside the files directory.

  4. Run the Ansible playbook: Navigate to the directory containing your playbook and run the following command:

    ansible-playbook -i hosts prometheus_install.yml

This will execute the playbook and install Prometheus on the target server(s).

These alternative methods offer different approaches to deploying Prometheus, each with its own advantages and disadvantages. Choosing the right method depends on your specific needs and existing infrastructure.

Leave a Reply

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