Day 75 - Sending Docker Log to Grafana

3 min read

Cover Image for Day 75 - Sending Docker Log to Grafana

Task 1

  • Install Docker and start docker service on a Linux EC2 through USER DATA.

  • Create 2 Docker containers and run any basic application on those containers (A simple todo app will work).

  • Now integrate the docker containers and share the real-time logs with Grafana (Your Instance should be connected to Grafana and the Docker plugin should be enabled on Grafana).

  • Check the logs or docker container names on Grafana UI.

To achieve this task on a Linux EC2 instance, you can use User Data and the following steps:

  1. Launch an EC2 Instance with User Data:

    • Launch an EC2 instance with a Linux-based AMI (e.g., Amazon Linux, Ubuntu).

    • In the instance launch configuration, add your User Data script. The User Data script will run automatically when the instance starts and can be used to install Docker and start the Docker service.

Here's a simple User Data script to install Docker and start the Docker service:

    #!/bin/bash
    yum update -y
    amazon-linux-extras install docker -y
    service docker start
    usermod -a -G docker ec2-user

    or
    #for ubuntu
    --------
    #!/bin/bash
    sudo apt-get update
    sudo apt install docker.io -y

Ensure that you replace the package manager (yum or apt) and package names as necessary for your Linux distribution.

  1. SSH into the EC2 Instance: Once the instance is running, SSH into it to continue the setup.

  2. Create Docker Containers: You can create two Docker containers and run a basic application in each of them. You can use Docker Compose or run individual docker run commands to do this. Below is an example of using docker run to create two containers running a simple ToDo app:

     # Create and run container 1
     docker run -d --name todo_app1 -p 8080:80 your-todo-app-image1
    
     # Create and run container 2
     docker run -d --name todo_app2 -p 8081:80 your-todo-app-image2
    

    Replace your-todo-app-image1 and your-todo-app-image2 with the actual Docker images for your ToDo app.

  3. Set Up Grafana: To share real-time logs with Grafana, you need to set up Grafana and configure it to collect Docker container logs. Here are the high-level steps:

    • Install Grafana on your EC2 instance using a package manager suitable for your Linux distribution.

    • Install and configure the Docker plugin for Grafana to collect container logs.

    • Configure data sources and create a dashboard in Grafana to visualize the logs.

The exact steps for setting up Grafana and its Docker plugin can vary depending on your specific use case and the Linux distribution you're using. Refer to Grafana's official documentation for detailed instructions.

  1. Check Logs in Grafana UI: After Grafana is set up and connected to your Docker containers, you can create a dashboard to visualize real-time logs. You can use the Docker logs as a data source in Grafana and create panels to display log data from your containers. The exact steps for creating dashboards in Grafana will depend on your specific requirements and the Grafana configuration.

Remember to secure your EC2 instance, Docker containers, and Grafana installation as appropriate for a production environment. Additionally, consider using a reverse proxy like Nginx to secure access to Grafana and Docker containers.