Day 75 of 90 Days of DevOps Challenge: Sending Docker Logs to Grafana

Today, we will take your monitoring skills to the next level by integrating Docker with Grafana. We'll install Docker on a Linux EC2 instance, run two Docker containers, and send their logs to Grafana.


Task Overview

  1. Install Docker on a Linux EC2 Instance via User Data.

  2. Create and Run Two Docker Containers (Todo App).

  3. Integrate Docker Logs with Grafana.


Step-by-Step Guide

Part 1: Install Docker via User Data

  1. Launch a New Linux EC2 Instance:

    • Use the following User Data script to install Docker and start the Docker service:
    #!/bin/bash
    sudo apt-get update -y
    sudo apt-get 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-get update -y
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io
    sudo systemctl start docker
    sudo systemctl enable docker
    sudo usermod -aG docker $USER
  1. Connect to the EC2 Instance:

    • Use SSH to connect to your instance after it's running:
    ssh -i "your-key.pem" ec2-user@your-linux-ec2-public-ip

Part 2: Create and Run Two Docker Containers

  1. Pull the Todo App Docker Image:

    • You can use a simple Todo application image from Docker Hub. For example:
    docker run -d --name todo-app1 -p 8081:80 <docker-todo-app-image>
    docker run -d --name todo-app2 -p 8082:80 <docker-todo-app-image>
  • Replace <docker-todo-app-image> with an actual Todo app image name from Docker Hub. You can use a simple web application like todobackend or any other lightweight Todo app.
  1. Verify the Containers are Running:

     docker ps
    

Part 3: Integrate Docker Logs with Grafana

The challenge suggests uing Grafana Docker Plugin however its no longer in use and now we have to get the logs using loki or prometheus

  1. Install Loki:

    You can run Loki as a Docker container:

     docker run -d --name=loki -p 3100:3100 grafana/loki:latest -config.file=/etc/loki/local-config.yaml
    
  2. Install Promtail:

    Promtail is a log collector for Loki, which helps to collect logs from Docker containers.

     docker run -d --name=promtail -v /var/log:/var/log -v /etc/promtail:/etc/promtail grafana/promtail:latest -config.file=/etc/promtail/config.yml
    
  3. Configure Promtail:

    Promtail configuration (/etc/promtail/config.yml) to scrape Docker logs:

     server:
       http_listen_port: 9080
       grpc_listen_port: 0
    
     clients:
       - url: http://<LOKI_HOST>:3100/loki/api/v1/push
    
     scrape_configs:
       - job_name: docker
         docker_sd_configs:
           - host: unix:///var/run/docker.sock
         relabel_configs:
           - source_labels: [__meta_docker_container_name]
             target_label: container
    
  4. Add Loki as a Data Source in Grafana:

    1. Go to your Grafana instance.

    2. Navigate to Configuration → Data Sources. Click Add Data Source and search for Loki.

    3. Enter http://<LOKI_HOST>:3100 as the URL and click Save & Test.

  5. Create a Dashboard:

    1. Create a new dashboard and add a panel.

    2. In the Query section, select Loki as the data source.

    3. Use LogQL queries to visualize your Docker logs, e.g.,

       {job="docker"}
      
  6. Visualize Docker Logs:

    • Now, you should be able to see the real-time logs from both Docker containers in your Grafana dashboard.

Conclusion

You have successfully set up Docker on a Linux EC2 instance, created two containers running a Todo app, and integrated their logs with Grafana. This project will significantly enhance your resume and monitoring skills. Let me know if you need any help with configurations or troubleshooting!