CONTAINER MONITORING GUIDE: USING CADVISOR, PROMETHEUS AND GRAFANA FOR EASY DOCKER CONTANER MONITORING
 Time to read: 15 minutes

In this guide, we will learn to set up cAdvisor in a container using docker-compose file, connect it with prometheus and monitor every containers of our server through grafana.


CAdvisor is a popular tool that is used to collect information about our containers. It is an agent that is used by prometheus and grafana to scrape information and visualize the information in graphs, charts, time series and various other forms


Part 1: Setting up cAdvisor and prometheus:

In my previous blog we learnt how to setup node_exporter for server monitoring. We made docker compose file and did setup of prometheus, grafana and node_exporter.

We will add cadvisor in our docker-compose file. You can look at my previous guide to find the docker-compose file.

Lets configure docker-compose.yml and start up our containers

sudo nano docker-compose.yml

Now add the following code into it:

cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    container_name: cadvisor
    ports:
      - "9200:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    devices:
      - /dev/kmsg
    networks:
      - prometheus-network
    restart: unless-stopped
Our configuration file should look like this:

Also, we need to add some configs in the config file for Prometheus that we created previously.

sudo nano config/prometheus.yml

Add the following code inside it:

- job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

Our config should look like this:

Now lets build and start our containers:

sudo docker-compose up --build –d

and check the status:

docker-compose ps

Looks like everything is running fine. Now go to http://yourserverip:9090/ to access Prometheus:

Go to Status->targets.

Here we can see that cadvisor is up.

It means that prometheus can scrape data from cadvisor as well.

Let’s login to our grafana server.

Goto http://yourserverip:3000/

Now let’s setup our dashboard for container monitoring.

Go to import:

Now paste 14282 and click on Load

This ID helps us to export full cadvisor dashboard from grafana dashboard library. You can browser more dashboards on https://grafana.com/grafana/dashboards/.

Click on load.

Set name, set the correct preometheus and click on import. Check my previous blog if you are confused.

After importing you can see a dashboard. This dashboard is readymade dashboard and contains a lot of information.

Your dashboard has been loaded along with various metrics such as CPU utilization, memory utilization, network traffic utilization, etc. There are a lot of various metrics that you can explore and customize as you need.

DONE!!! VERY EASY RIGHT?

You can also monitor containers from another server with only few modifications. Also you can set up alert system and send alert messages to your email, rocketchat or slack whenever some abnormality is detected in your server or containers.

In my next blog i will teach you how to monitor your mysql database using prometheus, grafana, and mysql_exporter.


Leave a Reply

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