Monitoring Containers with Prometheus and cAdvisor ๐Ÿณ๐Ÿ“Š

Monitoring Containers with Prometheus and cAdvisor ๐Ÿณ๐Ÿ“Š


3 min read


In the world of containerized applications, monitoring and gathering performance data is essential to ensure the reliability and efficiency of your services. cAdvisor, short for Container Advisor, is a powerful tool that allows you to analyze and expose resource usage and performance data from running containers. This article will guide you through setting up a monitoring stack using Prometheus, cAdvisor, and Grafana to help you gain insights into your containerized applications. ๐Ÿš€

What is cAdvisor? ๐Ÿง

cAdvisor is a container monitoring tool developed by Google. It provides real-time information about resource usage, performance metrics, and container statistics. One of its significant advantages is that it exposes metrics in a format compatible with Prometheus, a popular open-source monitoring and alerting toolkit. ๐Ÿ“ˆ

Setting Up Prometheus ๐Ÿ› ๏ธ

To get started with Prometheus, follow these steps:

  • Download the Prometheus Configuration File ๐Ÿ“ฅ:

  •       wget
  • Install Prometheus Using Docker ๐Ÿ‹:

    Run Prometheus in a Docker container, using the configuration file you downloaded:

  •       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

Adding cAdvisor as a Target ๐ŸŽฏ

To start collecting metrics from cAdvisor, add it as a scrape target in your Prometheus configuration. Edit the prometheus.yml file you downloaded earlier and append the following configuration:

- job_name: cadvisor
  scrape_interval: 5s
  - targets:
    - cadvisor:8080

Using Docker Compose ๐Ÿณ๐Ÿค

For a more streamlined setup, you can use Docker Compose. Create a docker-compose.yaml file with the following content:

version: '3.2'
    image: prom/prometheus:latest
    container_name: prometheus
    - 9090:9090
    - --config.file=/etc/prometheus/prometheus.yml
    - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    - cadvisor
    container_name: cadvisor
    - 8080:8080
    - /:/rootfs:ro
    - /var/run:/var/run:rw
    - /sys:/sys:ro
    - /var/lib/docker/:/var/lib/docker:ro
    - redis
    image: redis:latest
    container_name: redis
    - 6379:6379

Run Prometheus and cAdvisor with Docker Compose:

docker-compose up -d

You can check the status of the containers using:

docker-compose ps

Visualizing Metrics with Grafana ๐Ÿ“Š๐Ÿ“ˆ

To visualize the collected metrics, you can use Grafana, a popular open-source dashboard and visualization platform.

Installing Grafana on Debian or Ubuntu ๐Ÿ“Š๐Ÿง

  1. Install the required dependencies:
  •       sudo apt-get install -y apt-transport-https
          sudo apt-get install -y software-properties-common wget
  • Add the Grafana GPG key and repository:

sudo wget -q -O /usr/share/keyrings/grafana.key

For the stable release, add:

echo "deb [signed-by=/usr/share/keyrings/grafana.key] stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

Or for the beta release, add:

  •       echo "deb [signed-by=/usr/share/keyrings/grafana.key] beta main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
  • Update the package list:

  •       sudo apt-get update
  • Install Grafana:

  •       sudo apt-get install grafana
  • Start Grafana:

  •      sudo /bin/systemctl start grafana-server

Now, you can access Grafana at http://localhost:3000 and set up dashboards to visualize the metrics collected by Prometheus from cAdvisor.

Conclusion ๐ŸŽ‰

By setting up Prometheus with cAdvisor and Grafana, you have created a robust container monitoring stack. You can now effectively monitor the resource usage and performance metrics of your containerized applications, helping you to make data-driven decisions and ensure the health and performance of your services. Happy monitoring! ๐Ÿš€๐Ÿณ๐Ÿ“ˆ

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!