此任务将展示如何配置Istio以创建自定义日志条目并且发送给Fluentd守护进程。Fluentd适配器的设计目的是将Istio的日志发送给Fluentd守护进程。该适配器支持logentry模板。这个适配器会用最小传输量的方式将日志传输给监听日志的Fluentd守护进程。Fluentd会给收到的所有日志添加tag。如果日志条目中包含了一个tag变量,就会使用它;否则将会使用Name作为tag。
现在有一个正在运行的Fluentd守护进程,我们使用新的日志类型配置Istio,并将这些日志发送到监听守护进程。创建一个新的YAML文件保存配置,Istio将使用该配置自动生成和收集日志流。
将以下配置另存为f luentd-istio.yaml:
# Configuration for logentry instances
apiVersion: "config.istio.io/v1alpha2"
kind: logentry
metadata:
name: newlog
namespace: istio-system
spec:
severity: '"info"'
timestamp: request.time
variables:
source: source.labels["app"] | source.workload.name | "unknown"
user: source.user | "unknown"
destination: destination.labels["app"] | destination.workload.name | "unknown"
responseCode: response.code | 0
responseSize: response.size | 0
latency: response.duration | "0ms"
monitored_resource_type: '"UNSPECIFIED"'
---
# Configuration for a Fluentd handler
apiVersion: "config.istio.io/v1alpha2"
kind: fluentd
metadata:
name: handler
namespace: istio-system
spec:
address: "fluentd-es.logging:24224"
---
# Rule to send logentry instances to the Fluentd handler
apiVersion: "config.istio.io/v1alpha2"
kind: rule
metadata:
name: newlogtofluentd
namespace: istio-system
spec:
match: "true" # match for all requests
actions:
- handler: handler.fluentd
instances:
- newlog.logentry
---
其中,Fluentd适配器可接受的实例类型为kind:logentry,其参数定义如表8-5所示。
表8-5 kind:logentry参数
