接下来介绍如何配置Istio,对网格内服务的日志数据进行自动收集。假定Mixer使用的是默认配置(--conf igDefaultNamespace=istio-system),如果使用的是不同的值,需要根据实际情况对文中提及的配置和命令进行变更。
日志配置要求Mixer把日志发送给stdout。它使用了三个部分的配置:实例配置、处理程序配置以及规则配置。
1.新建一个YAML文件
文件用来配置新的日志数据流,Istio将会进行自动生成和收集的工作。配置中的kind:logentry一节定义了生成日志条目(命名为newlog的实例)的格式。这个实例配置告知Mixer如何根据请求过程中Envoy报告的属性生成日志条目。其中,severity参数用来指定生成的logentry的日志级别;timestamp参数为所有日志条目提供了时间信息;variables参数让运维人员可以配置每个logentry中应该包含什么数据,一组表达式控制了由Istio属性以及常量映射和组成logentry的过程。
在下面的示例中,每个logentry都有一个latency字段,这个字段是从response.duration属性中得来的。如果response.duration中没有值,latency字段就会设置为0ms。
以文件名new_logentry.yaml保存下面的代码:
# logentry(日志条目)的 instance 配置
apiVersion: "config.istio.io/v1alpha2"
kind: logentry
metadata:
name: newlog
namespace: istio-system
spec:
severity: '"warning"'
timestamp: request.time
variables:
source: source.labels["app"] | source.service | "unknown"
user: source.user | "unknown"
destination: destination.labels["app"] | destination.service | "unknown"
responseCode: response.code | 0
responseSize: response.size | 0
latency: response.duration | "0ms"
monitored_resource_type: '"UNSPECIFIED"'
---
# stdio(标准输入输出)处理程序的配置
apiVersion: "config.istio.io/v1alpha2"
kind: stdio
metadata:
name: newhandler
namespace: istio-system
spec:
severity_levels:
warning: 1 # Params.Level.WARNING
outputAsJson: true
---
# 将 logentry instance 发送到 stdio 的 rule 对象配置
apiVersion: "config.istio.io/v1alpha2"
kind: rule
metadata:
name: newlogstdio
namespace: istio-system
spec:
match: "true" # 匹配所有请求
actions:
- handler: newhandler.stdio
instances:
- newlog.logentry
kind:stdio这一段配置定义了一个叫做newhandler的处理程序。处理程序的spec配置了stdio适配器收到logentry实例之后的处理方式。severity_levels参数控制了logentry中severity字段的映射方式。这里的常量"warning"映射为WARNING日志级别。outputAsJson参数表示适配器生成JSON格式的日志。
kind:rule这一段配置则是定义了命名为newlogstdio的规则对象。这个规则对象引导Mixer把所有newlog.logentry实例发送给newhandler.stdio处理程序。由于match参数设置为true,因此将对网格中的所有请求执行规则。match:true表达式的含义是这一对象的执行无需过滤,对所有请求都会生效。在spec中省略match参数和设置match:true是等效的。这里的显式声明,目的是展示在rule控制过程中match表达式的使用方法。
2.把新配置推送给集群
命令如下:
$ kubectl create -f new_logentry.yaml logentry.config.istio.io/newlog created stdio.config.istio.io/newhandler created rule.config.istio.io/newlogstdio created