kubectl logs <pod-id>
从我的部署中获得最新的日志-我正在处理一个错误,并有兴趣了解运行时的日志-我如何获得连续的日志流?
编辑:最后更正的问题。
kubectl logs <pod-id>
从我的部署中获得最新的日志-我正在处理一个错误,并有兴趣了解运行时的日志-我如何获得连续的日志流?
编辑:最后更正的问题。
当前回答
在已经提出的其他建议中,我想补充两点:
您可以使用-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消失时重新启动命令。
其他回答
Kubctl logs -f=true [pod-name] -c [container-name]
如果你在pod上只有一个容器,容器名是不需要的,否则使用容器名和-c选项。 -f即follow默认为false。如果您没有将其设置为true,您将获得容器日志的快照。
试试这个,
豆荚的尾木
Kubectl——tail <"no of lines"> logs <"pod_name"> . log
例子:
Kubectl—tail 100日志app_pod
Kubectl logs -f zk-app-0——tail 10
该命令将从最后10行开始输出日志。如果这里没有提到tail,那么k8s将流化所有可用的日志(即最近10 MB的日志或最近5次日志循环)。
然而,有时我遇到使用这个命令时日志流停止的问题。然后我只需按ctrl-c并再次运行该命令。
从容器中的进程中流stdout和stderr的另一种方法是使用kubectl attach。
kubectl attach zk-app-0
这将附加到主容器并传输控制台日志。但是,kubectl日志中没有任何额外的日志功能。在这种情况下,如果日志记录停止,只需在键盘上按enter键,它就会恢复。
把它们放在一起,你可能不想看到所有旧的历史记录,所以只看到最近的20行,并继续添加新的输出行(即跟随),像这样运行它:
kubectl logs --tail=20 -f container-name
kubectl logs -f <pod-id>
你可以使用-f标志:
-f,——follow=false:指定是否对日志进行流式处理。
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs