背景: 因为机器一开始搭建的时候用的CRI 是 containerd, 所以只能分别升级 fluentbit-operator 来保证日志能够正常采集
机器配置:
- K8S 1.20
- CRI: containerd
- KS-installers v3.1.1
升级前我已经:
- 安装完成并可以开始正常使用 ks 了
- KS 已开启日志采集功能/插件 (这时候 es 依然没有内容)
- KS 已开启审计日志 (这时候 es 依然没有内容)
- 了解 helm 这种安装方式
升级步骤:
去 fluentbit-operator github 库将它的代码拉下来
git clone --depth=1 git@github.com:fluent/fluentbit-operator.git
进入它的 helm chart 目录fluentbit-operator/charts/fluentbit-operator
, 配置values.yaml
文件
# 为了页面整洁, 我这边就只放出我做了修改的部分
Kubernetes: true # 这个一定要开, 不然会少很多配置,导致日志不会写入 es,或者写入失败
containerRuntime: containerd
output:
es:
host: "elasticsearch-logging-data.kubesphere-logging-system.svc"
执行 helm install 命令
helm install fluentbit-operator --create-namespace -n kubesphere-logging-system . -f values.yaml
删除fluentbit 的 daemonset, 让他重启 fluentbit 的 pod
访问 es 的 9200端口, 查看它的 index 大小已经在增加, localhost:9200/_cat/indices
, 但是发现审计日志的 index 没有创建
经过观察logging.kubeshpere.io
中的 CRD 资源, 发现审计日志的采集是
- 通过 Input
tail-auditing
采集 auditing pod 的日志
- 然后在 Filter
filter-auditing
中对日志进行加工
- 最后在 Output
es-auditing
中写入 es
我们先将tail-auditing
&es-auditing
中的 tag 改成audit
, 然后将es-auditing
中的输出方式改成 stdout, 这样我们就可以在 fluentbit pod 的 stdout 中看到 fluentbit 是否有正常采集auditing pod 的日志了
修改完后,发现有以下日志输出
[0] auditing: [1632992836.228417247, {"stream"=>"stderr", "logtag"=>"F","message"=>"E0930 17:07:16.228306 1 statistics.go:125] {"InChanAverage":"0.069ms","InChanCount":"26","InChanSum":"1.796ms","Length":"0","MatchedAverage":"0.736ms","MatchedCount":"26","MatchedSum":"19.139ms","WaitToChanAverage":"0.007ms","WaitToChanCount":"26","WaitToChanSum":"0.191ms"}"}]
[0] auditing: [1632993116.194757611, {"stream"=>"stdout", "logtag"=>"F", "message"=>"{"AuditID":"ade5ce4f-2348-4c2f-b3c5-7abf63536e3f","Cluster":"host","Devops":"","Level":"Metadata","Message":"","ObjectRef":{"APIGroup":"tenant.kubesphere.io","APIVersion":"v1alpha2","Name":"ns-2","Namespace":"ns-2","Resource":"namespaces","ResourceVersion":"Namespace","Subresource":"","UID":""},"RequestObject":"","RequestReceivedTimestamp":"2021-09-30T17:11:55.205505+08:00","RequestURI":"/kapis/clusters/host/tenant.kubesphere.io/v1alpha2/workspaces/demo/namespaces/ns-2","ResponseObject":"","ResponseStatus":{"code":200,"metadata":{}},"SourceIPs":"192.168.200.204","Stage":"ResponseComplete","StageTimestamp":"2021-09-30T17:11:55.246574+08:00","Trace":{"MatchTimeout":false,"MatchedTime":"2021-09-30T17:11:56.194492382+08:00","OutputTime":"2021-09-30T17:11:56.19449252+08:00","PullTime":"2021-09-30T17:11:56.193937712+08:00","PushTime":"2021-09-30T17:11:56.19386928+08:00","ReceiviedTime":"2021-09-30T17:11:56.19386413+08:00","WaitRoutinesTimeout":false},"User":{"Groups":"system:authenticated","UID":"","Username":"admin"},"Verb":"delete","Workspace":"demo"}"}]
这说明日志是被正常采集的, 那么问题可能是出现在了 Filter 阶段了
我们查看filter-auditing
的配置, 发现parser
中使用的keyName
为log
, 但是通过上面的日志发现, 记录中的 key
只有stream
,logtag
,message
于是在 filter 中的filter-auditing
里面, 将keyName
字段的值改成message
spec:
filters:
- parser:
keyName: message # 这里原来是 log
parser: json
- modify:
conditions:
- keyDoesNotExist:
AuditID: ''
rules:
- add:
ignore: 'true'
- modify:
rules:
- remove: Trace
- grep:
exclude: ignore true
match: kube_auditing
然后将tail-auditing
&es-auditing
中的 tag 改回原样
重启 fluentbit, 再次查看 es 中的 index, 发现已经出现了 auditing 相关的 index
—
Q: 为什么不用普通的 yaml 文件直接 apply 呢?
A: 我试过, 但是我发现有不少文件缺失导致升级失败, 而且 yaml 文件的方式默认你是使用 docker 的 CRI, 不适用于本文的情况
Q:可不可以不开启日志插件直接安装 fluentbit-operator?
A: 不可以, 不然在apply 一些 CRD 资源的时候会失败, 另外, 如果你重启 ks-installer 的话, 你的升级会被回滚, 所以还是建议保留一些一键升级手段