El otro día publiqué una entrada en la que explicaba como levantar un contenedor LXC, el siguiente paso es configurar los recursos para que nuestros contenedores no tengan todos toda la memoria y CPU de nuestra máquina física.
Para limitar memoria y CPU tenemos que utilizar el comando lxc-cgroup y utilizar los modificadores de kernel que vienen descritos en: http://www.mjmwired.net/kernel/Documentation/cgroups/memory.txt que son los siguientes
tasks # attach a task(thread) and show list of threads cgroup.procs # show list of processes cgroup.event_control # an interface for event_fd() memory.usage_in_bytes # show current usage for memory (See 5.5 for details) memory.memsw.usage_in_bytes # show current usage for memory+Swap (See 5.5 for details) memory.limit_in_bytes # set/show limit of memory usage memory.memsw.limit_in_bytes # set/show limit of memory+Swap usage memory.failcnt # show the number of memory usage hits limits memory.memsw.failcnt # show the number of memory+Swap hits limits memory.max_usage_in_bytes # show max memory usage recorded memory.memsw.max_usage_in_bytes # show max memory+Swap usage recorded memory.soft_limit_in_bytes # set/show soft limit of memory usage memory.stat # show various statistics memory.use_hierarchy # set/show hierarchical account enabled memory.force_empty # trigger forced move charge to parent memory.pressure_level # set memory pressure notifications memory.swappiness # set/show swappiness parameter of vmscan (See sysctl's vm.swappiness) memory.move_charge_at_immigrate # set/show controls of moving charges memory.oom_control # set/show oom controls. memory.numa_stat # show the number of memory usage per numa node memory.kmem.limit_in_bytes # set/show hard limit for kernel memory memory.kmem.usage_in_bytes # show current kernel memory allocation memory.kmem.failcnt # show the number of kernel memory usage hits limits memory.kmem.max_usage_in_bytes # show max kernel memory usage recorded memory.kmem.tcp.limit_in_bytes # set/show hard limit for tcp buf memory memory.kmem.tcp.usage_in_bytes # show current tcp buf memory allocation memory.kmem.tcp.failcnt # show the number of tcp buf memory usage hits limits memory.kmem.tcp.max_usage_in_bytes # show max tcp buf memory usage recorded
Para limitar en un contenedor la memoria a 128M lo haríamos con el comando:
lxc-cgroup -n nombre_contenedor memory.limit_in_bytes 128M
Si nuestro servidor tiene 4 cores (0,1,2,3) y queremos asignar el core 1 y 3 a un contenedor lo haríamos con el comando:
lxc-cgroup -n nombre_contenedor cpuset.cpus 1 3
Y llegamos al momento de limitar la swap que debería de ser algo del estilo:
lxc-cgroup -n nombre_contenedor memory.memsw.limit_in_bytes 128M
Sin embargo a mi esto no me ha funcionado, y no he encontrado otra forma de hacerlo, seguramente algo del kernel que tengo instalado en mi portátil. La idea es limitar la memoria total incluyendo swap a la memoria RAM para que de facto la memoria swap sea 0, pero como ya he comentado no me funciona, si alguien consigue que funcione que lo comente por favor.
Tenemos que tener en cuenta que todos los comandos que hemos usado hasta ahora sólo nos sirven para la ejecución en vivo, pero no para el arranque, si queremos que arranquen con estas opciones habría que modificar el fichero de configuración que se encuentra en
/var/lib/lxc/nombre_contenedor/config
Y ahí añadiríamos las opciones deseadas, siguiendo este formato y usando las opciones de arriba:
#Limite de memoria lxc.cgroup.memory.limit_in_bytes = 256M #Limite de CPU a 1 core lxc.cgroup.cpuset.cpus = 1