Suggestions for limit CPU usage for LXC Containers

Hello everyone,

I need a help. I am working in LXC container. I have created a LXC container which is working fine. Now, I want to setup some configuration like limit CPU, RAM usage.
So, my question is how can I limit CPU usage for individual containers using python script? I have search in the forum but most of the topic is about LXD not LXC. It would be great, if you can help.
Thanks in advance.
@stgraber

You’ll want to set the relevant cgroup keys. Read about the following cgroups:

  • cpuset
  • cpu
  • memory

And you can set those keys using lxc.cgroup.NAME.KEY=VALUE in your lxc config.

LXD sets the following based on other settings:

  • lxc.cgroup.cpu.shares
  • lxc.cgroup.cpu.cfs_period_us (mutually exclusive with shares)
  • lxc.cgroup.cpu.cfs_quota_us (mutually exclusive with shares)
  • lxc.cgroup.cpuset.cpus
  • lxc.cgroup.memory.soft_limit_in_bytes
  • lxc.cgroup.memory.limit_in_bytes
  • lxc.cgroup.memory.memsw.limit_in_bytes

Note that LXD has a clear advantage over LXC there as it’s running a central daemon, it can dynamically re-balance containers between CPUs by adjusting their cpuset pinning. For LXC, it’s something you’d need to do centrally through your script and then live update all containers using cgroup_set through python3-lxc.

2 Likes

Thank you so much for your reply. That was helpful