Monitoring Containers with Prometheus

Get Started
1.5 hours
  • 3 Learning Objectives

About this Hands-on Lab

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. It’s time to implement some basic monitoring in the environment. You will handle this in two ways: using `Docker stats` and Prometheus. Your mission is to set up Prometheus and cAdvisor using Docker Compose.

You can get more information on Prometheus at [prometheus.io](https://prometheus.io/).

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a `prometheus.yml` File

In the root directory, create prometheus.yml.

scrape_configs:
- job_name: cadvisor
  scrape_interval: 5s
  static_configs:
  - targets:
    - cadvisor:8080
Create the Prometheus Services

Create a docker-compose.yml file:

version: '3'
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - 9090:9090
    command:
      - --config.file=/etc/prometheus/prometheus.yml
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    depends_on:
      - cadvisor

  cadvisor:
    image: google/cadvisor:latest
    container_name: cadvisor
    ports:
      - 8080:8080
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker:/var/lib/docker:ro

Stand up the environment.

docker-compose up -d
Create `stats.sh`

Create stats.sh in /root.

 docker stats --format "table {{.Name}} {{.ID}} {{.MemUsage}} {{.CPUPerc}}"

Make sure the file can be executed.

 chmod a+x stats.sh

Execute the script.

 ./stats.sh

When you’re finished, exit by pressing Ctrl+C.

Additional Resources

In this learning activity, you will monitor containers using Docker stats, Prometheus, and cAdvisor.

Part 1: Using Prometheus

  1. Create a prometheus.yml file that contains the cadvisor job.
    • Set the scrape interval to 5 seconds.
    • Target cadvisor on port 8080.
    • Create a Docker Compose file and use Version 3.
    • The first service will be called prometheus.
    • Use prom/prometheus:latest for the image.
    • Name the container prometheus.
    • Map port 9090 to 9090 on the container.
    • Set the command to --config.file=/etc/prometheus/prometheus.yml.
    • Create a volume that maps prometheus.yml to /etc/prometheus/prometheus.yml. Make the volume read-only.
    • Make sure it depends on the cAvisor container.
    • Create a second service called cadvisor.
    • Use google/cadvisor:latest for the image.
    • Name the container cadvisor.
    • Map port 8080 to 8080 on the container.
    • Create the following volumes:
    • / maps to /rootfs (make it read-only)
    • /var/run maps to /var/run (make it read-write)
    • /sys maps to /sys (make it read-only)
    • /var/lib/docker/ maps to /var/lib/docker (make it read-only)
    • Execute a docker-compose up.
    • Make sure the container is running in the background.

Part 2: Using Docker stats

There are two containers already running in your live environment: nginx and redis.

  1. Create a shell script in your root directory called stats.sh.
    • Make sure the script is executable.
    • In the file, add a line that will execute the Docker stats command using the formatting flag with the name, ID, memory usage, and CPU percentage placeholders.
    • Make sure the output is formatted as a table.
    • Execute stats.sh to view the results.
    • When you're finished, exit the results.
What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?