December 16, 2020

Monitor containers using Grafana, Prometheus, Node exporter and cAdvisor

Monitor containers using Grafana, Prometheus, Node exporter and cAdvisor

Below is how Prometheus powered Grafana dashboard looks like.

Architecture

Following tools will be used to collect, aggregate & visualize metrics.

  1. Container metrics using cAdvisor
  2. Host metrics using Prometheus Node Exporter
  3. Visualization using Grafana pre-built dashboards

Getting Start

Start off by launching all containers via a very simple docker-compose.yml configuration file

version: '3.2'
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: monitoring_prometheus
    user: "root"
    restart: unless-stopped
    volumes:
      - ./data/prometheus/config:/etc/prometheus/
      - ./data/prometheus/data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    expose:
      - 9090
    ports:
      - 9090:9090
    links:
      - cadvisor:cadvisor
      - node-exporter:node-exporter
  node-exporter:
    image: prom/node-exporter:latest
    container_name: monitoring_node_exporter
    restart: unless-stopped
    expose:
      - 9100
  cadvisor:
    image: google/cadvisor:latest
    container_name: monitoring_cadvisor
    restart: unless-stopped
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    expose:
      - 8080
  
  grafana:
    image: grafana/grafana:latest
    user: "root"
    container_name: monitoring_grafana
    restart: unless-stopped
    ports:
      - 3000:3000
    links:
      - prometheus:prometheus
    volumes:
      - ./data/grafana:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_SERVER_DOMAIN=myrul.com
      - GF_SMTP_ENABLED=true
      - GF_SMTP_HOST=smtp.gmail.com:587
      - [email protected]
      - GF_SMTP_PASSWORD=mypassword
      - [email protected]

Prometheus Configuration

Create new Prometheus configuration in a file called prometheus.yml . Prometheus server requires a configuration file that defines the endpoints to scrape along with how frequently the metrics should be accessed and to define the servers and ports that Prometheus should scrape data from.

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090', 'node_exporter:9100', 'cadvisor:8080'] 

Grafana Configuration

Grafana will be listening on http://localhost:3000. The default login is "admin" / "admin",

set up Prometheus server as the data source for Grafana. Give it a name, make sure you select “Prometheus” as the type, and set the URI to http://localhost:9090

Datasource-1

Once data source is completed import following Json file to create dashboard for containers.

Jsonfile

khan5217/Grafana
Contribute to khan5217/Grafana development by creating an account on GitHub.