简体   繁体   English

docker swarm容器内存限制不起作用

[英]docker swarm container memory limit does not work

I'm experimenting with forcing a container to use more memory than it's allowed but I can't get it to work. 我正在尝试强制容器使用超出其允许范围的内存,但无法使其正常工作。 The container is part of a stack defined with docker compose and it's deployed to docker in swarm mode. 该容器是使用docker compose定义的堆栈的一部分,并以群体模式部署到docker。

Docker is allowing the container to go way above the 50M limit I've set. Docker允许容器超过我设定的50M限制。 I was expecting docker to kill the container, throw an error, etc. 我期望docker杀死容器,抛出错误等。

Can anyone help me on why Docker does not enforce the memory limit here? 有人可以帮助我解释为什么Docker不在此处强制执行内存限制吗?

The container in docker-compose.yml is defined to have a memory limit of 50M, and then I have setup a very simple PHP test which will try to allocate 200M. docker-compose.yml中的容器定义为具有50M的内存限制,然后我设置了一个非常简单的PHP测试,它将尝试分配200M。 I've defined PHP mem limit to 128M. 我已将PHP内存限制定义为128M。

This is my docker-compose.yml 这是我的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

Instead of docker killing the container, it allows it to take as much memory as it wants and PHP eventually stops the process throwing the error below: 它没有使docker杀死该容器,而是允许它占用所需的内存,PHP最终停止了该过程,并在下面抛出错误:

"PHP message: PHP Fatal error: Allowed memory size of 125829120 bytes exhausted (tried to allocate 67108872 bytes) in /var/www/html/public/index.php on line 4" “ PHP消息:PHP致命错误:在第4行的/var/www/html/public/index.php中,耗尽了125829120字节(尝试分配67108872字节)的允许的内存大小”

I'm using Ubuntu 18.04. 我正在使用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 version 18.06.1-ce, build e68fc7a Docker版本18.06.1-CE,内部版本e68fc7a

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

This is the output of "docker stats" on the app container: 这是应用容器上“ 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

This is the output of "docker info": 这是“ docker info”的输出:

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

As you said in comment, swap is enabled on host but swap limit in cgroups does not supported yet. 如您在评论中所述,交换已在主机上启用,但尚不支持cgroups中的交换限制。

According to this enabling swap limit support . 根据此启用交换限制支持 Note that reboot of system is essential. 请注意,重新启动系统至关重要。

At last, —-memory-swap flag should be set. 最后,应设置“ —-memory-swap标志。 If you want to prevent your PHP app accessing swap, you should set it with the same value of —-memory . 如果要防止PHP应用程序访问交换,则应将其设置为—-memory More details about memory swap settings . 有关内存交换设置的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM