首页 > 解决方案 > docker swarm 容器内存限制不起作用

问题描述

我正在尝试强制容器使用比允许更多的内存,但我无法让它工作。该容器是使用 docker compose 定义的堆栈的一部分,它以 swarm 模式部署到 docker。

Docker 允许容器超出我设置的 50M 限制。我期待 docker 杀死容器,抛出错误等。

谁能帮我解释一下为什么 Docker 不在这里强制执行内存限制?

docker-compose.yml 中的容器被定义为具有 50M 的内存限制,然后我设置了一个非常简单的 PHP 测试,它将尝试分配 200M。我已将 PHP 内存限制定义为 128M。

这是我的 docker-compose.yml

  version: "3"

  services:
    nginx:
      image: nginx:latest
      restart: unless-stopped
      volumes:
        - ./deploy/nginx/nginx.conf:/etc/nginx/nginx.conf
        - ./public:/usr/share/nginx/html
      ports:
        - "8180:80"
      links:
        - app

    app:
      image: 127.0.0.1:5000/wpdemo
      build:
        context: .
        dockerfile: Dockerfile-app
      restart: unless-stopped
      volumes:
        - .:/var/www/html
      links:
        - mysql
      deploy:
        resources:
          limits:
            cpus: '0.50'
            memory: 50M
          reservations:
            cpus: '0.25'
            memory: 20M

    mysql:
        image: mysql:5.7
        restart: unless-stopped
        ports:
            - "13306:3306"
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - ~/docker/volumes/mysql:/var/lib/mysql

它不是让 docker 杀死容器,而是允许它占用尽可能多的内存,并且 PHP 最终会停止该进程并抛出以下错误:

“PHP 消息:PHP 致命错误:第 4 行 /var/www/html/public/index.php 中允许的内存大小为 125829120 字节已用尽(尝试分配 67108872 字节)”

我正在使用 Ubuntu 18.04。

uname -a
Linux  4.18.10-041810-generic #201809260332 SMP Wed Sep 26 07:34:01 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Docker 版本 18.06.1-ce,构建 e68fc7a

docker-compose 版本 1.17.1,构建未知 docker-py 版本:2.5.1 CPython 版本:2.7.15rc1 OpenSSL 版本:OpenSSL 1.1.0g 2017 年 11 月 2 日

这是应用容器上“docker stats”的输出:

CONTAINER ID        NAME                                        CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
679c8495ac1d        stackdemo_app.1.hr3ufwlskhdafre39aqrshxyu   0.00%               43.81MiB / 50MiB    87.62%              106kB / 389kB       2.05GB / 10.6GB     5

这是“码头信息”的输出:

Containers: 36
 Running: 5
 Paused: 0
 Stopped: 31
Images: 450
Server Version: 18.06.1-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: active
 NodeID: wnegv5lp41wfs3epfrua489or
 Is Manager: true
 ClusterID: hq7o176yffjglxzb9pu3fiomr
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 10
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
  Force Rotate: 0
 Autolock Managers: false
 Root Rotation In Progress: false
 Node Address: 192.168.1.120
 Manager Addresses:
  192.168.1.120:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.18.10-041810-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.49GiB
Name: rafxps15
ID: QEX7:FEB3:J76L:DCAQ:SO4S:SWVE:4XPI:PI6R:YM4C:MV4I:C3PM:FLOQ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

标签: dockerdocker-composedocker-swarm

解决方案


正如您在评论中所说,主机上启用了交换,但尚不支持 cgroups 中的交换限制。

根据此启用交换限制支持。请注意,重新启动系统是必不可少的。

最后,—-memory-swap应该设置标志。如果您想阻止您的 PHP 应用程序访问交换,您应该将其设置为相同的—-memory. 有关内存交换设置的更多详细信息。


推荐阅读