Easy Steps To Configure Prometheus on Ubuntu 22.04 – OrcaCore

Posted on

Easy Steps To Configure Prometheus on Ubuntu 22.04 - OrcaCore

Easy Steps To Configure Prometheus on Ubuntu 22.04 – OrcaCore

In this guide, we aim to demonstrate how to Install and Configure Prometheus on Ubuntu 22.04. Prometheus is a powerful open-source monitoring and alerting toolkit designed for cloud-native environments, particularly those leveraging Kubernetes. It excels at collecting and storing metrics as time-series data, where each data point is associated with a timestamp. Prometheus also supports the use of labels, which are optional key-value pairs that provide additional context and granularity to the collected metrics.

Now, let’s follow the steps outlined below on the Orcacore website to effectively Install and Configure Prometheus on Ubuntu 22.04.

Before we begin, to Install and Configure Prometheus on Ubuntu 22.04, ensure you’re logged into your Ubuntu server as a non-root user with sudo privileges and that a basic firewall is in place. If needed, you can refer to this guide on Initial Server Setup with Ubuntu 22.04.

1. Set up Prometheus on Ubuntu 22.04

Now, let’s dive into the process of installing Prometheus on your Ubuntu server.

Create Prometheus system group and user

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

sudo apt update

Next, create a dedicated system group and user for Prometheus using these commands:

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

Create data & configs directories for Prometheus

Prometheus needs dedicated directories to store its data and configuration files. Create the data directory using:

sudo mkdir /var/lib/prometheus

Then, create the necessary configuration directories with this command:

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 find 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 -

Once the download is complete, extract the archive:

sudo tar xvf prometheus*.tar.gz

Change to the extracted Prometheus directory:

cd prometheus*/

Move the prometheus and promtool binaries to the /usr/local/bin/ directory:

sudo mv prometheus promtool /usr/local/bin/

Verify the installation by checking the Prometheus version:

prometheus --version
**Output**
prometheus, version 2.43.0 (branch: HEAD, revision: edfc3bcd025dd6fe296c167a14a216cab1e552ee)
  build user:       root@8a0ee342e592
  build date:       20230321-12:56:07
  go version:       go1.19.7
  platform:         linux/amd64
  tags:             netgo,builtinassets

2. Configure Prometheus on Ubuntu 22.04

Now, move the default Prometheus configuration file to the /etc/prometheus/ directory:

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

Also, 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 service using systemd, create a 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 of the Prometheus 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 Prometheus service

Start and enable the Prometheus service on Ubuntu 22.04:

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

Verify the service status:

sudo systemctl status prometheus
**Output**
● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor pr>
     Active: **active** (**running**) since Wed 2023-04-19 10:06:49 UTC; 48s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 2348 (prometheus)
      Tasks: 8 (limit: 4575)
     Memory: 17.8M
        CPU: 214ms
     CGroup: /system.slice/prometheus.service
             └─2348 /usr/local/bin/prometheus --config.file=/etc/prometheus/pro>

Configure Firewall for Prometheus

If the firewall is enabled, allow traffic on port 9090:

sudo ufw allow 9090/tcp

Reload the firewall to apply the changes:

sudo ufw reload

Access Prometheus Server

Access the Prometheus web interface in your browser using the server’s IP address or domain name and port 9090:

http://server-ip-or-domain-name:9090

[Image of Prometheus server running on Ubuntu 22.04]

With these steps, you’ve successfully installed and configured Prometheus.

Conclusion

The Prometheus server is a powerful open-source solution tailored for monitoring and alerting in contemporary systems. Following this guide, you’ve successfully learned how to Install and Configure Prometheus on Ubuntu 22.04.

Hopefully, this guide was helpful. You might also be interested in these articles:

Install Grafana on Ubuntu 22.04

Install Elasticsearch on Ubuntu 22.04

Alternative Solutions for Prometheus Installation

While the above guide provides a comprehensive approach to installing and configuring Prometheus, alternative methods exist. Here are two different approaches:

1. Using Docker Compose

Docker Compose provides a convenient way to define and manage multi-container Docker applications. You can use Docker Compose to quickly deploy Prometheus and its dependencies. This is particularly useful for testing or running Prometheus in a containerized environment.

Explanation:

This method involves creating a docker-compose.yml file that defines the Prometheus container, its configuration, and any necessary volumes for persistent storage. This approach simplifies deployment and management, as all configurations are centralized in a single file. It’s highly portable and reproducible across different environments.

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: always

volumes:
  prometheus_data:

Steps to Deploy:

  1. Create a directory for your Prometheus setup.
  2. Create the docker-compose.yml file in that directory.
  3. Create a prometheus.yml file in the same directory with your desired Prometheus configuration (or use a default one).
  4. Run docker-compose up -d in the directory containing the docker-compose.yml file.

This will start the Prometheus container in detached mode, making it accessible at http://localhost:9090. This method of deploying prometheus helps to Install and Configure Prometheus on Ubuntu 22.04 more effeciently.

2. Using Ansible

Ansible is an automation tool that can be used to provision and configure systems. By creating an Ansible playbook, you can automate the entire process of installing and configuring Prometheus on Ubuntu 22.04.

Explanation:

Ansible playbooks define the desired state of a system. They consist of tasks that perform specific actions, such as installing packages, creating directories, and modifying configuration files. Using Ansible ensures that the Prometheus installation is consistent and repeatable across multiple servers.

Code Example (Ansible Playbook prometheus_install.yml):

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

    - 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
        state: present

    - name: Create 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.43.0/prometheus-2.43.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.43.0.linux-amd64 # Replace with the latest version
        owner: prometheus
        group: prometheus
        remote_src: yes

    - name: Copy binaries
      copy:
        src: "/tmp/prometheus-2.43.0.linux-amd64/{{ item }}"  # Replace with the latest version
        dest: /usr/local/bin/
        remote_src: yes
        mode: '0755'
      loop:
        - prometheus
        - promtool

    - name: Copy configuration file
      copy:
        src: prometheus.yml
        dest: /etc/prometheus/prometheus.yml
        owner: prometheus
        group: prometheus
        mode: '0644'

    - name: Create systemd service file
      template:
        src: prometheus.service.j2
        dest: /etc/systemd/system/prometheus.service
        owner: root
        group: root
        mode: '0644'
      notify: Restart Prometheus

    - name: Reload systemd
      systemd:
        daemon_reload: yes

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

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

Steps to Use:

  1. Create an Ansible inventory file (hosts) defining the target servers.
  2. Create the prometheus_install.yml playbook.
  3. Create a prometheus.yml file with your desired Prometheus configuration.
  4. Create a Jinja2 template for the systemd service file (prometheus.service.j2).
[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
  1. Run the playbook: ansible-playbook -i hosts prometheus_install.yml

These alternative methods provide flexibility in how you Install and Configure Prometheus on Ubuntu 22.04, depending on your infrastructure and automation preferences. The Ansible approach is particularly valuable for deploying Prometheus across many servers in a consistent manner.