Welcome back to the #90DaysOfDevOps Challenge! ๐ On Day 75, weโll explore how to send Docker logs to Grafana for real-time monitoring and analysis. By integrating Docker containers with Grafana, you can gain valuable insights into your containerized applications and ensure their smooth operation. Letโs get started! ๐ฅ
Monitoring Container Performance with cAdvisor and Prometheus ๐
cAdvisor (Container Advisor) is an open-source tool designed for monitoring and analyzing container performance at the individual container level. It provides valuable insights into resource usage, performance metrics, and health statistics of containers running on a host or within a container orchestration platform like Kubernetes. ๐ ๏ธ
Key Features of cAdvisor:
Container-Level Metrics: cAdvisor collects various metrics at the container level, including CPU usage, memory consumption, file system usage, network statistics, and more. ๐
Real-Time Monitoring: It provides real-time monitoring of containers, allowing users to observe changes in resource utilization and performance metrics as containers operate. โฑ๏ธ
Resource Utilization Analysis: cAdvisor offers comprehensive insights into the resource utilization of containers, helping identify bottlenecks and inefficiencies. ๐
Container Health Analysis: It provides health checks and analysis for containers, indicating the overall health status of containers. ๐
Integrating cAdvisor with Prometheus enhances container monitoring capabilities, providing valuable insights into container performance and resource utilization. By leveraging the powerful features of cAdvisor and Prometheus, DevOps teams can optimize containerized applications, troubleshoot performance issues, and ensure the overall health and efficiency of their container infrastructure. ๐
Task: Sending Docker Logs to Grafana ๐ฌ
Step 1: Install Docker and Start Docker Service on Ubuntu EC2 through USER Data ๐
Weโll use the below script through USER DATA to install Docker and start and enable the service. ๐ ๏ธ
#!/bin/bash
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
sudo reboot
Step 2: Create a docker-compose.yml
File with All the Required Containers ๐
Connect to your Ubuntu EC2 instance. I've chosen to create two containers: Code-Server, which is a containerized version of VS Code, and Jenkins. The docker-compose file also includes Grafana, Prometheus, and cAdvisor. Create a docker-compose.yml
file with the following content. ๐
version: "3"
volumes:
prometheus-data:
driver: local
grafana-data:
driver: local
services:
# Grafana service
grafana:
image: grafana/grafana-oss:latest
container_name: grafana
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
restart: unless-stopped
# Prometheus service
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- /etc/prometheus:/etc/prometheus
- prometheus-data:/prometheus
command: "--config.file=/etc/prometheus/prometheus.yml"
restart: unless-stopped
# Code-Server service
code-server:
image: lscr.io/linuxserver/code-server:latest
container_name: code-server
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
volumes:
- /etc/opt/docker/code-server/config:/config
ports:
- 8443:8443
restart: unless-stopped
# Jenkins service
jenkins:
image: jenkins/jenkins:lts
container_name: jenkins
privileged: true
user: root
volumes:
- /etc/opt/docker/jenkins/config:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/local/bin/docker:/usr/local/bin/docker
ports:
- 8081:8080
- 50000:50000
restart: unless-stopped
# cAdvisor service
cadvisor:
image: gcr.io/cadvisor/cadvisor:v0.47.0
container_name: cadvisor
ports:
- 8080:8080
network_mode: host
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
privileged: true
restart: unless-stopped
Start the Docker containers by running:
docker compose up -d
Step 3: Configure Prometheus to Scrap Metrics ๐
Ensure your Ubuntu EC2 instance is connected to Grafana with the Prometheus data source configured as described in the previous article (#90DaysOfDevOps โ Day 74: Connecting EC2 with Grafana). Amend the prometheus.yml
file as per the below:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['172.31.0.36:9090']
# Example job for cadvisor
- job_name: 'cadvisor'
static_configs:
- targets: ['172.31.0.36:8080']
Restart Prometheus and access its dashboard to check if it can connect to the target nodes. ๐
Step 4: Access Your Grafana Dashboard ๐
Access Grafana: Open your Grafana dashboard using the public IP address or DNS name of your EC2 instance and port 3000. ๐
Import a Dashboard:
Select โDashboardsโ, click on the plus sign at the top of the menu, and hit โimport dashboardโ. โ
Go to the Grafana Dashboards Library and search for cAdvisor. Choose your preferred dashboard and copy the ID number. In my case, Iโll use the cAdvisor exporter dashboard. ๐ข
Paste the above ID number in the Grafana โimport dashboardโ and hit load to create the chosen dashboard in our Grafana app. Select your Prometheus Server and select import. ๐
Step 5: Check the Logs or Docker Container Name on the Grafana UI ๐
Go to your Grafana dashboard and check the real-time logs from your Docker containers. Congratulations! Youโve successfully sent Docker logs to Grafana, allowing you to monitor your containerized applications in real-time. ๐
By integrating Docker containers with Grafana, you can gain valuable insights into your applicationโs performance and troubleshoot any issues effectively. ๐ง
Stay Tuned for Day 76! ๐
Stay tuned for Day 76 of the #90DaysOfDevOps challenge, where weโll explore how to build a Grafana Dashboard. I hope you learned something from this blog. If you have, donโt forget to follow and click the clap ๐ button below to show your support ๐. Subscribe to my blogs so that you wonโt miss any future posts. ๐ฌ
If you have any questions or feedback, feel free to leave a comment below. Thanks for reading and have an amazing day ahead! ๐
Happy Learning! ๐