Day 74- 90DaysOfDevOps

Connecting EC2 with Grafana

·

3 min read

Day 74- 90DaysOfDevOps

Hey Learners! Welcome back. In the previous challenge, we installed Grafana on the EC2 instance. Moving forward in this challenge we'll add Loki integrated with Promtail as a data source and create Dashboards in Grafana to monitor. Let's start...

Prerequisite:-

  • Grafana should be installed Refer DAY73Challenge

  • Docker should be present with appropriate permissions

Steps:-

  1. Download the Config file for Loki to run Loki in the Docker container

  2. Download the Config file for Promtail to run Promtail in the Docker container

  3. Add Data source in Grafana and check logs

  4. Create Dashboard

Step 1- Download the Config file for Loki to run Loki in the Docker container

Use the following command to download and store Loki's configuration file in the local

wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml

Run the docker container using the following command.

sudo docker run -d --name loki -v $(pwd):/mnt/config -p 3100:3100 grafana/loki:2.8.0 --config.file=/mnt/config/loki-config.yaml

Allow port 3100 in the EC2 instance's security group and check in the browser. Refer following screenshot.

Step 2- Download the Config file for Promtail to run Promtail in the Docker container

Use the following command to download and store Promtail's configuration file in the local

 wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/clients/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml

Run the Docker container using the following command

docker run -d --name promtail -v $(pwd):/mnt/config -v /var/log:/var/log --link loki grafana/promtail:2.8.0 --config.file=/mnt/config/promtail-config.yaml

Step 3- Add Data source in Grafana and check logs

As we successfully run the container for Loki and Promtail we have to add Loki as a Data source in Grafana

  1. Access your Grafana web interface <PublicIP:3000>

  2. Go to Home Click on DATA SOURCES to add data source

  3. Choose Loki from the list. Enter the URL as http://localhost:3100 as we running Loki on the same host machine.

  4. Check the system logs sent to Grafana by Loki as mentioned in the config file.

    Click on New Dashboard click Add Visualization then select Loki as the Data source. Select Job in the Label filter option. Refer below screenshots

Step 4- Create Dashboard

  1. Click on Dashboards and select New dashboard from the New button as shown below.

  2. Select Loki as a data source and try to add all logs as shown below

    Click Apply to apply and place the panel where you want to place.

  3. We have to add the Grafana log path in the Promtail configuration file to display Grafana logs. Make changes in the Promtail conf file as shown below

    After changing the conf file restart the Oromtail container using the sudo docker restart promtail command. Make sure Grafana logs show while selecting Loki as a data source.

  4. Go to the Grafana dashboard and click Add to add a new Panel.

    We'll add a new panel of log errors for Grafana and then add a new panel for the number of errors encountered in the last 5 min

  5. The final simple dashboard will look like below

This is the simple dashboard but you can explore Grafana and make new dashboards.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- Hashnode LinkedIn Github

Happy Learning!