How to install and configure Prometheus on CentOS 7

Arif
6 min readMay 22, 2021

--

Prometheus is an open-source toolkit initially built at SoundCloud for monitoring and alerting. The toolkit is now a standalone open source project and maintained independently of any company.

The toolkit collects metrics from monitored targets by scrapping HTTP endpoints on those targets. Most Prometheus components are in Go. Some written in Java, Python, and Ruby.

Prometheus works well to record any numerical time series. It fits both machine-centric monitoring and high-dynamic service-oriented architecture monitoring. In addition to making it easier to run and integrate into your environment, Prometheus offers a rich data model and query language.

The following tutorial shows steps to install Prometheus on CentOS.

Install and Configure Prometheus on CentOS 7

Login to Terminal as root, and start firing the commands below.

Step 1 — Update System

sudo yum update -y

Step 2 — Disable SELinux

Open SELinux configuration and edit the file:

sudo vim /etc/sysconfig/selinux

Change “SELINUX=enforcing” to “SELINUX=disabled”.

Save and exit the file. Then reboot the system.

sudo reboot

Step 3 — Download Prometheus package

Go to official Prometheus downloads page, and copy the URL of Linux “tar” file.

Run the following command to download package. Paste the copied URL after wget in the below command:

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

Step 4 — Configure Prometheus

Add a Prometheus user.

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

Create needed directories.

sudo mkdir /etc/prometheussudo mkdir /var/lib/prometheus

Change the owner of the above directories.

sudo chown prometheus:prometheus /etc/prometheussudo chown prometheus:prometheus /var/lib/prometheus

Now go to Prometheus downloaded location and extract it.

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

Rename it as per your preference.

sudo mv prometheus-2.27.1.linux-amd64 prometheuspackage

Copy “prometheus” and “promtool” binary from the “prometheuspackage” folder to “/usr/local/bin”.

sudo cp prometheuspackage/prometheus /usr/local/bin/sudo cp prometheuspackage/promtool /usr/local/bin/

Change the ownership to Prometheus user.

sudo chown prometheus:prometheus /usr/local/bin/prometheussudo chown prometheus:prometheus /usr/local/bin/promtool

Copy “consoles” and “console_libraries” directories from the “prometheuspackage” to “/etc/prometheus folder”

sudo cp -r prometheuspackage/consoles /etc/prometheussudo cp -r prometheuspackage/console_libraries /etc/prometheus

Change the ownership to Prometheus user

sudo chown -R prometheus:prometheus /etc/prometheus/consolessudo chown -R prometheus:prometheus /etc/prometheus/console_libraries

Add and modify Prometheus configuration file.

Configurations should be added to the “/etc/prometheus/prometheus.yml”

Now we will create the prometheus.yml file.

sudo vim /etc/prometheus/prometheus.yml

Add the following configuration to the file.

global:
scrape_interval: 10s
scrape_configs:
- job_name: 'prometheus_master'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']

save and exit the file

Change the ownership of the file.

sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml

Configure the Prometheus Service File.

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

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

Save and the exit file.

Reload the systemd service.

sudo systemctl daemon-reload

Start the Prometheus service.

sudo systemctl start prometheus

Check service status.

sudo systemctl status prometheus

Add firewall rules.

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

Reload firewall service.

sudo systemctl reload firewalld

Step 5 — Access Prometheus Web Interface

Use the following Url to access UI.

http://Server-IP:9090/graph

Then you can see the following interface.

Step 6 — Monitor Linux Server Using Prometheus

First, you need to configure Prometheus node exporter on a Linux server.

Copy URL of the Node Exporter form the official download page.

Paste the copied URL after wget in the following command:

wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz

Extract the downloaded package.

tar -xvzf node_exporter-1.1.2.linux-amd64.tar.gz

Create a user for the node exporter.

sudo useradd -rs /bin/false nodeusr

Move binary to “/usr/local/bin” from the downloaded extracted package.

sudo mv node_exporter-1.1.2.linux-amd64/node_exporter /usr/local/bin/

Create a service file for the node exporter.

sudo vim /etc/systemd/system/node_exporter.service

Add the following content to the file.

[Unit]
Description=Node Exporter
After=network.target
[Service]
User=nodeusr
Group=nodeusr
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target

Save and exit the file.

Reload the system daemon.

sudo systemctl daemon-reload

Start node exporter service.

sudo systemctl start node_exporter

Add a firewall rule to allow node exporter.

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

Reload firewall service.

sudo systemctl restart firewalld

Enable node exporter on system boot.

sudo systemctl enable node_exporter

View the metrics browsing node exporter URL.

http://IP-Address:9100/metrics

Add configured node exporter Target On Prometheus Server.

Login to Prometheus server and modify the prometheus.yml file

Edit the file:

sudo vim /etc/prometheus/prometheus.yml

Add the following configurations under the scrape config.

- job_name: 'node_exporter_centos'
scrape_interval: 5s
static_configs:
- targets: ['TARGET_SERVER_IP:9100']

The file should look like as follows.

Restart Prometheus service.

sudo systemctl restart prometheus

Login to Prometheus server web interface, and check targets.

http://Prometheus-Server-IP:9090/targets

You can click the graph and query any server metrics and click execute to show output. It will show the console output.

Query Browser:

Console output:

Click Graph to view.

Step 7 — Monitor MySQL Server Using Prometheus

Login to MySQL and execute the following queries.

CREATE USER 'mysqlexporter'@'localhost' IDENTIFIED BY 's56fsg#4W2126&dfk' WITH max_user_connections 2;GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqlexporter'@'localhost';FLUSH PRIVILEGES;

Download mysqld_exporter from the official download page.

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0-rc.0/mysqld_exporter-0.13.0-rc.0.linux-amd64.tar.gz

Extract the Downloaded file.

tar -xvzf mysqld_exporter-0.13.0-rc.0.linux-amd64.tar.gz

Add a user for mysqld_exporter.

sudo useradd -rs /bin/false mysqld_exporter

Copy mysqld_exporter file to /usr/bin.

sudo mv mysqld_exporter-0.13.0-rc.0.linux-amd64/mysqld_exporter /usr/bin

Change ownership of the file.

sudo chown mysqld_exporter:mysqld_exporter /usr/bin/mysqld_exporter

Create needed folders.

sudo mkdir -p /etc/mysql_exporter

Create a MySQL password file for mysqld_exporter.

sudo vim /etc/mysql_exporter/.my.cnf

Add the following configurations to the file.

[client]
user=mysqlexporter
password=sdfsg#4W2126&gh

Save and exit the file.

Change ownership.

sudo chown -R mysqld_exporter:mysqld_exporter /etc/mysql_exporter

Grant needed permission.

sudo chmod 600 /etc/mysql_exporter/.my.cnf

Create a service file.

sudo vim /etc/systemd/system/mysql_exporter.service

Add the following content to the file.

[Unit]
Description=MySQL Server NAMESERVER
After=network.target
[Service]
User=mysqld_exporter
Group=mysqld_exporter
Type=simple
ExecStart=/usr/bin/mysqld_exporter \
--config.my-cnf="/etc/mysql_exporter/.my.cnf"
Restart=always
[Install]
WantedBy=multi-user.target

Reload the system daemon.

sudo systemctl daemon-reload

Enable mysql_exporter on system boot.

sudo systemctl enable mysql_exporter

Start service.

sudo systemctl start mysql_exporter

View the metrics using the following URL.

http://Server_IP:9104/metrics

Now go to Prometheus server and modify the prometheus.yml file.

sudo vim /etc/prometheus/prometheus.yml

Add the following content to the file.

- job_name: 'mysql_exporter_fosslinux'
scrape_interval: 5s
static_configs:
- targets: ['TARGET_SERVER_IP:9104']

Restart Prometheus.

sudo systemctl restart prometheus

You can see added targets by clicking targets under the status.

http://IP:9090/targets

Now you can select query using query browser and get the result of MySQL server.

That’s all about the installation and configuration of Prometheus server on CentOS 7. Hope you enjoyed this tutorial. Let us know your feedback in the comments below.

--

--

Arif
Arif

Written by Arif

Blue Team at Indonesian SOCs

Responses (2)