A few days ago, at the office, my colleague in the engineering team tried to play with Grafana and Prometheus to monitor Mikrotik devices in the office. That made me remember that I also have experience with these tools. Back in 2018, when I was still working as a developer, I tried to set up Grafana and Prometheus to monitor all of the office VMs at that time. Since our office didn’t have a DevOps team, my boss asked me to handle everything related to our VPS used for application deployment. However, because I was too lazy to check the condition of each VPS one by one, I took the initiative to find monitoring tools that would make it easier to monitor the conditions of all our office VPS. That’s when I discovered Prometheus and Grafana.

That’s why, in this note, I want to share how to set up or install Prometheus and Grafana using Docker. So, without further ado, let’s get started! 🚀


Prometheus Docker Installation

  1. Create user prometheus  and node_exporter without possibility to log in
sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
sudo useradd --no-create-home --shell /bin/false node_exporter
  1. create directory for prometheus config file
sudo mkdir /etc/prometheus
  1. create the config file for prometheus
touch prometheus.yml
  1. change owner for /etc/prometheus & prometheus.yml
cd /etc
sudo chown prometheus:prometheus -R prometheus
  1. add config to prometheus.yml file
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

6 Install Prometheus docker container

$ docker run -d --name prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

Ensure to replace PATH_TO_prometheus.yml_FILE with the Path where you have kept prometheus.yml file.


Node Exporter  Installation for Host Monitoring

DOCKER INSTALLATION :

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

MANUAL INSTALLATION (without docker ) :

  • Create User for node exporter without home directory and disable the shell``` sudo useradd –no-create-home –shell /bin/false node_exporter

  • Download node exporter from : https://prometheus.io/download/#node_exporter

  • unpack the download acrvhive tar xfv node_exporter.tar.gz

  • Copy the *node_exporter *binary file into directory /usr/local/bin

  • Change the ownership of node_exporter binary file

      sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
    
  • Create service for node exporter at /etc/systemd/system/node_exporter.service

         [Unit]
         Description=Node Exporter
         Wants=network-online.target
         After=network-online.target
    
         [Service]
         User=node_exporter
         Group=node_exporter
         Type=simple
         ExecStart=/usr/local/bin/node_exporter
    
         [Install]
         WantedBy=multi-user.target
    
  • Reload systemd to use node_exporter service

      sudo systemctl daemon-reload
    
  • Start the node_exporter service

      sudo systemctl start node_exporter
    
  • Check Status node_exporter Service with sudo systemctl status node_exporter


Grafana Docker Installation

docker run -d -p 3000:3000 grafana/grafana