Kubernetes Exercise: Deploy Loki Stack with Log Collector and Sample App
Objectives
By the end of this exercise, you will:
- Deploy the Loki Stack (Loki, Promtail, and Grafana) using Helm.
- Deploy a sample application that emits logs.
- View the application logs in Grafana using LogQL.
Prerequisites
- A Kubernetes cluster (minikube, kind, cloud, etc.)
kubectl
installed and configuredhelm
installed (https://helm.sh/docs/intro/install/)- Optional: port 3000 open on localhost for Grafana
Step 1: Add and Install Loki Stack with Helm
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
# Create a monitoring namespace
kubectl create namespace monitoring
# Install the Loki stack (Loki + Promtail + Grafana)
helm install loki-stack grafana/loki-stack --namespace monitoring \
--set grafana.enabled=true \
--set promtail.enabled=true \
--set loki.persistence.enabled=false \
--set grafana.service.type=NodePort
Wait until everything is ready:
kubectl get pods -n monitoring
Step 2: Access Grafana UI
Port forward Grafana:
kubectl port-forward -n monitoring service/loki-stack-grafana 3000:80
Then open: http://localhost:3000
Login credentials:
- User:
admin
- Password:
prom-operator
(default for Loki Helm chart)
Step 3: Deploy a Sample App with Logs
Here’s a simple "log generator" app that prints messages to stdout.
apiVersion: apps/v1
kind: Deployment
metadata:
name: log-emitter
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: log-emitter
template:
metadata:
labels:
app: log-emitter
spec:
containers:
- name: logger
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo Hello from $(hostname); sleep 5; done"]
Apply it:
kubectl apply -f log-emitter.yaml
Step 4: View Logs in Grafana
Add the Loki data source (if not already added)
- Go to Grafana UI → ⚙️ Configuration → Data Sources
- Click Add data source → choose Loki
- URL:
http://loki-stack:3100
- Click Save & Test
Explore logs
- Go to Explore
- Select Loki as the datasource
- Use a query like:
{app="log-emitter"}
You should see logs like:
Hello from log-emitter-xxxx
Bonus Tasks
- Scale the deployment to
replicas: 3
and observe the logs. - Modify the log message (e.g., include timestamps or random IDs).
- Add a
label
to logs and filter by it in LogQL.
Cleanup
kubectl delete deployment log-emitter
helm uninstall loki-stack -n monitoring
kubectl delete namespace monitoring