Wednesday, January 19, 2022

Prometheus Setup on Linux Server

What is  Prometheus ?

Prometheus is a free software application used for event monitoring and alerting. It records real-time metrics in a time series database  built using a HTTP pull model, with flexible queries and real-time alerting.


Installing and configuring Prometheus?

1. Go to the official Prometheus downloads page and get the latest download link for the Linux binary.



2. Download the source using wget, untar it, and rename the extracted folder to prometheus-files.

[root@server1 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
[root@server1 ~]# tar -xvzf prometheus-2.32.1.linux-amd64.tar.gz
[root@server1 ~]# mv prometheus-2.32.1.linux-amd64 prometheus-files

3. Create a Prometheus user, required directories, and make Prometheus the user as the owner of those directories.

[root@server1 ~]# useradd --no-create-home --shell /bin/false prometheus
[root@server1 ~]# mkdir /etc/prometheus
[root@server1 ~]# mkdir /var/lib/prometheus
[root@server1 ~]# chown prometheus:prometheus /etc/prometheus
[root@server1 ~]# chown prometheus:prometheus /var/lib/prometheus

4. Copy prometheus and promtool binary from prometheus-files folder to /usr/local/bin and change the ownership to prometheus user.

[root@server1 ~]# cp prometheus-files/prometheus /usr/local/bin/
[root@server1 ~]# cp prometheus-files/promtool /usr/local/bin/
[root@server1 ~]# chown prometheus:prometheus /usr/local/bin/prometheus
[root@server1 ~]# chown prometheus:prometheus /usr/local/bin/promtool

5. Move the consoles and console_libraries directories from prometheus-files to /etc/prometheus folder and change the ownership to prometheus user.

[root@server1 ~]# cp -r prometheus-files/consoles /etc/prometheus
[root@server1 ~]# cp -r prometheus-files/console_libraries /etc/prometheus
[root@server1 ~]# chown -R prometheus:prometheus /etc/prometheus/consoles
[root@server1 ~]# chown -R prometheus:prometheus /etc/prometheus/console_libraries


Setup Prometheus Configuration

All the prometheus configurations should be present in /etc/prometheus/prometheus.yml file.

1. Create the prometheus.yml file.
[root@server1 ~]# vi /etc/prometheus/prometheus.yml

2. Copy the following contents to the prometheus.yml file.

global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

3. Change the ownership of the file to prometheus user.

[root@server1 ~]# chown prometheus:prometheus /etc/prometheus/prometheus.yml

Setup Prometheus Service File

1. Create a prometheus service file.

[root@server1 ~]# vi /etc/systemd/system/prometheus.service

2. Copy the following content to the file.

[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

[Install]
WantedBy=multi-user.target

3. Reload the systemd service to register the prometheus service and start the prometheus service.

[root@server1 ~]#  systemctl daemon-reload
[root@server1 ~]#  systemctl start prometheus

4. Check the prometheus service status using the following command.

[root@server1 ~]# systemctl status prometheus.service
● prometheus.service - Prometheus
   Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-01-19 05:02:15 EST; 35min ago
 Main PID: 1220 (prometheus)
    Tasks: 8 (limit: 4304)
   Memory: 124.4M
   CGroup: /system.slice/prometheus.service
           └─1220 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml

5. Open Firewall port 9090/tcp.

[root@server1 ~]# firewall-cmd --permanent --add-port=9090/tcp
success
[root@server1 ~]# firewall-cmd --reload
success 


Access Prometheus Web UI

Now you will be able to access the prometheus UI on 9090 port of the prometheus server.

http://<prometheus-ServerIp>:9090/graph 

Right now, we have just configured the Prometheus server. You need to register the target in the prometheus.yml file to get the metrics from the source systems.

For example, if you want to monitor ten servers, the IP address of these servers should be added as a target in the Prometheus configuration to scrape the metrics.

The server should have Node Exporter installed to collect all the system metrics and make it available for Prometheus to scrap it.

No comments: