kubernetes-如何查看终止的pod的日志:

时间:2019-07-12 12:34:21

标签: kubernetes kubectl

我正在运行硒毂,并且我的豆荚频繁地被终止。我想看看终止的豆荚的日志。怎么做?

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```

3 个答案:

答案 0 :(得分:1)

您可以按以下方式查看上一个终止的pod的日志:

kubectl logs -p <terminated pod name>

但是在大​​多数情况下,配置,机密或持久卷存在问题时,运行

kubectl describe deployment <deployment name>

将帮助进一步调试。

答案 1 :(得分:0)

来自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集群上将日志发送客户端部署为守护程序,将所有Pod的所有日志发送到中央日志管理系统(例如ELK )。设置,例如 fluentbit ,它将继续将日志发送到ELk,在那里我可以根据名称空间,pod,container或任何其他标签进行过滤。

答案 2 :(得分:0)

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

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

或者给定logging architecture in Kubernetes,您也许可以fetch the logs directly from the log-rotate files在托管容器的节点上。但是,此选项可能取决于Kubernetes的实现,因为触发pod逐出时可能会删除日志文件。