kubectl logs <pod-id>

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

编辑:最后更正的问题。


当前回答

Kubctl logs -f=true [pod-name] -c [container-name]

如果你在pod上只有一个容器,容器名是不需要的,否则使用容器名和-c选项。 -f即follow默认为false。如果您没有将其设置为true,您将获得容器日志的快照。

其他回答

你可以在log后面加上-f

kubectl logs -f <pod_name>

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

kubectl describe deploy/ds <deploy_or_ds_name>?

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

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

试试这个,

豆荚的尾木

Kubectl——tail <"no of lines"> logs <"pod_name"> . log

例子:

Kubectl—tail 100日志app_pod

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,从相对时间之前开始。

等待kubes旋转吊舱,然后继续前进…

k8s_pod=some_pod
kubectl get pods -w $k8s_pod | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Running"* ]] && pkill -P $$ kubectl
done

尾日志

for line in $(kubectl get pods | grep $k8s_pod | awk '{print $1}'); do
    kubectl logs -f $line | tee logfile
done

寻找成功的标志

tail logfile | grep successful! 
RESULT=$?
exit $RESULT

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

您可以使用-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消失时重新启动命令。