63. 如何向 Rancher 配置的 RKE2 或 K3s 集群中的节点添加 kubelet 配置文件
- Rancher v2.7.2+ 牧场主 v2.7.2+
- A Rancher-provisioned RKE2 or K3s cluster
Rancher 配置的 RKE2 或 K3s 集群
This article details how to configure the Kubernetes kubelet parameters by providing a custom configuration file in a Rancher-provisioned RKE2/K3s cluster. Whilst it is still possible to pass kubelet parameters as arguments, even though this is noted as deprecated in the Kubernetes documentation, the following steps enable deployment via a configuration file.
本文详细介绍了如何在 Rancher 配置的 RKE2/K3s 集群中提供自定义配置文件来配置 Kubernetes 的 kubelet 参数。虽然仍然可以将 kubelet 参数作为参数传递,尽管 Kubernetes 文档中注明已弃用,但以下步骤可以通过配置文件实现部署。
Per the RKE2 and K3s documentation there are two methods for passing a kubelet configuration file:
根据 RKE2 和 K3s 文档,传递 kubelet 配置文件有两种方法:
- In Kubernetes v1.32+ a drop-in configuration file can be written to the directory /var/lib/rancher/<rke2/k3s>/agent/etc/kubelet.conf.d/
在 Kubernetes v1.32+中,可以直接写入配置文件到/var/lib/rancher/<rke2/k3s>/agent/etc/kubelet.conf.d/ - The kubelet argument config can be set to reference a specific kubelet configuration file at another location
kubelet 参数配置可以设置为引用另一个位置的特定 kubelet 配置文件
The steps below make use of a machineSelectorFiles block on the cluster resource, to enable deployment of a file across nodes in a Rancher-provisioned cluster.
以下步骤利用集群资源上的 machineSelectorFiles 块 ,实现文件在 Rancher 配置集群中的节点间部署。
In this example the imageMaximumGCAge parameter is configured to 12h.
在本例中,imageMaximumGCAge 参数配置为 12 小时。
1. Create a ConfigMap or Secret in the Rancher local cluster
1. 在 Rancher 本地集群中创建 ConfigMap 或 Secret
Create a ConfigMap or Secret within the fleet-default Namespace in the Rancher local cluster, containing your custom kubelet configuration.
在 theRancher 本地集群的 fleet 默认命名空间内创建一个 ConfigMap 或 Secret,包含你自定义的 Kubelet 配置。
The Secret or ConfigMap must meet the following requirements:
秘密或配置图必须满足以下要求:
- It must exist within the same fleet workspace as the cluster itself, for Rancher-deployed clusters this is fleet-default by default.
它必须与集群本身存在于同一个舰队工作区内,对于 Rancher 部署的集群,默认是舰队默认。 - It must have the annotation
rke.cattle.io/object-authorized-for-clusters: <cluster-name1>,<cluster-name2>with a comma separated list of the clusters which are permitted to use it.
它必须带有逗号分隔的注释rke.cattle.io/object-authorized-for-clusters: <cluster-name1>,<cluster-name2>,列出允许使用该列表的簇。
In the example below a ConfigMap is created, with access granted to the cluster named rke2custom, and a kubelet configuration to define imageMaximumGCAge.
在下面的示例中,创建一个配置图,授予名为 rke2custom 的集群访问权限,并有一个 kubelet 配置来定义 imageMaximumGCAge。
<span style="color:#000000"><span style="background-color:#ffffff"><span style="background-color:#efefef"><code>apiVersion: v1
kind: ConfigMap
metadata:
name: custom-kubelet-config
namespace: fleet-default
annotations:
<a data-cke-saved-href="http://rke.cattle.io/object-authorized-for-clusters" href="http://rke.cattle.io/object-authorized-for-clusters">rke.cattle.io/object-authorized-for-clusters</a>: rke2custom
data:
kubelet-config.yaml: |
apiVersion: <a data-cke-saved-href="http://kubelet.config.k8s.io/v1beta1" href="http://kubelet.config.k8s.io/v1beta1">kubelet.config.k8s.io/v1beta1</a>
kind: KubeletConfiguration
imageMaximumGCAge: 12h</code></span></span></span>
2a. In Kubernetes v1.32+ 2a。在 Kubernetes v1.32+
The recommended method for passing a custom kubelet configuration in Kubernetes v1.32+ is the use of a drop-in file, which removes the requirement to pass an additional config argument to instruct kubelet to read the file.
Kubernetes v1.32+ 中推荐的自定义 kubelet 配置传递方法是使用直接插入文件,这样就无需通过额外的配置参数来指示 kubelet 读取该文件。
In the example below, the kubelet configuration ConfigMap created above is written to the kubelet configuration drop-in directory on the nodes of an RKE2 cluster (/var/lib/rancher/rke2/agent/etc/kubelet.conf.d/).
在下面的示例中,上述创建的 kubelet 配置 ConfigMap 被写入到 RKE2 集群节点上的 kubelet 配置插入目录(/var/lib/rancher/rke2/agent/etc/kubelet.conf.d/)。
To define this machineSelectorFiles block in a Rancher-provisioned RKE2 or K3s cluster, navigate to Cluster Management in the Rancher UI and click Edit Config for the relevant cluster. Then click Edit as YAML to enter the machineSelectorFiles block, before clicking Save to apply the change.
要在 Rancher 配置的 RKE2 或 K3s 集群中定义该 machineSelectorFiles 块,请在 Rancher UI 中进入集群管理 ,点击相应集群的编辑配置 。然后点击 “编辑为 YAML 以进入 machineSelectorFiles 块,然后点击保存应用更改。
<span style="color:#000000"><span style="background-color:#ffffff"><span style="background-color:#efefef"><code>[...]
spec:
[...]
rkeConfig:
[...]
machineSelectorFiles:
- fileSources:
- configMap:
items:
- key: kubelet-config.yaml
path: /var/lib/rancher/rke2/agent/etc/kubelet.conf.d/01-custom.conf
name: custom-kubelet-config
[...]</code></span></span></span>
2b. In Kubernetes <v1.32 2b。在 Kubernetes <v1.32中
In Kubernetes < v1.32, it is not possible to use the drop-in file feature, so the file must be written to a location (via a machineSelectorFiles block) which is then explicitly passed to the kubelet via the config argument (using a machineSelectorConfig block).
在 Kubernetes < v1.32 中,无法使用直接插入文件功能,因此文件必须通过 machineSelectorFiles 块写入某个位置,然后通过配置参数(使用 machineSelectorConfig 块)显式传递给 kubelet。
In the example below, the kubelet configuration ConfigMap created above is written to the file /etc/rancher/rke2/kubelet-config-custom.yaml on the nodes of an RKE2 cluster, which is then referenced in the config argument passed to the kubelet.
在下面的示例中,上面创建的 kubelet 配置 ConfigMap 被写入 RKE2 集群节点上的 /etc/rancher/rke2/kubelet-config-custom.yaml 文件,然后在传递给 kubelet 的配置参数中引用该配置文件。
To define the machineSelectorFiles and machineSelectorConfig blocks in a Rancher-provisioned RKE2 or K3s cluster, navigate to Cluster Management in the Rancher UI and click Edit Config for the relevant cluster. Then click Edit as YAML to enter the blocks, before clicking Save to apply the change.
要在 Rancher 配置的 RKE2 或 K3s 集群中定义 machineSelectorFiles 和 machineSelectorConfig 块,请在 Rancher UI 中进入集群管理 ,点击相应集群的编辑配置 。然后点击 “编辑为 YAML”输入块,再点击 保存应用更改。
<span style="color:#000000"><span style="background-color:#ffffff"><span style="background-color:#efefef"><code>[...]
spec:
[...]
rkeConfig:
[...]
machineSelectorConfig:
- config:
kubelet-arg:
- config=/etc/rancher/rke2/kubelet-config-custom.yaml
[...]
machineSelectorFiles:
- fileSources:
- configMap:
items:
- key: kubelet-config.yaml
path: /etc/rancher/rke2/kubelet-config-custom.yaml
name: custom-kubelet-config
[...]</code></span></span></span>
更多推荐


所有评论(0)