简体   繁体   English

无法从 pod Kubernetes 获取应用程序日志

[英]Can't get the application logs from pods Kubernetes

I run three services in three different containers.我在三个不同的容器中运行三个服务。 The logs for these services are sent to the system so if I run these on a Linux server, I can see the logs with journalctl.这些服务的日志被发送到系统,所以如果我在 Linux 服务器上运行这些服务,我可以使用 journalctl 查看日志。

Also, if I run the services in Docker containers, I can gather the logs with docker logs <container_name> or from /var/lib/docker/containers directory.此外,如果我在 Docker 容器中运行服务,我可以使用 docker logs <container_name> 或 /var/lib/docker/containers 目录收集日志。 But when I move to Kubernetes (Microk8s), I cannot retrieve them with kubectl logs command, and there are also no logs in /var/log/containers or /var/log/pods.但是当我移动到 Kubernetes (Microk8s) 时,我无法使用 kubectl logs 命令检索它们,并且 /var/log/containers 或 /var/log/pods 中也没有日志。

If I login to the pods, I can see that the processes are running, but without logs I couldn't say if there are running correctly.如果我登录到 pod,我可以看到进程正在运行,但是如果没有日志,我就不能说它们是否正确运行。 Also, I tried to change the runtime of microk8s kubelet from containerd to docker, but still I can't get any logs.另外,我尝试将 microk8s kubelet 的运行时从 containerd 更改为 docker,但仍然无法获取任何日志。

# kubectl get po -o wide
NAME                              READY   STATUS        RESTARTS   AGE   IP             NODE     NOMINATED NODE   READINESS GATES
amf-deployment-7785db9758-h24kz   1/1     Running       0          72s   10.1.243.237   ubuntu   <none>

# kubectl describe po amf-deployment-7785db9758-h24kz
Events:
Type    Reason          Age   From               Message
  ----    ------          ----  ----               -------
Normal  Scheduled       87s   default-scheduler  Successfully assigned default/amf-deployment-7785db9758-h24kz to ubuntu
Normal  AddedInterface  86s   multus             Add eth0 [10.1.243.237/32]
Normal  Pulled          86s   kubelet            Container image "amf:latest" already present on machine
Normal  Created         86s   kubelet            Created container amf
Normal  Started         86s   kubelet            Started container amf

# kubectl logs amf-deployment-7785db9758-h24kz
# kubectl logs -f amf-deployment-7785db9758-h24kz
^C

You can see in the following screenshot the difference of running the same container with Docker and running it with Kubernetes.您可以在以下屏幕截图中看到使用 Docker 运行同一容器和使用 Kubernetes 运行它的区别。 The behaviour seems very strange, since the logs can be gathered if the application run as an independent Docker container, but not when it is running with Kubernetes.这种行为看起来很奇怪,因为如果应用程序作为独立的 Docker 容器运行,则可以收集日志,但当它与 Kubernetes 一起运行时则不能。 enter image description here在此处输入图像描述

In traditional server environments, application logs are written to a file such as /var/log/app.log .在传统的服务器环境中,应用程序日志被写入一个文件,例如/var/log/app.log However, when working with Kubernetes, you need to collect logs for multiple transient pods (applications), across multiple nodes in the cluster, making this log collection method less than optimal.但是,在使用 Kubernetes 时,您需要跨集群中的多个节点收集多个临时 pod(应用程序)的日志,这使得这种日志收集方法不是最佳的。 Instead, the default Kubernetes logging framework recommends capturing the standard output ( stdout ) and standard error output ( stderr ) from each container on the node to a log file.相反,默认的 Kubernetes 日志框架建议从每个节点上的日志文件中捕获标准 output ( stdout ) 和标准错误 output ( stderr )。 If you can't see you apps logs when using kubectl logs command it most likely means that your app is not writing logs in the right place.如果您在使用kubectl logs命令时看不到您的应用程序日志,则很可能意味着您的应用程序没有在正确的位置写入日志。 The official Logging Architecture docs explain this topic in more detail.官方Logging Architecture文档更详细地解释了这个主题。 There is also an example of Basic logging in Kubernetes : Kubernetes 中还有一个基本日志记录的示例

This example uses a Pod specification with a container to write text to the standard output stream once per second.此示例使用带有容器的 Pod 规范将文本写入标准 output stream 每秒一次。

 apiVersion: v1 kind: Pod metadata: name: counter spec: containers: - name: count image: busybox args: [/bin/sh, -c, 'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']

To run this pod, use the following command:要运行此 pod,请使用以下命令:

 kubectl apply -f https://k8s.io/examples/debug/counter-pod.yaml

The output is: output 是:

 pod/counter created

To fetch the logs, use the kubectl logs command, as follows:要获取日志,请使用 kubectl logs 命令,如下所示:

 kubectl logs counter

The output is: output 是:

 0: Mon Jan 1 00:00:00 UTC 2001 1: Mon Jan 1 00:00:01 UTC 2001 2: Mon Jan 1 00:00:02 UTC 2001...

You can use kubectl logs --previous to retrieve logs from a previous instantiation of a container.您可以使用kubectl logs --previous从容器的先前实例中检索日志。 If your pod has multiple containers, specify which container's logs you want to access by appending a container name to the command.如果您的 pod 有多个容器,请通过将容器名称附加到命令来指定要访问哪个容器的日志。 See the kubectl logs documentation for more details.有关更多详细信息,请参阅kubectl 日志文档

You can compare it with your Pod /app configs to see if there are any mistakes.您可以将其与您的Pod /app 配置进行比较,看看是否有任何错误。


Having that knowledge in mind you now have several option to Debug Running Pods such as:考虑到这些知识,您现在有几个选项来调试正在运行的 Pod ,例如:


To sum up:总结一下:

  • Make sure your logging is in place确保您的日志记录到位

  • Debug with the options listed above使用上面列出的选项进行调试

For kubernetes logs, you can try this command to see the logs:对于 kubernetes 日志,您可以尝试使用此命令查看日志:

kubectl logs -f <pod-name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM