如何查看终止的日志 pods

How to see logs of terminated pods

我是 运行 selenium 集线器,我的 pods 经常被终止。我想查看已终止的 pods 的日志。怎么做?

NAME                                               READY     STATUS              RESTARTS   AGE
chrome-75-0-0e5d3b3d-3580-49d1-bc25-3296fdb52666   0/2       Terminating         0          49s
chrome-75-0-29bea6df-1b1a-458c-ad10-701fe44bb478   0/2       Terminating         0          23s
chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5   0/2       ContainerCreating   0          7s

kubectl logs chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5
Error from server (NotFound): pods "chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5" not found
$ kubectl logs chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5 --previous
Error from server (NotFound): pods "chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5" not found

来自 kubernetes 文档:

例子

# Return snapshot logs from pod nginx with only one container
kubectl logs nginx

# Return snapshot of previous terminated ruby container logs from pod web-1
kubectl logs -p -c ruby web-1

# Begin streaming the logs of the ruby container in pod web-1
kubectl logs -f -c ruby web-1

# Display only the most recent 20 lines of output in pod nginx
kubectl logs --tail=20 nginx

# Show all logs from pod nginx written in the last hour
kubectl logs --since=1h nginx

选项

 -c, --container="": Print the logs of this container
  -f, --follow[=false]: Specify if the logs should be streamed.
      --limit-bytes=0: Maximum bytes of logs to return. Defaults to no limit.
  -p, --previous[=false]: If true, print the logs for the previous instance of the container in a pod if it exists.
      --since=0: Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be used.
      --since-time="": Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be used.
      --tail=-1: Lines of recent log file to display. Defaults to -1, showing all log lines.
      --timestamps[=false]: Include timestamps on each line in the log output

这只是一个简单的方法。但是在 production 中,我会通过部署将 kubernetes 集群上的客户端发送日志作为 daemon-set 例如 fluentbit ,这将继续向 ELk 发送日志,在那里我可以根据命名空间、pod、容器过滤事物,或任何其他标签。

运行 kubectl logs -p 将从 API 级别的现有资源中获取日志。这意味着终止的 pods' 日志将无法使用此命令。

如其他答案中所述,最好的方法是通过 logging agents or directly pushing these logs into an external service 集中您的日志。

或者,在托管 pods 的节点中给出 logging architecture in Kubernetes, you might be able to fetch the logs directly from the log-rotate files。但是,此选项可能取决于 Kubernetes 实现,因为当触发 pod 驱逐时可能会删除日志文件。