Kubernetes - 如何读取写入 pod 中的文件而不是 stdout/stderr 的日志? [英] Kubernetes - How to read logs that are written to files in pods instead of stdout/stderr?

查看:26
本文介绍了Kubernetes - 如何读取写入 pod 中的文件而不是 stdout/stderr 的日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处于 CrashLoopBackOff 状态的 pod,我从 kubectl logs 看到的日志-p 仅显示部分图片.其他日志可以在其他文件中找到(例如 /var/log/something/something.log).

I have a pod in a state of CrashLoopBackOff, the logs I'm seeing from kubectl logs <pod-name> -p present only a partial picutre. Other logs are found in other files (e.g. /var/log/something/something.log).

由于这个 pod 崩溃了,我无法 kubectl exec 进入 shell 并查看文件.

Since this pod is crashed, I can't kubectl exec into a shell there and look at the files.

如何查看不再运行的容器生成的日志文件?

How can I look at the log files produced by a container that is no longer running?

更具体地说,我正在查找 $HOME/logs/es.log 下的日志文件(在失败的容器中)

To be more specific, I'm looking for log files file under $HOME/logs/es.log (in the container that failed)

推荐答案

我对这个看似常见的问题没有找到解决方案感到非常沮丧,因此我构建了一个 docker 镜像,它可以跟踪日志文件并将它们发送到 stdout,用作一个边车容器.

I was so frustrated from finding no solution to this seemingly common problem that I built a docker image that tails log files and sends them to stdout, to be used as a sidecar container.

这是我所做的:

  1. 我向 Pod 添加了一个带有 emptyDir{} 的卷
  2. 我将该卷安装到我的主容器中,mountPath 是将日志写入的目录
  3. 我在 pod 中添加了另一个容器,称为logger",图像是我编写的日志跟踪器 (lutraman/logger-sidecar:v2),并将相同的卷安装到 /logs(我编写了脚本以从该目录读取日志)
  1. I added a volume with emptyDir{} to the pod
  2. I mounted that volume to my main container, with the mountPath being the directory to which it writes the logs to
  3. I added another container to the pod, called "logger", with the image being the log tracker I wrote (lutraman/logger-sidecar:v2), and mounted the same volume to /logs (I programmed the script to read the logs from this directory)

然后,写入该目录的所有日志都可以通过 kubectl logs 访问.-c 记录器

then, all the logs written to that directory, can be accessed by kubectl logs <pod-name> -c logger

这是一个 yaml 示例:

Here is an example yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dummy
  labels:
    app: dummy
spec:
  selector:
    matchLabels:
      app: dummy
  template:
    metadata:
      labels:
        app: dummy
    spec:
      volumes:
        - name: logs
          emptyDir: {}
      containers:
        - name: dummy-app # the app that writes logs to files
          image: lutraman/dummy:v2
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
          env:
            - name: MESSAGE
              value: 'hello-test'
            - name: LOG_FILE
              value: '/var/log/app.log'
          volumeMounts:
            - name: logs
              mountPath: /var/log
        - name: logger # the sidecar container tracking logs and sending them to stdout
          image: lutraman/logger-sidecar:v2
          volumeMounts:
            - name: logs
              mountPath: /logs

<小时>

对于任何感兴趣的人,以下是我制作边车容器的方法:


For anyone who is interested, here is how I made the sidecar container:

Dockerfile:

Dockerfile:

FROM alpine:3.9

RUN apk add bash --no-cache

COPY addTail /addTail
COPY logtrack.sh /logtrack.sh

CMD ["./logtrack.sh"]

添加尾:

#!/bin/sh

(exec tail -F logs/$3 | sed "s/^/$3: /" ) &
echo $! >> /tmp/pids

logtrack.sh:

logtrack.sh:

#!/bin/bash

trap cleanup INT

function cleanup() {
  while read pid; do kill $pid; echo killed $pid; done < /tmp/pids
}

: > /tmp/pids

for log in $(ls logs); do
  ./addTail n logs $log
done

inotifyd ./addTail `pwd`/logs:n 

这篇关于Kubernetes - 如何读取写入 pod 中的文件而不是 stdout/stderr 的日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆