如何查看 k8s 容器正在写入临时存储的内容

How to see what a k8s container is writing to ephemeral storage

我们的一个容器正在使用临时存储,但我们不知道为什么。容器中的应用 运行 不应向磁盘写入任何内容。

我们将存储限制设置为 20MB,但它仍在被逐出。我们可以增加限制,但这似乎是一个创可贴修复。

我们不确定此容器正在写入什么内容或写入何处,我也不确定如何检查。当一个容器被逐出时,我唯一能看到的信息是容器超出了它的存储限制。

是否有一种有效的方法来了解正在编写的内容,或者是我们梳理代码的唯一选择?

We're not sure what or where this container is writing to, and I'm not sure how to check that.

尝试查看使用 emptyDir 装载的容器 volumeMounts 部分,然后添加一个边车容器(例如 busybox)以启动一个 shell 会话,您可以在其中检查小路。如果您的集群支持 ephemeral debug container,则不需要 sidecar 容器。

正在为主题添加详细信息。

Pods use ephemeral local storage for scratch space, caching, and logs. Pods can be evicted due to other pods filling the local storage, after which new pods are not admitted until sufficient storage has been reclaimed.

kubelet 可以提供暂存 space 到 Pods 使用本地临时存储将 emptyDir 卷装载到容器中。

  • 对于container-level隔离,如果容器的可写层和日志使用超过其存储限制,kubelet 会将 Pod 标记为逐出。

  • 对于 pod-level 隔离,kubelet 通过对 Pod 中容器的限制求和来计算出总体 Pod 存储限制。在这种情况下,如果所有容器的本地临时存储使用量总和以及 Pod 的 emptyDir 卷超过总体 Pod 存储限制,那么 kubelet 也会将 Pod 标记为逐出。

要查看自 pod 启动以来写入了哪些文件,您可以 运行:

find / -mount -newer /proc -print

这将输出比“/proc”最近修改的文件列表。

/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/run/secrets
/run/secrets/kubernetes.io
/run/secrets/kubernetes.io/serviceaccount
/run/nginx.pid
/var/cache/nginx
/var/cache/nginx/fastcgi_temp
/var/cache/nginx/client_temp
/var/cache/nginx/uwsgi_temp
/var/cache/nginx/proxy_temp
/var/cache/nginx/scgi_temp
/dev

此外,请尝试不使用“-mount”选项。

要查看是否有任何新文件被修改,您可以 运行 在 Pod 中使用以下命令的一些变体:

while true; do rm -f a; touch a; sleep 30; echo "monitoring..."; find / -mount -newer a -print; done

并使用 du -h someDir 命令检查文件大小。

此外,正如@gohm'c 在他的回答中指出的那样,您可以使用 sidecar/ephemeral 调试容器。

详细了解 Local ephemeral storage here