Kubernetes 如何查看已终止POD的日志

Kubernetes 如何查看已终止POD的日志,kubernetes,kubectl,Kubernetes,Kubectl,我正在运行selenium集线器,我的播客经常被终止。我想看看终止的吊舱的日志。怎么做 NAME READY STATUS RESTARTS AGE chrome-75-0-0e5d3b3d-3580-49d1-bc25-3296fdb52666 0/2 Terminating 0 49s chrome-75-0-29

我正在运行selenium集线器,我的播客经常被终止。我想看看终止的吊舱的日志。怎么做

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

这只是一个简单的方法。但是在生产中,我会通过在kubernetes集群上部署一个日志发送客户端作为守护进程集(如fluentbit),将所有POD的所有日志发送到一个中心日志管理系统,如ELK,该客户端将继续向ELK发送日志,在那里我可以根据名称空间进行过滤,pod、容器或任何其他标签。

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

正如在其他答案中提到的,最好的方法是通过将日志集中化

或者,在给定的条件下,您可能能够在托管pod的节点中进行访问。但是,此选项可能取决于Kubernetes实现,因为触发pod逐出时可能会删除日志文件