Skip to main content
In this lesson, you’ll learn how to deploy a new API application while ensuring its logs are automatically forwarded to Loki. This guide covers configuring log collection with Promtail, deploying the application on Kubernetes, and verifying the logs through Grafana.

Configuring Log Collection

Begin by creating a file named app-deployment.yaml to define both the log collection settings and the deployment configuration for your application. Add the following Promtail snippet to configure how log labels are applied and where Promtail should locate log files from Kubernetes pods:
This configuration directs Promtail on how to label and locate Kubernetes pod log files effectively.

Deploying the Application

Next, add the deployment configuration to the same file. The YAML snippet below describes a standard Kubernetes Deployment for an API application. This deployment uses the kodekloud/loki-demo image from Docker Hub—a demo container that generates random logs to help verify log collection via Loki.
After adding this configuration, save the app-deployment.yaml file.

Applying the Configuration

Deploy the new application using the following command:
You should see output similar to the following:
Wait a few seconds, then verify that the pod is running by executing:
A successful deployment might produce an output like this:
Ensure that all pods are up and running before proceeding to verify the logs.

Verifying Logs in Grafana

To confirm that your API pod’s logs are being captured, open Grafana and navigate to the query interface. Follow these steps:
  1. Select the pod field from the available options.
  2. Clear any current selections if necessary.
  3. Find and click on your API pod to execute the query.
The logs generated by the application mimic a typical REST API, including details such as HTTP methods, routes, and response codes. For example:
These entries confirm that logs are correctly collected and forwarded to Loki.
The image shows a Grafana dashboard displaying log data with a timeline graph at the top and detailed log entries below, including fields like app, container, and filename.
Promtail automatically collects these logs and sends them to Loki without requiring any additional configuration.
This concludes the lesson on deploying an application and monitoring its logs using Loki and Grafana. By following these steps, you ensure that your Kubernetes-based application logs are efficiently collected and easily viewed in Grafana.

Watch Video