- Kubernetes 审计功能提供了与安全相关的按时间顺序排列的记录集,记录每个用户、管理员 或系统其他组件影响系统的活动顺序。 它能帮助集群管理员处理以下问题:
-
发生了什么?
-
什么时候发生的?
-
谁触发的?
-
活动发生在哪个(些)对象上?
-
在哪观察到的?
-
它从哪触发的?
-
活动的后续处理行为是什么?
- 每个请求都可以用相关的 “stage” 记录。已知的 stage 有:
-
RequestReceived - 事件的 stage 将在审计处理器接收到请求后,并且在委托给其余处理器之前生成。
-
ResponseStarted - 在响应消息的头部发送后,但是响应消息体发送前。 这个阶段仅为长时间运行的请求生成(例如 watch)。
-
ResponseComplete - 当响应消息体完成并且没有更多数据需要传输的时候。
-
Panic - 当 panic 发生时生成。
- 说明: 审计日志记录功能会增加 API server 的内存消耗,因为需要为每个请求存储审计所需的某些上下文。 此外,内存消耗取决于审计日志记录的配置。审计策略
- 审计政策定义了关于应记录哪些事件以及应包含哪些数据的规则。 审计策略对象结构定义在 audit.k8s.io API 组 处理事件时,将按顺序与规则列表进行比较。第一个匹配规则设置事件的 “审计级别”。已知的审计级别有:
-
None - 符合这条规则的日志将不会记录。
-
Metadata - 记录请求的元数据(请求的用户、时间戳、资源、动词等等), 但是不记录请求或者响应的消息体。
-
Request - 记录事件的元数据和请求的消息体,但是不记录响应的消息体。 这不适用于非资源类型的请求。
-
RequestResponse - 记录事件的元数据,请求和响应的消息体。这不适用于非资源类型的请求。
-
你可以使用 --audit-policy-file 标志将包含策略的文件传递给 kube-apiserver。 如果不设置该标志,则不记录事件。 注意 rules 字段 必须 在审计策略文件中提供。没有(0)规则的策略将被视为非法配置。
以下编辑一个审计策略文件的示例:
vim /etc/kubernetes/audit-policy.yaml
首先看官网的列子
apiVersion: audit.k8s.io/v1 # This is required.
kind: Policy
# Don't generate audit events for all requests in RequestReceived stage.
omitStages:
- "RequestReceived"
rules:
# Log pod changes at RequestResponse level
- level: RequestResponse
resources:
- group: ""
# Resource "pods" doesn't match requests to any subresource of pods,
# which is consistent with the RBAC policy.
resources: ["pods"]
# Log "pods/log", "pods/status" at Metadata level
- level: Metadata
resources:
- group: ""
resources: ["pods/log", "pods/status"]
# Don't log requests to a configmap called "controller-leader"
- level: None
resources:
- group: ""
resources: ["configmaps"]
resourceNames: ["controller-leader"]
# Don't log watch requests by the "system:kube-proxy" on endpoints or services
- level: None
users: ["system:kube-proxy"]
verbs: ["watch"]
resources:
- group: "" # core API group
resources: ["endpoints", "services"]
# Don't log authenticated requests to certain non-resource URL paths.
- level: None
userGroups: ["system:authenticated"]
nonResourceURLs:
- "/api*" # Wildcard matching.
- "/version"
# Log the request body of configmap changes in kube-system.
- level: Request
resources:
- group: "" # core API group
resources: ["configmaps"]
# This rule only applies to resources in the "kube-system" namespace.
# The empty string "" can be used to select non-namespaced resources.
namespaces: ["kube-system"]
# Log configmap and secret changes in all other namespaces at the Metadata level.
- level: Metadata
resources:
- group: "" # core API group
resources: ["secrets", "configmaps"]
# Log all other resources in core and extensions at the Request level.
- level: Request
resources:
- group: "" # core API group
- group: "extensions" # Version of group should NOT be included.
# A catch-all rule to log all other requests at the Metadata level.
- level: Metadata
# Long-running requests like watches that fall under this rule will not
# generate an audit event in RequestReceived.
omitStages:
- "RequestReceived"
- 我的配置是以下的,我觉得够用了
# cat /etc/kubernetes/audit-policy.yaml apiVersion: audit.k8s.io/v1 # This is required. kind: Policy rules: - level: Metadata
开启日志后
日志后端将审计事件写入JSONlines格式的文件。您可以使用以下kube-apiserver标志配置日志审核后端:
- audit-log-path指定日志后端用于写入审计事件的日志文件路径。不指定此标志会禁用日志后端。-表示标准输出 - --audit-log-maxage定义保留旧审计日志文件的最大天数 - --audit-log-maxbackup定义要保留的审计日志文件的最大数量 - --audit-log-maxsize定义审核日志文件在轮换之前的最大大小(以 MB 为单位)
前集群中kube-apiserver组件作为static pod方式运行,生命周期由kubelet直接管理,static pod由kebelet根据yaml文件创建,Log后端需要在kube-apiserver.yaml的启动参数里加以下参数:
- --audit-policy-file=/etc/kubernetes/audit-policy.yaml
- --audit-log-path=/var/log/kubernetes/kubernetes-audit.log
- --audit-log-maxage=30
- --audit-log-maxbackup=3
- --audit-log-maxsize=100
然后挂载卷
volumeMounts:
- mountPath: /etc/kubernetes/audit-policy.yaml
name: audit
readOnly: true
- mountPath: /var/log/kubernetes/
name: audit-log
readOnly: false
最后配置hostPath
volumes:
- hostPath:
path: /etc/kubernetes/audit-policy.yaml
type: File
name: audit
- hostPath:
path: /var/log/kubernetes/
type: DirectoryOrCreate
name: audit-log
更多参数可见官网



