通过 Stackdriver 将日志导出到 BigQuery、GCS、Pub/Sub
如何通过 Stackdriver 将 Istio 访问日志导出到 BigQuery、GCS、Pub/Sub 等不同的接收器。
本文介绍如何将 Istio 日志定向到 Stackdriver,并将这些日志导出到各种配置的接收器,例如 BigQuery、Google Cloud Storage 或 Cloud Pub/Sub。在本篇文章的最后,您可以从您最喜欢的场所(例如 BigQuery、GCS 或 Cloud Pub/Sub)执行对 Istio 数据的分析。
在整个任务中,使用 Bookinfo 示例应用程序作为示例应用程序。
开始之前
在您的集群中 安装 Istio 并部署应用程序。
配置 Istio 以导出日志
Istio 使用 logentry
模板 导出日志。这指定了可用于分析的所有变量。它包含源服务、目标服务、身份验证指标(即将推出)等信息。以下是管道图
Istio 支持将日志导出到 Stackdriver,而 Stackdriver 又可以配置为将日志导出到您最喜欢的接收器(例如 BigQuery、Pub/Sub 或 GCS)。请按照以下步骤首先设置您最喜欢的用于导出日志的接收器,然后在 Istio 中设置 Stackdriver。
设置各种日志接收器
所有接收器的通用设置
- 为该项目启用 Stackdriver Monitoring API。
- 确保设置接收器的
principalEmail
对该项目具有写入访问权限,并且具有日志管理员角色权限。 - 确保设置了
GOOGLE_APPLICATION_CREDENTIALS
环境变量。请按照 此处 的说明进行设置。
BigQuery
- 创建 BigQuery 数据集 作为日志导出的目标。
- 记录数据集的 ID。配置 Stackdriver 处理程序时将需要它。它将采用以下格式:
bigquery.googleapis.com/projects/[PROJECT_ID]/datasets/[DATASET_ID]
- 在 IAM 中授予 接收器写入器身份:
cloud-logs@system.gserviceaccount.com
BigQuery 数据编辑器角色。 - 如果使用 Google Kubernetes Engine,请确保在集群上启用了
bigquery
范围。
Google Cloud Storage (GCS)
- 创建您希望在 GCS 中导出日志的 GCS 存储桶。
- 重新编码存储桶的 ID。配置 Stackdriver 时将需要它。它将采用以下格式:
storage.googleapis.com/[BUCKET_ID]
- 在 IAM 中授予 接收器写入器身份:
cloud-logs@system.gserviceaccount.com
存储对象创建者角色。
Google Cloud Pub/Sub
- 创建您希望在 Google Cloud Pub/Sub 中导出日志的 主题。
- 重新编码主题的 ID。配置 Stackdriver 时将需要它。它将采用以下格式:
pubsub.googleapis.com/projects/[PROJECT_ID]/topics/[TOPIC_ID]
- 在 IAM 中授予 接收器写入器身份:
cloud-logs@system.gserviceaccount.com
Pub/Sub 发布者角色。 - 如果使用 Google Kubernetes Engine,请确保在集群上启用了
pubsub
范围。
设置 Stackdriver
必须创建 Stackdriver 处理程序以将数据导出到 Stackdriver。Stackdriver 处理程序的配置在 此处 描述。
将以下 yaml 文件保存为
stackdriver.yaml
。用其特定值替换<project_id>
、<sink_id>
、<sink_destination>
、<log_filter>
。apiVersion: "config.istio.io/v1alpha2" kind: stackdriver metadata: name: handler namespace: istio-system spec: # We'll use the default value from the adapter, once per minute, so we don't need to supply a value. # pushInterval: 1m # Must be supplied for the Stackdriver adapter to work project_id: "<project_id>" # One of the following must be set; the preferred method is `appCredentials`, which corresponds to # Google Application Default Credentials. # If none is provided we default to app credentials. # appCredentials: # apiKey: # serviceAccountPath: # Describes how to map Istio logs into Stackdriver. logInfo: accesslog.logentry.istio-system: payloadTemplate: '{{or (.sourceIp) "-"}} - {{or (.sourceUser) "-"}} [{{or (.timestamp.Format "02/Jan/2006:15:04:05 -0700") "-"}}] "{{or (.method) "-"}} {{or (.url) "-"}} {{or (.protocol) "-"}}" {{or (.responseCode) "-"}} {{or (.responseSize) "-"}}' httpMapping: url: url status: responseCode requestSize: requestSize responseSize: responseSize latency: latency localIp: sourceIp remoteIp: destinationIp method: method userAgent: userAgent referer: referer labelNames: - sourceIp - destinationIp - sourceService - sourceUser - sourceNamespace - destinationIp - destinationService - destinationNamespace - apiClaims - apiKey - protocol - method - url - responseCode - responseSize - requestSize - latency - connectionMtls - userAgent - responseTimestamp - receivedBytes - sentBytes - referer sinkInfo: id: '<sink_id>' destination: '<sink_destination>' filter: '<log_filter>' --- apiVersion: "config.istio.io/v1alpha2" kind: rule metadata: name: stackdriver namespace: istio-system spec: match: "true" # If omitted match is true. actions: - handler: handler.stackdriver instances: - accesslog.logentry ---
推送配置
$ kubectl apply -f stackdriver.yaml stackdriver "handler" created rule "stackdriver" created logentry "stackdriverglobalmr" created metric "stackdriverrequestcount" created metric "stackdriverrequestduration" created metric "stackdriverrequestsize" created metric "stackdriverresponsesize" created
向示例应用程序发送流量。
对于 Bookinfo 示例,请在您的网络浏览器中访问
http://$GATEWAY_URL/productpage
或发出以下命令$ curl http://$GATEWAY_URL/productpage
验证日志是否已通过 Stackdriver 流向配置的接收器。
- Stackdriver:导航到您项目的 Stackdriver 日志查看器,并在“GKE 容器”->“集群名称”->“命名空间 ID”下查找 Istio 访问日志。
- BigQuery:导航到您项目的 BigQuery 界面,您应该在您的接收器数据集中找到一个以
accesslog_logentry_istio
为前缀的表。 - GCS:导航到您项目的 存储浏览器,您应该在您的接收器存储桶中找到一个名为
accesslog.logentry.istio-system
的存储桶。 - Pub/Sub:导航到您项目的 Pub/Sub 主题列表,您应该在您的接收器主题中找到一个名为
accesslog
的主题。
了解发生了什么
上面的 Stackdriver.yaml
文件配置了 Istio 将访问日志发送到 Stackdriver,然后添加了接收器配置,这些日志可以在其中导出。详细说明如下
添加了 kind 为
stackdriver
的处理程序apiVersion: "config.istio.io/v1alpha2" kind: stackdriver metadata: name: handler namespace: <your defined namespace>
在 spec 中添加了
logInfo
spec: logInfo: accesslog.logentry.istio-system: labelNames: - sourceIp - destinationIp ... ... sinkInfo: id: '<sink_id>' destination: '<sink_destination>' filter: '<log_filter>'
在上述配置中,sinkInfo 包含有关您希望将日志导出到的接收器的信息。有关如何为不同的接收器填充此信息的更多信息,请参阅 此处。
为 Stackdriver 添加了一个规则
apiVersion: "config.istio.io/v1alpha2" kind: rule metadata: name: stackdriver namespace: istio-system spec: match: "true" # If omitted match is true actions: - handler: handler.stackdriver instances: - accesslog.logentry
清理
删除新的 Stackdriver 配置
$ kubectl delete -f stackdriver.yaml
如果您不打算探索任何后续任务,请参考 Bookinfo 清理 指令以关闭应用程序。
导出接收器中日志的可用性
导出到 BigQuery 在几分钟内完成(我们发现它几乎是即时的),GCS 可能会有 2 到 12 小时的延迟,而 Pub/Sub 几乎是立即完成的。