简体   繁体   English

如何在 GKE 上通过 stackdriver 查看容器日志

[英]How to view container logs via stackdriver on GKE

I've read that by default, clusters on GKE write all logs to stackdriver.我读过默认情况下,GKE 上的集群将所有日志写入堆栈驱动程序。 On the GKE UI, I can see the container logs of all my deployments, but am looking to get these logs from an API programmatically.在 GKE UI 上,我可以看到我所有部署的容器日志,但我希望以编程方式从 API 获取这些日志。

I've tried to do something like gcloud logging read "resource.labels.pod_id="<pod-id>"" but nothing is returned.我尝试执行gcloud logging read "resource.labels.pod_id="<pod-id>""操作,但没有返回任何内容。

Do I need to do something special in order to enable logging on the cluster?我是否需要做一些特别的事情才能在集群上启用日志记录? And if not, how do I access logs for a specific deployment/pod/container?如果没有,我如何访问特定部署/pod/容器的日志?

Further, are these logs persistent?此外,这些日志是持久的吗? As in, when the deployment dies, can I still access these logs?例如,当部署终止时,我仍然可以访问这些日志吗?

You have several options to read the GKE pods logs from Stackdriver.您可以通过多种方式从 Stackdriver 读取GKE pod 日志。 Some of them are:他们之中有一些是:

Reading logs阅读日志

To read log entries in Logging, you can do any of the following:要读取 Logging 中的日志条目,您可以执行以下任何操作:

  • Use the Logs Viewer in the Google Cloud Console.使用 Google Cloud Console 中的日志查看器。
  • Call the Logging API through the Client Libraries for your programming language.通过您的编程语言的客户端库调用 Logging API。
  • Call the Logging API REST endpoints directly.直接调用 Logging API REST 端点。 See the Logging API reference documentation.请参阅日志记录 API 参考文档。
  • Use the Cloud SDK.使用云 SDK。 For more information, see the gcloud logging command-line interface.有关更多信息,请参阅 gcloud logging 命令行界面。

    Cloud.google.com: Logging: Setup Cloud.google.com:记录:设置

As for:至于:

Do I need to do something special in order to enable logging on the cluster?我是否需要做一些特别的事情才能在集群上启用日志记录? And if not, how do I access logs for a specific deployment/pod/container?如果没有,我如何访问特定部署/pod/容器的日志?

Please refer to: Cloud.google.com: Logging: Access control请参考: Cloud.google.com:日志:访问控制

Answering below:下面回答:

Further, are these logs persistent?此外,这些日志是持久的吗? As in, when the deployment dies, can I still access these logs?例如,当部署终止时,我仍然可以访问这些日志吗?

Yes you can still access this logs, even if the deployment was deleted.是的,即使部署已删除,您仍然可以访问此日志。 You can still access the logs even if you delete your cluster.即使您删除了集群,您仍然可以访问日志。 Logs stored in Stackdriver have retention policies which will store the logs for set amount of time.存储在 Stackdriver 中的日志具有保留政策,可将日志存储一段时间。 Please refer to:请参阅:


Please take a look on the example below which shows how to access logs with gcloud command:请看下面的示例,该示例显示了如何使用gcloud命令访问日志:

Steps:脚步:

  • Create a Deployment which will send logs to Stackdriver创建一个将日志发送到 Stackdriver 的Deployment
  • Check if the logs are stored in Stackdriver检查日志是否存储在 Stackdriver
  • Get the logs from Stackdriver with gcloud使用gcloud从 Stackdriver 获取日志

Create a Deployment创建部署

Please follow a Google Cloud Platform guide to spawn a Deployment which will send data to Stackdriver:请按照 Google Cloud Platform 指南生成一个将数据发送到 Stackdriver 的Deployment

Check if the logs are in Stackdriver.检查日志是否在 Stackdriver 中。

Logs exported by above deployment will be stored in Stackdriver.通过上述部署导出的日志将存储在 Stackdriver 中。 The example of it should look like this:它的示例应如下所示:

{
 insertId: "REDACTED"  
 labels: {
  k8s-pod/pod-template-hash: "545464fb5"   
  k8s-pod/run: "custom-metric-sd"   
 }
 logName: "projects/REDACTED/logs/stderr"  
 receiveTimestamp: "2020-05-26T10:17:16.161949129Z"  
 resource: {
  labels: {
   cluster_name: "gke-logs"    
   container_name: "sd-dummy-exporter"    
   location: "ZONE"    
   namespace_name: "default"    
   pod_name: "custom-metric-sd-545464fb5-2rdvx"    
   project_id: "REDACTED"    
  }
  type: "k8s_container"   
 }
 severity: "ERROR"  
 textPayload: "2020/05/26 10:17:10 Finished writing time series with value: 0xc420015290
"  
 timestamp: "2020-05-26T10:17:10.356684667Z"  
}

Above log entry will help with creating a gcloud command to get only specified logs from the Stackdriver.上述日志条目将有助于创建gcloud命令以仅从 Stackdriver 获取指定的日志。

Get the logs from Stackdriver with gcloud使用gcloud从 Stackdriver 获取日志

As you pointed:正如你所指出的:

I've tried to do something like gcloud logging read "resource.labels.pod_id=""" but nothing is returned.我尝试过执行 gcloud logging read "resource.labels.pod_id=""" 之类的操作,但没有返回任何内容。

The fact that nothing is returned is most probably connected with the fact that the requested resource was not found with the command you used.没有返回任何内容的事实很可能与您使用的命令未找到请求的资源这一事实有关。

To get this logs from Stackdriver invoke below command:要从 Stackdriver 调用以下命令获取此日志:

$ gcloud logging read "resource.type=k8s_container AND resource.labels.container_name=sd-dummy-exporter" 

Dividing above command on smaller pieces:将上述命令划分为较小的部分:

  • resource.type=k8s_container - it will get the logs with a type of k8s_container resource.type=k8s_container - 它将获取类型为k8s_container的日志
  • resource.labels.container_name=XYZ - it will get the logs with a specified container_name . resource.labels.container_name=XYZ - 它将获取具有指定container_name的日志。

This pieces are directly connected with example singular log entry mentioned earlier.这些片段与前面提到的示例单数日志条目直接相关。

A tip:小费:

resources.labels.container_name can be used to collect logs from multiple pods as the specific pod can be referenced with pod_name . resources.labels.container_name可用于从多个 pod 收集日志,因为可以使用pod_name引用特定的 pod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM