Kubernetes (GKE) 的日志记录解决方案

Logging solutions for Kubernetes (GKE)

我希望从 kubernetes 中的 pod 中捕获两个用例的日志:

  1. 实时 -> 我现在正在使用 kubectl logs ---
  2. 不是实时的 -> 使用 stackdriver 传送到 bigquery

对于这两种用例,一切正常,但是,当容器由于错误而提前退出时,我丢失了日志(即 stackdriver 获取它们的速度不够快)。

是否在某处记录了这种延迟?假设 stackdriver 不够快,是否有另一种更有效的日志记录解决方案?我正在考虑使用一个 sidecar 容器来捕获日志,但我不确定这是否是最好的方法。

GKE 上的日志堆栈使用 fluentd to pick the logs from the stdout, stderr that the container runtime writes to the nodes, as show in the node logging agent approach

这不是 much different 你在使用 kubectl logs 时所做的事情:

When you run kubectl logs as in the basic logging example, the kubelet on the node handles the request and reads directly from the log file, returning the contents in the response.

你的问题听起来 Stackdriver 不够快但是,你的容器运行时是,出于某种原因没有将日志写入上述日志文件,而 fluentd 选择导出之前的日志。

在更改日志架构之前,您可能需要 determine the reasons for pod failure and even customize the termination message path in order to later retrieve it with a custom fluentd log collector

如果这不符合您的需要,您可以尝试 Elasticsearch

至于 sidecar 方法,虽然它是完全可行的,但 official documentation 警告这种方法的一些缺点:

Using a logging agent in a sidecar container can lead to significant resource consumption. Moreover, you won't be able to access those logs using kubectl logs command, because they are not controlled by the kubelet.

最后,您还应该考虑到所有先前的信息都依赖于 容器进入创建阶段并且能够写入日志文件 的事实。如果您的容器“提前退出”,这意味着 aren't even created,那么日志可能一开始就不存在,Stackdriver 永远不会选择它们。

编辑:

提到你还想考虑一个失败的容器需要写入两个输出,stdoutstderr。如果它“无声地”失败,那也不会反映在 Stackdriver 中。