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消失时重新启动命令。

其他回答

如果您想从特定的命名空间获取日志,您可以使用任何一个命令,

kubectl logs -n <NAMESPACE> -f <POD_NAME> -c <CONTAINER_NAME>

or

kubectl logs -n <NAMESPACE> -p <POD_NAME> -c <CONTAINER_NAME> --previous=false

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

你可以在log后面加上-f

kubectl logs -f <pod_name>

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

kubectl describe deploy/ds <deploy_or_ds_name>?

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

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

建议

似乎您希望在不使用“沉重的”第三方日志记录解决方案的情况下从您的终端查看日志。

为此,我会考虑使用K9S,它是一个很棒的CLI工具,可以帮助您控制集群——查看不同的k8s资源,在工作负载之间导航,深入日志并持续地观察它们。


如何使用工具(几行)

在当前终端中设置K8S上下文后,只需输入k9s来点击仪表板。在那里,你可以输入你想要查看的资源(服务、部署、pod..),输入“:”-以及资源名称。

你也可以从命名空间级别开始,一直到pods和容器日志——如下面的例子所示:


选择

如果你不是只绑定到CLI,但仍然想在本地运行,我会推荐在Lens。