如何查看终止的 Pod 的日志

use*_*610 29 kubernetes kubectl

我正在运行 selenium hubs 并且我的 pod 经常被终止。我想查看终止的 pod 的日志。怎么做?

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
Run Code Online (Sandbox Code Playgroud)

yyy*_*hir 10

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

正如其他答案中提到的,最好的方法是通过日志代理将日志集中起来,或者直接将这些日志推送到外部服务中

或者,考虑到Kubernetes 中日志架构,您也许可以直接从托管 Pod 的节点中的日志轮换文件获取日志。但是,此选项可能取决于 Kubernetes 实现,因为在触发 pod 驱逐时可能会删除日志文件。


Ija*_*han 8

来自 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
Run Code Online (Sandbox Code Playgroud)

选项

 -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
Run Code Online (Sandbox Code Playgroud)

这只是一种简单的方法。但是在生产中,我会通过在 kubernetes 集群上部署日志发送客户端作为守护程序集(例如fluentbit),将所有 pod 的所有日志发送到中央日志管理系统(例如 ELK),这将继续将日志发送到我所在的 ELk能够根据 namespace 、 pod 、 container 或任何其他标签过滤事物。