Prometheus and Docker
cAdvisor: Richer Container Metrics
cAdvisor is an open-source container monitoring tool developed by Google. It has more metrics than what Docker provides by default. Docker metrics are mainly centered around the Docker daemon itself, while cAdvisor has a more comprehensive focus on the resource usage and performance of individual containers.
To use cAdvisor with Prometheus, you can run it as a Docker container on server1:
docker run -d \
--name=cadvisor \
--restart=always \
-p 8080:8080 \
-v /:/rootfs:ro \
-v /var/run:/var/run:rw \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
gcr.io/cadvisor/cadvisor:v0.52.0
Run the following command to verify that cAdvisor is running:
docker ps | grep cadvisor
Followed by this command to get the URL to access cAdvisor's web UI:
echo "http://$(curl ifconfig.me):8080"
To get what cAdvisor exposes, run the following command on monitoring server:
cat < /etc/prometheus/prometheus.yml && kill -HUP $(pgrep prometheus)
global:
scrape_interval: 15s
scrape_configs:
# The existing job for Docker
- job_name: 'docker'
static_configs:
- targets: ['$server1:9323']
# The new job for cAdvisor
- job_name: 'cadvisor'
static_configs:
- targets: ['$server1:8080']
EOF
Let's run 5 containers on server1 to generate some load and metrics. You can use the following command:
# On server1, run 5 containers
for i in {1..5}; do docker run -d \
--name=container$i eon01/infinite; done
On monitoring server, you can now check the metrics being collected:
# Fetch the metrics from cAdvisor for container1
curl -s http://$server1:8080/metrics | grep 'name="container1"'
# Fetch the metrics from Prometheus for container2
curl -s http://$server1:8080/metrics | grep 'name="container2"'
# ..etc for other containers
Observability with Prometheus and Grafana
A Complete Hands-On Guide to Operational Clarity in Cloud-Native SystemsEnroll now to unlock all content and receive all future updates for free.
