In a Kubernetes cluster running an alternative container runtime, such as containerd, instead of Docker, the kubelet manages container logs. By default container logs are not rotated by the kubelet, but this can be achieved 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.