下游集群开启 API 审计日志
Kubernetes 审计提供了由 Kube-apiserver 执行的与安全相关的、按时间顺序排列的集群审计记录。Kube API 会在请求执行的每个阶段都生成一个事件,然后根据策略进行预处理并保存,审计策略配置了要记录的内容。
你可能希望将审计日志配置为遵守互联网安全中心 (CIS) Kubernetes 基准控制的一部分。
有关配置的详细信息,请参阅 Kubernetes 官方文档。
- RKE2
- K3s
方法1(建议):设置 machineGlobalConfig
字段的 audit-policy-file
你可以在配置文件中设置 audit-policy-file
,Rancher 会将该文件保存在 Control Plane 节点的 /var/lib/rancher/rke2/etc/config-files/audit-policy-file
目录中,并在 RKE2 服务器中配置相应的选项。
例子:
apiVersion: provisioning.cattle.io/v1
kind: Cluster
spec:
rkeConfig:
machineGlobalConfig:
audit-policy-file: |
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
resources:
- group: ""
resources:
- pods
方法2:直接使用 machineSelectorFiles
和 machineGlobalConfig
配置
Rancher v2.7.2 及以上版本提供此功能。
你可以使用 machineSelectorFiles
将审计策略文件传递到 Control Plane 节点,并使用 machineGlobalConfig
设置 kube-apiserver 的选项。
在此之前,你需要创建 Secret 或 ConfigMap 作为审计策略的来源。
Secret 或 ConfigMap 必须满足以下要求:
- 必须位于 Cluster 对象所在的
fleet-default
命名空间中。 - 它必须含有 Annotation
rke.cattle.io/object-authorized-for-clusters: <cluster-name1>,<cluster-name2>
,以允许目标集群使用它。
Rancher Dashboard 提供了易用的表单页面用于创建 Secret 或 ConfigMap。
例子:
apiVersion: v1
data:
audit-policy: >-
IyBMb2cgYWxsIHJlcXVlc3RzIGF0IHRoZSBNZXRhZGF0YSBsZXZlbC4KYXBpVmVyc2lvbjogYXVkaXQuazhzLmlvL3YxCmtpbmQ6IFBvbGljeQpydWxlczoKLSBsZXZlbDogTWV0YWRhdGE=
kind: Secret
metadata:
annotations:
rke.cattle.io/object-authorized-for-clusters: cluster1
name: <name1>
namespace: fleet-default
可以通过编辑集群 YAML 的 machineSelectorFiles
和 machineGlobalConfig
字段来启用和配置审计日志。
例子:
apiVersion: provisioning.cattle.io/v1
kind: Cluster
spec:
rkeConfig:
machineGlobalConfig:
kube-apiserver-arg:
- audit-policy-file=<customized-path>/dev-audit-policy.yaml
- audit-log-path=<customized-path>/dev-audit.logs
machineSelectorFiles:
- fileSources:
- configMap:
name: ''
secret:
items:
- key: audit-policy
path: <customized-path>/dev-audit-policy.yaml
name: dev-audit-policy
machineLabelSelector:
matchLabels:
rke.cattle.io/control-plane-role: 'true'
你还可以使用指令 machineSelectorConfig
和适当的 machineLabelSelectors
来达到相同的效果。
有关集群配置的更多信息,请参阅 RKE2 集群配置参考页面。
Rancher v2.7.2 及以上版本提供此功能。
你可以使用 machineSelectorFiles
将审计策略文件传递到 Control Plane 节点,并使用 machineGlobalConfig
设置 kube-apiserver 的选项。
在此之前,你需要创建 Secret 或 ConfigMap 作为审计策略的来源。
Secret 或 ConfigMap 必须满足以下要求:
- 必须位于 Cluster 对象所在的
fleet-default
命名空间中。 - 它必须含有 Annotation
rke.cattle.io/object-authorized-for-clusters: <cluster-name1>,<cluster-name2>
,以允许目标集群使用它。
例子:
apiVersion: v1
data:
audit-policy: >-
IyBMb2cgYWxsIHJlcXVlc3RzIGF0IHRoZSBNZXRhZGF0YSBsZXZlbC4KYXBpVmVyc2lvbjogYXVkaXQuazhzLmlvL3YxCmtpbmQ6IFBvbGljeQpydWxlczoKLSBsZXZlbDogTWV0YWRhdGE=
kind: Secret
metadata:
annotations:
rke.cattle.io/object-authorized-for-clusters: cluster1
name: <name1>
namespace: fleet-default
可以通过编辑集群 YAML 的 machineSelectorFiles
和 machineGlobalConfig
字段来启用和配置审计日志。
例子:
apiVersion: provisioning.cattle.io/v1
kind: Cluster
spec:
rkeConfig:
machineGlobalConfig:
kube-apiserver-arg:
- audit-policy-file=<customized-path>/dev-audit-policy.yaml
- audit-log-path=<customized-path>/dev-audit.logs
machineSelectorFiles:
- fileSources:
- configMap:
name: ''
secret:
items:
- key: audit-policy
path: <customized-path>/dev-audit-policy.yaml
name: dev-audit-policy
machineLabelSelector:
matchLabels:
rke.cattle.io/control-plane-role: 'true'
你还可以使用指令 machineSelectorConfig
和适当的 machineLabelSelectors
来达到相同的效果。
有关集群配置的更多信息,请参阅 K3s 集群配置参考 页面。