How to customise container log rotation in an RKE2 or K3s cluster
如何在 RKE2 或 K3s 集群中自定义容器日志轮换

环境
服务联系WX:136-6258-1235

A Rancher-provisioned or standalone RKE2 or K3s cluster
Rancher 配置或独立的 RKE2 或 K3s 集群

情况

In RKE2 or K3s clusters, which utilize containerd via the Container Runtime Interface (CRI), the kubelet is responsible for managing the rotation of container logs.
在通过容器运行时接口(CRI)利用容器的 RKE2 或 K3s 集群中,kubelet 负责管理容器日志的轮换。

This log rotation is configured via two kubelet options:
这种对木旋转通过两个 kubelet 选项进行配置:

  • --container-log-max-files : "the maximum number of container log files that can be present for a container" (default: 5).
    --container-log-max-files :“一个容器可以存在的最大容器日志文件数量”(默认:5)。
  • --container-log-max-size : "the maximum size (e.g. 10Mi) of container log file before it is rotated" (default: 10Mi).
    --container-log-max-size :“旋转前容器日志文件的最大大小(例如 10Mi)”(默认:10Mi)。

In this default configuration, with a maximum of 5 log files of 10MiB each, each container can retain up to 50MiB of logs.
在默认配置下,每个容器最多可保留 5 个 10MiB 的日志文件,最多可保留 50MiB 的日志。

解决方案

Note: The examples below use 4 log files of 50MiB each (totaling ~200MiB per container). Please adjust these values to meet your specific retention and disk space requirements.
注: 以下示例使用 4 个 50MiB 的日志文件(每个容器总计约 200MiB)。请调整这些数值以符合您的具体存储和磁盘空间需求。

Standalone RKE2/K3s clusters
独立 RKE2/K3s 集群

In standalone clusters, you must apply the configuration to the RKE2/K3s config file on every node in the cluster.
在独立集群中,你必须在集群中每个节点的 RKE2/K3s 配置文件中应用配置。

  1. Configure the kubelet's container-log-max-files and container-log-max-size arguments in the the RKE2 or K3s configuration file (/etc/rancher/rke2/config.yaml or /etc/rancher/k3s/config.yaml) per the following example:
    按照以下示例,在 RKE2 或 K3s 配置文件(/etc/rancher/rke2/config.yaml 或 /etc/rancher/k3s/config.yaml)中配置 kubelet 的  容器-log-max-files 和 container-log-max-size 参数:
    <span style="background-color:#efefef"><code>kubelet-arg:
      - "container-log-max-files=4"
      - "container-log-max-size=50Mi"</code></span>
  2. After updating the configuration file, you will need to restart the RKE2/K3s service to apply the changes:
    更新配置文件后,您需要重启 RKE2/K3s 服务以应用以下更改:
    • On RKE2 server nodes: systemctl restart rke2-server
      在 RKE2 服务器节点上:systemctl restart rke2-server
    • On RKE2 worker nodes: systemctl restart rke2-agent
      On RKE2 worker nodes: systemctl restart rke2-agent
    • On K3s server nodes: systemctl restart k3s
      在 K3s 服务器节点上:systemctl restart k3s
    • On K3s worker nodes: systemctl restart k3s-agent
      On K3s worker nodes: systemctl restart k3s-agent

Rancher-provisioned RKE2/K3s cluster
牧场配置 RKE2/K3s 集群

In a Rancher-provisioned cluster, the configuration can be managed at the cluster-level within Rancher:
在 Rancher 配置的集群中,配置可以在 Rancher 内部的集群层级管理:

  1. Navigate to Cluster Management within the Rancher UI and click Edit Config for the relevant RKE2/K3s cluster
    在 Rancher UI 中进入集群管理  ,点击  编辑配置  以获取相关的 RKE2/K3s 集群
  2. Under Cluster Configuration click the Advanced tab
    集群配置中点击高级标签
  3. Scroll down to For all machines, use the Kubelet args: in the section Additional Kubelet Args
    向下滚动到“ 所有机器,请使用 Kubelet args:  在”额外 Kubelet Args“部分 
  4. Click Add Argument and enter container-log-max-files=4
    点击添加参数并输入  容器-日志-最大文件=4
  5. Click Add Argument again and enter container-log-max-size=50Mi
    再次点击添加参数 ,输入容器-log-max-size=50Mi
  6. Click Save to apply the changes
    点击保存以应用更改

Rancher will perform a rolling update of the cluster nodes to apply the new kubelet arguments.
Rancher 会对集群节点进行滚动更新,以应用新的 kubelet 参数。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐