Hi š
This is a short tutorial describing how to monitor your Kubernetes cluster container logs using Loki stack. But why? Because it is easier to view, filter your logs in Grafana and to store them persistently in Loki rather than viewing them in a terminal.
Letās get started! Assuming you already have Microk8s installed, enable the following addons:
You can enable an add-on by running microk8s enable. Ex: microk8s enable dns
|
|
Note: Microk8s comes with a bundled kubectl and helm3. Just run microk8s kubectl or microk8s helm3. If you want to use your host kubectl you can configure it via: microk8s config > ~/.kube/config.
Warning: Be extra careful when running the microk8s config > ~/.kube/config command because it will overwrite the old config file.
Then proceed by installing Loki. Loki will store all the logs using object storage. This is efficient but the trade-off is that you canāt do complex aggregations and searches against your data. We are going to install Loki for exploration purposes but if youāre looking for a production ready version, check out the loki distributed helm chart.
Run the following helm commands to install Loki. You may want to install helm or use microk8s helm3 command.
|
|
You should get the following pods and services by running kubectl get pods and kubectl get services:
|
|
Now, we can safely install Promtail. Promtail will import all the container logs into Loki and it should work auto-magically by auto-discovering all the pods that are running inside your cluster.
To let Promtail know about our existing Lokiās address, we can give it the service URL: http://loki-headless.default.svc.cluster.local:3100/loki/api/v1/push.
|
|
Finally, we need to visualize the logs using Grafana. Install it by running the helm command and then, edit the service to change its type from ClusterIP to NodePort.
Changing the service type to NodePort will allow you to visit Grafana in your browser without the need of adding an ingester.
āāTo use vscode as the default editor export the following environment variable: KUBE_EDITOR=code -w
|
|
Note: If youāre on Windows to access the service you will need to run kubectl cluster-info and use the IP address of the cluster. On Linux you should be able to access http://localhost:32204.
|
|
To access Grafana visit: http://172.20.138.170:32204 where 32204 is the serviceās NodePort.
Grab your Grafana admin password by following the instructions from the helm notes. The notes are displayed after Grafana has been installed. If you donāt have base64 on your OS check out CyberChef, it can decode base64 text.
After youāve successfully logged in, head to Settings -> DataSources and add the Loki data source.
Head back to the Explore menu and display Lokiās logs using the Loki data source in Grafana. You can click log browser to view all available values for the app label.
Promtail should now import logs into Loki and create labels dynamically for each newly created container. If you followed along, congratulations!
Thanks for reading and happy hacking! š§