K3s Logs | Where are K3s Logs and How to Save Them

Posted February 28, 2024
K3s Logs | Where are K3s Logs and How to Save Them

K3s is one of the most Lightweight Kubernetes distributions, A K3s server only needs a few resources to get up and running. Now a running K3s server produces logs and adds to your system.

Logs tell a story of how the K3s server is running. Now, by default, K3s will manage its logs using systemd. This is where K3s logs are managed. To cees them you must use the journalctl command.

In this guide you will learn where to find K3s logs and if you can change them to other transports.

Accessing K3s Logs Using Systemd Journal

Systemd Journal is the default K3s log manager. You use Journal to access them via the journalctl command.

On Linux, Systemd captures logs from K3s and stores them in its journal.

To view K3s logs:

  • Ensure K3s is running:

Accessing K3s Logs Using Systemd Journal

  • Examine the systemd unit file for K3s:

This should display the content of the unit file and any K3S logging directives. Look for StandardOutput and StandardError directives. They indicate where the logs are directed:

systemctl cat k3s
  • Use the journalctl command as follows:
sudo journalctl -u k3s

This command retrieves logs specifically for the K3s service as follows:

Filtering K3s Logs

Filtering K3s Logs

You can add options to journalctl for filtering, such as specifying a time range or filtering by severity level.

On the same command use options for filtering logs as follows:

  • filter logs based on when they were generated.
sudo journalctl -u k3s --since "1 hour ago"
  • Severity Level such as emerg, alert, crit, err, warning, notice, info, debug. Example using err severity:
sudo journalctl -u k3s -p err
  • Use Follow Mode to continuously monitor logs as they are written to the journal:
sudo journalctl -u k3s -f

How to add K3s Logs to Files

If you want an easier way to save these logs you can use specific log files. By default, K3s may log to stdout/stderr. However, if you’ve configured K3s to log to a file directly, you should check your K3s configuration for the log file location.

Find K3s configuration files in the /etc/rancher/k3s directory and add the log file path:

The Best Solution to Accessing K3s Logs

A handy way to manage logs is to use a Log manger. You can set up dashboards such as Prometheus and Grafana. This will enhance K3s Monitoring and Log management. Here you get:

  • Systemd services statistics.
  • CPU / Memory / Filesystem usage as well as individual pod

The Best Solution to Accessing K3s Logs

To set up K3s with Prometheus and Grafana, check this guide.

Conclusion

I hope this guide gave you an overview of K3s logs.

K3s Logs | Where are K3s Logs and How to Save Them

Written By:

Joseph Chege