Skip to content

mloning/minimal-python-app-monitoring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal Python app monitoring on k8s with Grafana Loki

Requirements

Start minikube

minikube start

 Install monitoring stack

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
helm upgrade loki-stack grafana/loki-stack \
    --install \
    --namespace=monitoring \
    --create-namespace \
    --values ./helm/loki-stack-values.yaml

Wait for all resource to be available: kubectl get pods -n monitoring -w.

For details, see the loki-stack Helm chart.

Connect to Grafana dashboard

kubectl port-forward service/loki-stack-grafana 3000:80 -n monitoring

where 80 is the Grafana service port. To look up the port, run: kubectl get service -n monitoring | grep grafana.

You can now log in to dashboard at http://localhost:3000/.

To get the login credentials, run:

kubectl get secret loki-stack-grafana -n monitoring -o jsonpath='{.data.admin-user}' | base64 --decode; echo
kubectl get secret loki-stack-grafana -n monitoring -o jsonpath='{.data.admin-password}' | base64 --decode; echo

where loki-stack-grafana is the name of the config map.

Install minimal Python app

The application is based on an example from the Kubernetes blog.

Test app in Docker (optional)

docker build . -t minimal-app:latest
docker run --rm -p 5001:5000 minimal-app:latest

To check the app in Docker, open http://localhost:5001/.

Build image

eval $(minikube docker-env)
docker build . -t minimal-app:latest

The first line enables us to use local images in minikube, when we set the image pull policy to "Never" in the k8s manifest files. This works by pointing your terminal's Docker client to the Docker client used inside minikube.

Run app in Kubernetes

kubectl apply -f ./k8s/
kubectl port-forward service/minimal-app-service 5001:6000 

To check the app, open http://localhost:5001/ (or http://localhost:5001/docs for API docs).

View logs

To view logs on the Grafana dashboard, go to the Explore page and select Loki as the data source.

To filter out the application logs, run this LogQL query:

{app="minimal-app"} |= `` 

To view logs in k8s, run:

kubectl logs -f -l app=minimal-app

 Clean up

minikube stop
minikube delete

About

Minimal Python app monitoring on k8s with Grafana Loki

Topics

Resources

Stars

Watchers

Forks