How to enable container log rotation with k3s or containerd
This document (000020071) is provided subject to the disclaimer at the end of this document.
In a Kubernetes cluster running an alternative container runtime, such as containerd, instead of Docker, the kubelet manages container logs. The kubelet default values in relation to log rotation can be found in the upstream kubelet | Kubernetes documentation. These values can be adjusted by adding flags to the kubelet process.
These steps have been validated for a k3s cluster using the default containerd runtime, in theory these same flags should work for any Kubernetes cluster which does not use Docker as the container runtime.
Two kubelet flags need to be added to configure log rotation, the flags will take effect only at start time.
In the case of k3s, passing the needed flags can be done a number of ways, the most common perhaps is with the
INSTALL_K3S_EXEC environment variable when installing k3s as a service. These same flags can be added to a previous install command to update the service configuration of an existing install of k3s.
Note When updating an existing k3s install, the following command will restart k3s.
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--kubelet-arg "container-log-max-files=4" --kubelet-arg "container-log-max-size=50Mi"" sh -
However, flags can also be supplied to the k3s binary directly if a service is not being used. A restart of k3s is required, using the updated flags.
k3s server --kubelet-arg container-log-max-files=4 --kubelet-arg container-log-max-size=50Mi
Note please adjust the values to suit your needs, for demonstration purposes the above commands used 4 log files of 50MB, allowing for 200MB of total space to be retained per container.
Please reference the k3s and kubelet documentation pages to find more information on these flags.
This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.
- Document ID:000020071
- Creation Date: 23-Aug-2022
- Modified Date:23-Aug-2022
- SUSE Rancher
For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com