kubernetes 如何查看已终止Pod的日志

kq4fsx7k  于 2022-11-02  发布在  Kubernetes
关注(0)|答案(3)|浏览(1164)

我正在运行 selenium 集线器,我的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
rwqw0loc

rwqw0loc1#

运行kubectl logs -p将从API级别的现有资源中获取日志。这意味着使用此命令将无法使用已终止Pod的日志。
正如在其他回答中提到的,最好的方法是通过日志代理集中日志,或者直接将这些日志推送到外部服务。
或者,在Kubernetes中给定日志记录体系结构的情况下,您可以直接从托管Pod的节点中的log-rotate文件中提取日志。但是,此选项可能取决于Kubernetes的实现,因为在触发Pod收回时,日志文件可能会被删除。

ca1c2owp

ca1c2owp2#

来自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

这只是一种简单的方法。但在生产中,我会将所有pod的所有日志发送到一个中心日志管理系统,如ELK,方法是在kubernetes集群上部署一个日志发送客户端作为守护程序集,如fluentbit,它将不断将日志发送到ELK,在ELK中,我可以根据名称空间、pod、容器、或任何其它标签。

rxztt3cl

rxztt3cl3#

您可以在logs上尝试--previous标志
也就是说
kubectl --namespace namespace logs pod_name --previous
这将根据kubernetes文档显示先前容器的转储pod日志(stdout)中的日志

相关问题