简体   繁体   English

如何在Google Cloud的Stackdriver中获取kubernetes容器日志文件的内容?

[英]How to get content of a kubernetes container log files in the Stackdriver in google cloud?

I am new to kubernetes and google cloud and I need some help. 我是kubernetes和Google Cloud的新手,我需要一些帮助。

We have a pod with a single container runing in kubernetes in gke. 我们有一个带有单个容器的容器,该容器在gke的kubernetes中运行。 There are logging messages that the container sends to its stdout and also some logging messages that it write into few log files in its storage. 容器发送一些日志消息到其stdout,还将一些日志消息写入容器中的几个日志文件中。

The log messages sent to the container's stdout are picked by Stackdriver and we can see them there as expected. 发送到容器标准输出的日志消息由Stackdriver选择,我们可以按预期在此处看到它们。 I want to get the messages written to the log files in stackdriver as well. 我也想将消息写到stackdriver中的日志文件中。 My understanding from what I read here: ( https://kubernetes.io/docs/concepts/cluster-administration/logging/#using-a-sidecar-container-with-the-logging-agent ), is that a solution here is to add a sidecar container in the pod, and share a persistent volume between the two containers and somehow copy the log files in the shared volume and then make the sidecar container send the content of the shared log files to its own stdout (eg by sending a tail command to the sidecar container). 根据我在这里阅读的内容( https://kubernetes.io/docs/concepts/cluster-administration/logging/#using-a-sidecar-container-with-the-logging-agent ),我的理解是这里的解决方案是在pod中添加sidecar容器,并在两个容器之间共享一个持久卷,然后以某种方式将日志文件复制到共享卷中,然后使sidecar容器将共享日志文件的内容发送到其自己的stdout(例如,通过发送一个tail命令到sidecar容器)。 Then those log messages will be picked by stackdriver, as they are in a container's stdout. 然后,这些日志消息将被堆栈驱动程序选择,就像它们在容器的标准输出中一样。

The problem is, how can I share the log files of my main container with the sidecar container. 问题是,如何与sidecar容器共享主容器的日志文件。 I tried to get the log files in the shared volume using a symbolic link (by adding a ln -s command to the first container), but then the sidecar container was not able to see the content of those files (although it was able to see the list of those files, I think because that would be only a shortcut to the storage of the main container, not a real copy of the files). 我试图使用符号链接(通过向第一个容器添加ln -s命令)来获取共享卷中的日志文件,但是随后sidecar容器无法看到那些文件的内容(尽管它能够请参阅这些文件的列表,我认为是因为那只是存储主容器的捷径,而不是文件的真实副本)。

Another problem is, when I add a command to the main container (the ln -s command using command/args[]) in the template file where my pod is defined, then the default command of the container image will not be run! 另一个问题是,当我在定义了pod的模板文件中向主容器添加命令(使用command / args []的ln -s命令)时,将不会运行容器映像的默认命令! So I will not see the original logging messages of my main container in stackdriver anymore! 因此,我将不再在stackdriver中看到我的主容器的原始日志记录消息!

By the way, it seems even adding the sidecar container to the pod itself, disturbs the normal functionality of my main container. 顺便说一句,似乎甚至将sidecar容器添加到了pod本身,也干扰了我的主容器的正常功能。 I assume this has to do with how I defined my sidecar container where I am probably missing something? 我认为这与我在可能遗漏了一些东西的边车容器中定义的方式有关?

Thanks in advance for any advice! 在此先感谢您的任何建议!

Samanta SAMANTA

You don't need to create any symbolic links. 您无需创建任何符号链接。 It is enough to mount the volume in your main container so that it writes to mounted volume directly. 将卷安装在主容器中就足够了,这样它就可以直接写入已安装的卷。 The page you linked describes exactly that. 您链接的页面对此进行了准确描述。 Try using following snippet ( source ): 尝试使用以下代码段( source ):

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)" >> /var/log/1.log;
        echo "$(date) INFO $i" >> /var/log/2.log;
        i=$((i+1));
        sleep 1;
      done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-1
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/1.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-2
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/2.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

Just use your own main container in place of the "count" container. 只需使用您自己的主容器代替“计数”容器即可。 Of course, if your application sends logs to a different directory than /var/log, you need to change the mountPath in the main container accordingly. 当然,如果您的应用程序将日志发送到/ var / log以外的其他目录,则需要相应地更改主容器中的mountPath。

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

相关问题 如何将kubernetes容器日志文件转发到Stackdriver? - How to forward a kubernetes container log file into Stackdriver? 使用Stackdriver在Kubernetes集群中使用日志自定义Docker容器 - Custom docker container in Kubernetes cluster with log using Stackdriver 如何使用Google Cloud Stackdriver Log Export服务设置自定义日志目标 - How to set up a custom log destination using Google Cloud Stackdriver Log Export service 从 Kubernetes pod 中的 Google Cloud Endpoints 登录到 Stackdriver - Logging to Stackdriver from Google Cloud Endpoints in Kubernetes pod 如何在Google Cloud Platform中清除Stackdriver日志? - How to clear Stackdriver logs in Google Cloud Platform? Kubernetes:如何将容器日志文件挂载到主机 - Kubernetes: how can I mount container log files to host 将堆栈驱动程序日志作为csv文件导出到Google Cloud Storage - Exporting stackdriver logs into Google Cloud Storage as csv files 如何将 JSON 从 GCE 容器 VM 记录到 Stackdriver? - How to log JSON from a GCE Container VM to Stackdriver? Google Stackdriver未按照预期从Google Kubernetes Engine显示结构条目日志 - Google Stackdriver not showing struct entry log as expected from Google Kubernetes Engine Kubernetes 堆栈驱动程序日志级别与 Wildfly 10 不正确 - Kubernetes stackdriver log level not correct with wildfly 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM