我正在尝试在 GKE 上的应用程序 pod 中捕获一些基于文件的日志,并从 Google Cloud Logging 中查看它们。
由于各种原因,这些应用程序日志不会发送到 STDOUT 或 STDERR(因为这些日志会自动发送到 Cloud Logging)。有人建议我实施一个脚本解决方案,跟踪日志并将它们发送到 STDOUT。但是,我希望采用 Fluentd(或 Fluentbit)日志记录代理的侧车方法,该代理将跟踪日志并将它们输出到 Cloud Logging。
使用 sidecar image "k8s.gcr.io/fluentd-gcp:1.30"
,我尝试了下面的 YAML 文件(包含 fluentd configmap 和部署):
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-log-config
data:
fluentd.conf: |
<source>
type tail
format none
path /var/log/execution*.log
pos_file /var/log/execution.pos
tag app.*
</source>
<match **>
type google_cloud
</match>
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: app
template:
metadata:
labels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: app
spec:
serviceAccountName: app
volumes:
- name: executionlogs
emptyDir: {}
- name: fluentdconfig
configMap:
name: app-log-config
containers:
- name: app
image: appimage:version
imagePullPolicy: IfNotPresent
volumeMounts:
- name: executionlogs
mountPath: /tmp/executionLogs
ports:
- name: http
containerPort: 8080
protocol: TCP
- name: log-agent
image: "k8s.gcr.io/fluentd-gcp:1.30"
imagePullPolicy: IfNotPresent
env:
- name: FLUENTD_ARGS
value: "-c /etc/fluentd-config/fluentd.conf"
volumeMounts:
- name: executionlogs
mountPath: /var/log
- name: fluentdconfig
mountPath: /etc/fluentd-config
最初,sidecar 日志抛出 403 错误,因为我没有为服务帐户提供必要的权限(我使用的是 GKE 工作负载身份,并且相应的 GCP IAM 服务帐户需要添加 logWriter 权限)。修复错误后,我得到以下日志:
2021-06-27 12:49:09 +0000 [info]: fluent/supervisor.rb:471:read_config: reading config file path="/etc/fluentd-config/fluentd.conf"
2021-06-27 12:49:09 +0000 [info]: fluent/supervisor.rb:337:supervise: starting fluentd-0.12.29
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-mixin-config-placeholders' version '0.4.0'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-mixin-plaintextformatter' version '0.2.6'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-google-cloud' version '0.5.2'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-kafka' version '0.3.1'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-mongo' version '0.7.15'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-record-reformer' version '0.8.2'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-rewrite-tag-filter' version '1.5.5'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-s3' version '0.7.1'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-scribe' version '0.10.14'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-systemd' version '0.0.5'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-td' version '0.10.29'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-td-monitoring' version '0.2.2'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluent-plugin-webhdfs' version '0.4.2'
2021-06-27 12:49:09 +0000 [info]: fluent/engine.rb:126:block in configure: gem 'fluentd' version '0.12.29'
2021-06-27 12:49:09 +0000 [info]: fluent/agent.rb:129:add_match: adding match pattern="**" type="google_cloud"
2021-06-27 12:49:10 +0000 [info]: plugin/out_google_cloud.rb:519:block in detect_platform: Detected GCE platform
2021-06-27 12:49:10 +0000 [info]: plugin/out_google_cloud.rb:290:configure: Logs viewer address: https://console.developers.google.com/
project/projectname/logs?service=compute.googleapis.com&key1=instance&key2=9071465168741286442
2021-06-27 12:49:10 +0000 [info]: fluent/root_agent.rb:147:add_source: adding source type="tail"
2021-06-27 12:49:10 +0000 [info]: fluent/engine.rb:133:configure: using configuration file: <ROOT>
<source>
type tail
format none
path /var/log/execution*.log
pos_file /var/log/execution.pos
tag app.*
</source>
<match **>
type google_cloud
</match>
</ROOT>
2021-06-27 12:52:10 +0000 [info]: plugin/in_tail.rb:557:initialize: following tail of /var/log/execution1.log
2021-06-27 12:53:10 +0000 [info]: plugin/out_google_cloud.rb:451:block in write: Successfully sent to Google Cloud Logging API.
尽管消息成功,但我在 Cloud Logging 端看不到任何内容。
所以,这是我的问题:
- 这对我的用例有更好的解决方案吗?
- 是我应该使用的边车图像吗?我找不到任何其他流利的图像,而我使用的是 3 岁的图像。我更喜欢使用 Google 推荐的东西,而不是自己创建。
- 我还需要做什么才能查看 Cloud Logging 上的日志?我该如何进一步调试?
谢谢!