
Loki is a log aggregation system. The Loki stack comprises of three components that work with Grafana and Promtail to enable application log monitoring within a Kubernetes cluster.
Loki is the core component responsible for log ingestion, storage, and query processing while Promtail acts as the agent responsible for collecting log information from cluster sources. In addition, the Grafana dashboard serves as the main interface for querying, visualizing, and exploring the log data stored in Loki.
This article explains how to monitor application logs in Kubernetes using Loki. You are to deploy the Loki stack in a Vultr Kubernetes Engine (VKE) cluster, deploy a sample application, and monitor logs using the Grafana dashboard.
Before you begin:
Deploy a Vultr Kubernetes Engine (VKE) cluster with at least 3 nodes and 4096 MB per worker node
Deploy a OneClick Docker instance using the Vultr Marketplace application to use as the management workstation.
Create a new Vultr Container Registry to store containerized application images.
Access your management server using SSH as a non-root sudo user.
Install and Configure Kubectl to access your VKE cluster.
Install the Helm package manager.
Add the grafana Helm repository to your sources.
Install the Loki stack to your cluster using the Helm chart.
The above command installs the Loki stack with Grafana enabled, and sets up a LoadBalancer service which in turn creates a Vultr Load Balancer for external access. In addition, a new namespace loki-stack is created to centralize all Loki resources.
When successful, your output should be similar to the one below:
Wait for at least 2 minutes for the deployment process to complete. Then, view all Pods in the loki-stack namespace.
Output:
To test the Loki stack functionalities, follow the steps below to create a sample Golang application to run and monitor within your Kubernetes cluster.
Create a new application directory.
Switch to the directory.
Create a new main application file main.go using a text editor such as Nano.
Add the following contents to the file.
Save and close the file.
The above application code defines an HTTP server that runs on the host port 8080 using the net/http standard library package. The main function sets up two HTTP handlers, the root (/) and /error paths. When triggered, the root handler responds with a "Hello, World!" message and logs information about the received request, while the /error handler simulates an error by returning a simulated internal server error (HTTP status code 500). The server is configured to listen on port 8080.
Create a new dependency file go.mod.
Add the following contents to the file.
Save and close the file.
Create a Dockerfile to define the application container environment.
Add the following contents to the file.
Save and close the file.
The above configuration defines a multi-stage build for the Go application. Within the first stage, it uses the official Golang base image to build the application. Then, it uses /app as the working directory, copies the go.mod and main.go files, and builds the Go application with CGO disabled, to create the go-app binary.
The second stage uses the gcr.io/distroless/base-debian10 image, / as the working directory. Then. it copies the compiled binary from the build stage to /go-app that works as the application entry point that listens for connections on port 8080.
Build the Docker image.
Export your Vultr Container Registry access credentials as environmental variables to store Docker images.
Log in to your Vultr Container Registry.
Output:
Tag the local Docker image with the Vultr Container Registry repository name.
Push the image to your registry.
Access your Vultr Container Registry control panel in the Vultr Console.
Navigate to the Docker/Kubernetes tab.
Click Generate Kubernetes YAML within the Docker Credentials For Kubernetes section to generate a new YAML file with your access details.
Open the downloaded file and copy all contents to your clipboard.
Create a new Secret resource file secret.yaml.
Paste your generated Kubernetes YAML contents to the file. For example:
Apply the Secret to your VKE cluster.
When successful, all cluster resources can access and pull data from your Vultr Container Registry repositories.
Create a new Deployment resource file app.yaml.
Add the following contents to the file. Replace testreg with your actual Vultr Container Registry name.
Save and close the file.
The above configuration creates a new Deployment and Service resource for the Go web application with the following specifications:
Deployment specifies a container within a Pod that uses the Go web application image from your Vultr Container Registry.LoadBalancer resource to expose the application port 8080 and direct traffic to Pods with the go-web-app label.imagePullSecrets section uses the vultr-cr-credentials Secret variable to authenticate and pull the container image from your Vultr Container Registry repository.Deploy the application to your cluster.
Wait for at least 1 minute for the deployment process to complete, then, view all Pods with the go-web-app label.
Your output should be similar to the one below:
Retrieve the default Grafana login password from the loki-grafana Secret.
Copy the generated administrator password similar to the one below:
View the loki-grafana Service and retrieve the assigned Vultr Load Balancer IP Address for external access.
Your output should be similar to the one below:
Visit your cluster Load Balancer IP to access the Grafana web interface using a web browser such as Chrome.
When prompted, enter the following details in the respective fields to log in.
adminYour Generated PasswordWhen successful, navigate to Explore within the Grafana dashboard.
Open your SSH session and view the go-web-app Load Balancer Service IP Address. Access the application endpoint using Vultr Load Balancer. To get the Load Balancer IP, run the below command:
Your output should be similar to the one below:
Test access to the application on port 8080 using the Curl utility.
Output:
To generate more HTTP traffic, invoke the application endpoint using the for loop.
To simulate an error scenario, invoke a different endpoint such as error.
Generate more HTTP traffic for the error scenario using the for loop.
Switch to your web browser session, and access the Grafana dashboard.
Within the Explore section, enter the following query in the Log browser field.
Click Run query to execute the statement and view all application access metrics.
You have deployed the Loki stack to a VKE cluster and monitored application logs using Grafana. In the process, you containerized a sample application image to the Vultr Container Registry to experiment with different Loki stack functionalities. For more information and implementation samples, visit the Loki documentation.
For more information, visit the following resources.
0 Comments
Be the first to comment and share your perspective with the community.