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.
- 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
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.
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.
Start the prometheus service and make it start every time the system starts.
systemctl start prometheussystemctl enable prometheus
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.
Make sure you get the results shown below.
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.
You will get TCP port “9090” in the list.
Next, open your web browser and type the server IP address using port 9090.
Enter “prometheus_http_requests_total” in the query field and click the “Go” button.
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
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.
Save and exit.
Now reload the systemd system.
Then start the node_exporter service and make it start every time the system starts.
systemctl start node_exportersystemctl enable 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.
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.
Step 6-test Prometheus and node_exporter
Open a web browser and enter the server IP address.
Enter meminfo collector “node_memory_MemAvailable_bytes” in the query field and click the “Execute” button.
You will get the following results.
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.
You will then get the raw node_exporter data as shown below.
As a result, the installation and configuration of the Prometheus surveillance system was completed using a node_exporter on a CentOS 8 server.