在 Google Cloud Kubernetes pod 中消耗磁盘空间的日志上的日志轮换

chr*_*sva 6 logging log-rotation google-cloud-platform kubernetes kubernetes-pod

我们在 Google Cloud Platform Kubernetes 集群中有一个 pod,将 JsonFormatted 写入 StdOut。这是 Stackdriver 开箱即用的。但是,我们看到 pod 的磁盘使用量一直在增长,我们无法理解如何在 Deployment 上设置最大大小以进行日志轮换。

Google Cloud 和 Kubernetes 上的文档对此不清楚。

这只是最后一个小时:

Pod 上的内存消耗

VAS*_*VAS 2

您确定 pod 的磁盘使用率因日志而很高吗?\n如果应用程序将日志写入 stdout,则它不会使用 pod 内的任何磁盘空间。\n所有日志通常存储在 pod 上的日志文件中node\xe2\x80\x99s 文件系统,可以由节点 logrotate 进程管理。

\n

也许应用程序将 pod 的磁盘空间用于其他用途,例如临时文件或调试信息?

\n

以下是与日志轮转相关的文档部分:

\n
\n

节点级别的日志记录:

\n

容器化应用程序写入 stdout 和 stderr 的所有内容都由容器引擎处理并重定向到某处。例如,Docker 容器引擎将这两个流重定向到日志记录驱动程序,该驱动程序在 Kubernetes 中配置为以 json 格式写入文件。

\n

节点级日志记录的一个重要考虑因素是实现日志轮转,以便日志不会消耗节点上的所有可用存储空间

\n

Kubernetes 目前不负责轮换日志,但部署工具应该设置一个解决方案来解决这个问题。例如,在 Kubernetes 集群中,由 kube-up.sh 脚本部署,有一个 logrotate 工具配置为每小时运行一次。

\n

您还可以设置容器运行时来自动轮换应用程序\xe2\x80\x99s 日志\n,例如通过使用 Docker\xe2\x80\x99s log-opt。

\n

在 kube-up.sh 脚本中,后一种方法用于 GCP 上的 COS 镜像,前一种方法用于任何其他环境。在这两种情况下,默认情况下轮换配置为当日志文件超过 10MB 时进行。

\n

例如,您可以在相应的脚本中找到有关 kube-up.sh\n如何在 GCP 上设置 COS 镜像日志记录的详细信息。

\n
\n

以下是与 logrotate 相关的脚本的一部分:

\n
# Installs logrotate configuration files\nfunction setup-logrotate() {\n  mkdir -p /etc/logrotate.d/\n  # Configure log rotation for all logs in /var/log, which is where k8s services\n  # are configured to write their log files. Whenever logrotate is ran, this\n  # config will:\n  # * rotate the log file if its size is > 100Mb OR if one day has elapsed\n  # * save rotated logs into a gzipped timestamped backup\n  # * log file timestamp (controlled by \'dateformat\') includes seconds too. This\n  #   ensures that logrotate can generate unique logfiles during each rotation\n  #   (otherwise it skips rotation if \'maxsize\' is reached multiple times in a\n  #   day).\n  # * keep only 5 old (rotated) logs, and will discard older logs.\n  cat > /etc/logrotate.d/allvarlogs <<EOF\n/var/log/*.log {\n    rotate ${LOGROTATE_FILES_MAX_COUNT:-5}\n    copytruncate\n    missingok\n    notifempty\n    compress\n    maxsize ${LOGROTATE_MAX_SIZE:-100M}\n    daily\n    dateext\n    dateformat -%Y%m%d-%s\n    create 0644 root root\n}\nEOF\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n