为什么 Openshift 报告的容器内存使用量比本地相同的 Docker 容器 运行 大得多?

Why is Openshift reporting a far greater container memory usage than the same Docker container running locally?

我有一个 Docker 容器,它在本地 运行ning 时报告此资源使用情况:

docker run -i --rm -p 8080:8080 my-application

如您所见,容器使用了 10.6MiB:

docker ps -q | xargs  docker stats --no-stream             
CONTAINER ID        NAME                 CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
b73afe5ee771        mystifying_neumann   0.00%               10.6MiB / 7.777GiB   0.13%               11.7kB / 2.38kB     0B / 0B             21

现在我 运行 Openshift 中的那个容器,设置以下内存限制:

        resources:
          limits:
            memory: 64Mi
          requests:
            memory: 64Mi

当 pod 启动时,我预计使用了 ~11MiB,总共使用了 64MiB。但是容器正在使用 53MiB!!!为什么会有这种差异?

我终于在这两个参考文献中找到了这种差异的原因:

https://github.com/openshift/origin-web-console/issues/1315

https://access.redhat.com/solutions/3639191

总结:Docker 将内存报告为添加了几个元素,例如 rss 和缓存:

https://docs.docker.com/config/containers/runmetrics/#metrics-from-cgroups-memory-cpu-block-io

cache The amount of memory used by the processes of this control group that can be associated precisely with a block on a block device. When you read from and write to files on disk, this amount increases. This is the case if you use “conventional” I/O (open, read, write syscalls) as well as mapped files (with mmap). It also accounts for the memory used by tmpfs mounts, though the reasons are unclear.

rss The amount of memory that doesn’t correspond to anything on disk: stacks, heaps, and anonymous memory maps.

Openshift 3.x 使用 Heapster 读取该信息,但无法告诉 appart 两种类型的内存。

如果您检查 Openshift 中 运行 容器的 docker 统计信息,您会发现预期的(较低的)值。