kubectl logs <pod-id>

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

编辑:最后更正的问题。


当前回答

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

其他回答

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键,它就会恢复。

等待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

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

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

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

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

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