CKS-CN 考试知识点分享(6) 日志审计
考试版本:cks-cn v1.33
Context
您必须为 kubeadm 配置的集群实施审计。
Task
首先,重新配置集群的API服务器,以便:
位于 /etc/kubernetes/logpolicy/sample-policy.yaml 的基本审计策略被使用
日志存储在 /var/log/kubernetes/audit-logs.txt
最多保留 2 个日志,保留时间为 10 天
注意:基本策略仅指定不记录的内容。
然后,编辑并扩展基本策略以记录:
RequestResponse 级别的 persistentvolumes 事件
front-apps namespace中的 configmaps事件的请求正文
Metadata 级别的所有 namespace 中的 ConfigMap 和 Secret 的更改
Metadata 级别记录所有其他请求
注意:确保API服务器使用扩展后的策略。
Answer
添加审计规则
vim /etc/kubernetes/logpolicy/sample-policy.yaml
- level: RequestResponseresources:- group: ""# Resource "pods" doesn't match requests to any subresource of pods,# which is consistent with the RBAC policy.resources: ["persistentvolumes"]- level: Requestresources:- group: "" # core API groupresources: ["configmaps"]# This rule only applies to resources in the "kube-system" namespace.# The empty string "" can be used to select non-namespaced resources.namespaces: ["front-apps"]- level: Metadataresources:- group: "" # core API groupresources: ["secrets", "configmaps"]- level: Metadata# Long-running requests like watches that fall under this rule will not# generate an audit event in RequestReceived.omitStages:- "RequestReceived"
添加审计日志保存规则
- --audit-policy-file=/etc/kubernetes/logpolicy/sample-policy.yaml- --audit-log-path=/var/log/kubernetes/audit-logs.txt- --audit-log-maxbackup=2- --audit-log-maxage=10
修改完成后,等待一会,因为是静态pod 会自动重新创建。
检查pod已经重新创建。
检查审计日志参数是否生效
ps aux |grep apiserver
检查审计日志是否正常生成
tail /var/log/kubernetes/audit-logs.txt
正常生成说明配置生效。