Kubernetes-如何读取写入Pod中而不是stdout/stderr的文件中的日志? [英] Kubernetes - How to read logs that are written to files in pods instead of stdout/stderr?
问题描述
我有一个状态为CrashLoopBackOff
的吊舱,从kubectl logs <pod-name> -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
进入那里的外壳并查看文件.
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.
这就是我所做的:
- 我将带有
emptyDir{}
的卷添加到了豆荚中 - 我将该卷安装到了我的主容器中,其中
mountPath
是它将日志写入到的目录 - 我在Pod中添加了另一个容器,称为记录器",该图像是我编写的日志跟踪器(
lutraman/logger-sidecar:v2
),并将相同的卷装载到/logs
中(我编写了脚本以从中读取日志此目录)
- I added a volume with
emptyDir{}
to the pod - I mounted that volume to my main container, with the
mountPath
being the directory to which it writes the logs to - 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 <pod-name> -c logger
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
对于任何有兴趣的人,这是我制作Sidecar容器的方式:
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"]
addTail:
#!/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屋!