kubectl logs <pod-id>

从我的部署中获得最新的日志-我正在处理一个错误,并有兴趣了解运行时的日志-我如何获得连续的日志流?

编辑:最后更正的问题。


当前回答

kubectl logs -f <pod-id>

你可以使用-f标志:

-f,——follow=false:指定是否对日志进行流式处理。

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs

其他回答

Kubectl日志—帮助将指导您:

例子:

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

国旗:

-f, --follow[=false]: Specify if the logs should be streamed.

你还可以加上——since=10m or so,从相对时间之前开始。

你可以在log后面加上-f

kubectl logs -f <pod_name>

如果日志被停止,很可能是吊舱崩溃了,你能检查吊舱是否真的在运行吗?检查一下年龄或者:

kubectl describe deploy/ds <deploy_or_ds_name>?

或者你也可以查看吊舱内集装箱的日志,因为里面有多个集装箱

kubectl logs -f <pod_name> -c <container_name> 

在已经提出的其他建议中,我想补充两点:

您可以使用-l标志通过标签来跟踪pod的日志,而不是获取单个pod名称:kubectl logs -f -l app=cwagent-prometheus -n amazon-cloudwatch。如果没有匹配的吊舱,该命令仍然无效。 你可以使用类似于overmind或foreman的工具来运行日志命令的Procfile:

cloudwatch: kubectl logs -f -l app=cwagent-prometheus -n amazon-cloudwatch
egress: kubectl logs -f -l istio=egressgateway -n istio-ingress
extauth: kubectl logs -f -l context=api-gateway -n istio-ingress
external-dns: kubectl logs -f -l app=external-dns -n external-dns
ingress: kubectl logs -f -l istio=ingressgateway -n istio-ingress
istiod: kubectl logs -f -l app=istiod -n istio-system
ratelimit: kubectl logs -f -l app=ratelimit -n istio-ingress
redis: kubectl logs -f -l app.kubernetes.io/name=redis -n istio-ingress

使用这个Procfile,你可以得到一个用颜色编码的日志输出视图(这里没有显示颜色):

$ OVERMIND_AUTO_RESTART=cloudwatch,external-dns,ingress,egress,extauth,ratelimit,istiod,redis \
  overmind s \
  -f Procfile-logs
system       | Tmux socket name: overmind-api-gateway-1mvHWIKJ47dOFnOZVLfWuO
system       | Tmux session ID: api-gateway
system       | Listening at ./.overmind.sock
ratelimit    | Started with pid 57088...
istiod       | Started with pid 57074...
ingress      | Started with pid 57061...
egress       | Started with pid 57036...
cloudwatch   | Started with pid 57031...
external-dns | Started with pid 57051...
redis        | Started with pid 57095...
extauth      | Started with pid 57041...

你可以把这个命令放在Makefile中,这样你就可以运行这样的命令:

$ make logs

本例中的OVERMIND_AUTO_RESTART使overmind在所有pod消失时重新启动命令。

我需要访问一个长时间运行的pod的日志,而-f开始流几天前的日志,这需要花费几个小时才能到达我需要查看的地方(仅仅是最后几分钟左右)。

有一个——since=10m的标志,但这似乎对我不起作用。

最神奇的是——tail=100,其中100是要显示的最近行数。

把它们放在一起,你可能不想看到所有旧的历史记录,所以只看到最近的20行,并继续添加新的输出行(即跟随),像这样运行它:

kubectl logs --tail=20 -f container-name