Monitoring your application servers is essential to ensure optimal performance and prevent downtime. Prometheus and Grafana are two popular tools for monitoring and visualizing metrics. Prometheus is an open-source systems monitoring and alerting toolkit that collects metrics from various sources and stores them in a time-series database. Grafana, on the other hand, is an open-source visualization and analytics software that allows you to create dashboards to monitor and analyze data from various sources.
In this article, we will walk you through how to monitor your application servers using Prometheus and Grafana, with code examples and images.
Prerequisites:
- A Linux server running Ubuntu 18.04 or later.
- Docker installed on the server.
- A Prometheus and Grafana account.
Step 1: Install Prometheus
To install Prometheus, we will use Docker. Docker allows us to run Prometheus in a container, which is isolated from the host operating system. Run the following command to pull the Prometheus image from Docker Hub:
docker pull prom/prometheus
After pulling the image, create a directory named prometheus
in your home directory to store the Prometheus configuration file:
mkdir ~/prometheus
Create a prometheus.yml
file in the prometheus
directory with the following configuration:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
This configuration tells Prometheus to scrape metrics from itself on localhost:9090
. You can add additional scrape targets for other applications that you want to monitor.
Next, start the Prometheus container with the following command:
docker run -d --name prometheus -p 9090:9090 -v ~/prometheus:/etc/prometheus prom/prometheus
This command starts the Prometheus container, maps port 9090 from the container to the host, and mounts the prometheus
directory as the configuration directory in the container.
Step 2: Install Node Exporter
Node Exporter is a Prometheus exporter that collects hardware and OS metrics from the host system. We will install Node Exporter to collect metrics from our application servers.
Run the following command to download the Node Exporter binary:
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
Extract the binary and move it to the /usr/local/bin
directory:
tar xvfz node_exporter-1.2.2.linux-amd64.tar.gz
sudo mv node_exporter-1.2.2.linux-amd64/node_exporter /usr/local/bin/
Create a node_exporter.service
file in the /etc/systemd/system
directory with the following content:
[Unit]
Description=Node Exporter
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=default.target
This configuration tells Systemd to run Node Exporter as the node_exporter
user and start it automatically on boot.
Start the Node Exporter service with the following commands:
sudo useradd -rs /bin/false node_exporter
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
Step 3: Configure Prometheus to Scrape Metrics from Node Exporter
Now that we have Node Exporter installed, we need to configure Prometheus to scrape metrics from it.
Update the prometheus.yml
file in the prometheus
directory with the following configuration:
global:
scrape_interval: 15s
scrape