Setup server monitor with Prometheus and Grafana


Setup server monitor with Prometheus and Grafana

Overview

We are going to cover the process on how to setup server monitor with Prometheus and Grafana using Docker. The entire setup will be installed and configured on Debian 10 Buster and Docker but this also can be done and will work on Ubuntu server or any other Linux distro running Docker. With this setup we’ll be able to track and monitor resources on our server(host machine) – CPU usage, RAM usage, SWAP, network, disk space and etc.

Prerequisites

This setup requires to have Docker installed and running on your machine. You can check out this link to install Docker on Debian or check out the official Docker documentation.

Install Node Exporter

1. After you have Docker installed and running, the first step of the setup is to install the Node Exporter tool. Node Exporter is the part Prometheus monitor and it’s purpose is to gather all the data from your machine so that Prometheus can retrieve it. Node Exporter also comes in a Docker container, so the setup will be straightforward.

setup server monitor prometheus and grafana

docker run -d --net="host" --pid="host" -v "/:/host:ro,rslave" quay.io/prometheus/node-exporter:latest --path.rootfs=/host

You can easily check if the Node Exporter is running properly. In the browser, open the node exporter URL – http://your-server-ip-adress:9100 and if you see a directory metrics with data populating in it, then it means it’s working.

prometheus and grafana

Other way to test it is with the command:

curl localhost:9100/metrics

or

curl http://your-server-ip-address:9100/metrics

Create a Prometheus YAML config

2. Now we need to make a Promethes YAML config file. Create this file where you want to place it on the server, but for the sake of the post, let’s have it placed in home directory.

You can do this step like this:

sudo touch ~/prometheus/prometheus.yml
sudo nano ~/prometheus/prometheus.yml

or just right away

sudo nano ~/prometheus/prometheus.yml

and then paste in the config and save the file and exit.

global:
scrape_interval: 5s
external_labels:
monitor: 'node'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.100.252:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['192.168.100.252:9100']

At the targets variable, write down the IP address of your server.

Install Prometheus

3. Next step is to install Prometheus, also with Docker. Run the next command and please do watch out on the path to the prometheus.yml file:

docker run -d --name prometheus -p 9090:9090 -v ~/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

prometheus and grafana server monitor

We are going to break down the command to explain it in more details:

  • docker run -d –name prometheus -p 9090:9090 – this part of the command should be understandable to many – run a Docker image, name it prometheus and expose container port 9090 to the same port on the host server.
  • -v ~/prometheus/prometheus/yml:/etc/prometheus/prometheus/yml – this is the most important part of the command when installing prometheus. On this part of the command, we’re telling Prometheus to use the prometheus.yml file we created in the previous step and where it’s located on the host server(home directory in this example) in order to configure itself with the parameters we have set and to place the same config file in the /etc directory of the Prometheus Docker container.
  • prom/prometheus – this is a variable for Docker which Docker image from the Docker to download and install.

You can also check if Prometheus is running via web browser by opening also your server’s IP address with port 9090.  You should get a result like in the pictures bellow:

prometheus and grafana

setup server monitor prometheus and grafana

Install Grafana

4. Now we can install Grafana. Also we will Grafana with Docker container.

docker run -d --name=grafana -p 3456:3000 grafana/grafana

Access the Grafana in your web browser. It will prompt you to login right away. The default user name and password is admin. It’s a temporary login. As soon as you login, the Grafana will ask from you to change the password right away.

 

 

Create a data source and a dashboard

After the login, we need to create a monitor dashboard and to create a new data source which Grafana will use. So, first we’ll create the data source. Right on the Grafana home page, click on the – add your first data source option.

Choose for the data source option to be Prometheus – It’ll be the first available option right away.

Name the data source as you want. On the URL input field, type in your server’s IP address with Prometheus port 9090 and HTTP. Like in the picture bellow.

Setup server monitor with Prometheus and Grafana

Save & test the data source, if all is good, it will pop out a prompt stating that the Data source is working.

Now to setup our server monitor dashboard. You can create a new dashboard from scratch the way you want it(takes time), but also you can check out the Grafana site and see the already created dashboards by the Grafana community. In this example, we will do just that, import an existing dashboard. There are plenty Grafana dashboards for server monitoring which uses Prometheus and Node Exporter. The one we’re going to use for this example is this one.

So, once you go on the import dashboard option, we just need to type in the Dashboard ID from the Grafana site – in this example it’s 1860. Name the dashboard and also the important part – to select the Prometheus as a data source for this dashboard. Then just click on the import button and that’s it.

Setup server monitor with Prometheus and Grafana

At the end of the setup, we should have a server monitor dashboard with Prometheus and Grafana, all running in Docker.

Summary

This was the process how to setup server monitor with Prometheus and Grafana using Docker. This can be a really powerful monitor setup, since you can monitor Docker containers, multiple host servers and also setup email notifications from Prometheus.

Thank you very much for your time.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *