How to install Prometheus Monitoring and node_exporter on CentOS 8

How to install Prometheus Monitoring and node_exporter on CentOS 8

Prometheus is an open source monitoring system that allows you to collect metrics from any configured target system. Prometheus implements a multi-dimensional data model using powerful queries, excellent visualizations, precise alerts, and many integrations.

Prometheus was originally created by SoundCloud in 2012, but has now become an independent open source project independently maintained by the open source community-all components are available under the Apache 2 License on Github.

In this tutorial, I will show you how to install and configure Prometheus and node_exporter. We will install the Prometheus monitoring system and then configure node_exporter to collect metrics from the server. This tutorial guide will use the latest CentOS 8 server.

prerequisites

  • CentOS 8 server
  • Root privilege

What will we do?

  • Create a new user and download Prometheus
  • Configure Prometheus as a system service
  • Configure firewall
  • Install and configure node_exporter
  • Add node_exporter to the Prometheus server
  • testing

Step 1-Create a new user and download Prometheus

In this tutorial, the Prometheus service will run as a non-root user. Therefore, we need to create a new non-root user on the system. We will create a new user named “prometheus” and all prometheus services will run under the prometheus user.

Use the following command to create a new “prometheus” user.

useradd -m -s /bin/bash prometheus

A new user has been created. Now log in to the user and download the prometheus using the wget command.

su - prometheuswget https://github.com/prometheus/prometheus/releases/download/v2.13.1/prometheus-2.13.1.linux-amd64.tar.gz

Extract the prometheus-xx.x.x.tar.gz file and rename the directory to “prometheus”.

tar -xf prometheus-2.13.1.linux-amd64.tar.gzmv prometheus-2.13.1.linux-amd64/ prometheus/

After that, create a new “data” directory and use it as “tsdb” storage.

mkdir -p ~/prometheus/data

Now, download prometheus to the server and all prometheus files are located in the “/ home / prometheus / prometheus” directory.

Extracting archive files

Step 2-configure Prometheus as a system service

In this step, we configure Prometheus as a system service. We will create a new service file prometheus.service in the “/ etc / systemd / system” directory.

Go to “/ etc / systemd / system /” directory and use vim editor to create a new service file “prometheus.service”.

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

Paste the prometheus service configuration below.

[Unit]Description=Prometheus ServerDocumentation=https://prometheus.io/docs/introduction/overview/After=network-online.target[Service]User=prometheusRestart=on-failure#Change this line if you download the #Prometheus on different path userExecStart=/home/prometheus/prometheus/prometheus   --config.file=/home/prometheus/prometheus/prometheus.yml   --storage.tsdb.path=/home/prometheus/prometheus/data[Install]WantedBy=multi-user.target

Save and exit.

Now, reload the systemd system using the systemctl command below.

systemctl daemon-reload

Start the prometheus service and make it start every time the system starts.

systemctl start prometheussystemctl enable prometheus

Configure Prometheus service in systemd

Prometheus is now running as a systemd service, please check it with the following command.

systemctl status prometheus

Make sure you get the “Running” service status.

Or check it using the netstat command below.

netstat -plntu

Make sure you get the results shown below.

Check the status of Prometheus services

Prometheus runs on port “9090” by default.

Step 3-configure firewall

If you use Firewalld on your system, add port 9090 to the Firewalld list port.

Use the following firewall-cmd command to open port 9090 for Prometheus access.

firewall-cmd --add-port=9090/tcp --permanentfirewall-cmd --reload

Now check firewalld’s rules.

firewall-cmd --list-all

You will get TCP port “9090” in the list.

Configure firewall

Next, open your web browser and type the server IP address using port 9090.

http://10.5.5.15:9090/graph

Enter “prometheus_http_requests_total” in the query field and click the “Go” button.

Prometheus System Monitor Dashboard

You will see the HTTP request to the Prometheus server and all the information about time, HTTP status codes and methods, etc.

Step 4-install and configure node_exporter

node_exporter is an exporter of machine metrics that can be run on * Nix and Linux systems.

In this tutorial, we will install node_exporter on the same server. We will monitor and get metrics for Prometheus server.

Log in to the prometheus user and download the “node_exporter” using the wget command.

su - prometheuswget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

Extract node_exporter and rename the directory to “node_exporter”.

tar -xf node_exporter-0.18.1.linux-amd64.tar.gzmv node_exporter-0.18.1.linux-amd64 node_exporter

Install and configure node_exporter

Next, we will create a new service file for node_exporter. Return to the root shell, go to the “/ etc / systemd / system” directory, and use vim to create a new node_exporter service file “node_exporter.service”.

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

Paste the following configuration.

[Unit]Description=Node ExporterWants=network-online.targetAfter=network-online.target[Service]User=prometheusExecStart=/home/prometheus/node_exporter/node_exporter[Install]WantedBy=default.target

Save and exit.

Now reload the systemd system.

systemctl daemon-reload

Then start the node_exporter service and make it start every time the system starts.

systemctl start node_exportersystemctl enable node_exporter

Start node_exporter

The node_exporter service is now running on the server-check it with the netstat command.

systemctl status node_exporternetstat -plntu

You will get the node exporter up and running on the default port 9100.

Check node_exporter status

Step 5-add node_exporter to the Prometheus server

In this step, we add node_exporter to the prometheus server. Log in to the prometheus user and edit the configuration “prometheus.yml” file.

Log in to the prometheus user.

su - prometheus

Go to the “prometheus” directory and edit the configuration file “prometheus.yml”.

cd ~/prometheus/vim prometheus.yml

Under the “scrape_config” line, add a new job_name node_exporter by copy-pasting the following configuration.

  - job_name: 'node_exporter'    static_configs:    - targets: ['localhost:9100']

Save and exit.

Now restart the prometheus service.

systemctl restart prometheus

node_exporter has been added to the prometheus server.

Restart Prometheus service

Step 6-test Prometheus and node_exporter

Open a web browser and enter the server IP address.

http://10.5.5.15:9090/

Enter meminfo collector “node_memory_MemAvailable_bytes” in the query field and click the “Execute” button.

You will get the following results.

Test Prometheus

You will get available memory on server instance ‘localhost: 9100’ and job_name ‘node-exporter’.

Alternatively, if you want to access the node_exporter raw data, you need to add node_exporter port 9100 to Firewalld.

firewall-cmd --add-port=9100/tcp --permanentfirewall-cmd --reload

Then type the server IP address with port 9100.

http://10.5.5.15:9100/metrics

You will then get the raw node_exporter data as shown below.

Prometheus metric

As a result, the installation and configuration of the Prometheus surveillance system was completed using a node_exporter on a CentOS 8 server.

reference

  • https://github.com/prometheus/prometheus
  • https://github.com/prometheus/node_exporter
Sidebar