Mastering LXC Resource Allocation: CPU, Memory, and Disk Management
This guide walks through Linux Container (LXC) resource management, explaining how to control CPU, memory, and storage using cgroup commands, configuration files, and monitoring tools, with step‑by‑step examples and practical tips for efficient container operation.
LXC (Linux Containers) provides lightweight virtualization for process and resource isolation, requiring minimal packages and low overhead. It is now part of the Linux kernel and represents a competitive lightweight container technology.
Important Resources for Virtual Machines
Resource management allocates resources from providers to users, addressing overload and dynamic demand. Key resources include CPU, memory, storage, and network; this article focuses on memory, CPU, and storage.
LXC Resource Allocation Methods
Directly Editing Configuration Files
LXC uses lxc.conf with key=value entries and cgroup for resource control. Editing this file allows direct resource adjustments.
Using Command‑Line Tools
The lxc-cgroup command manipulates cgroup parameters dynamically. Its syntax is: lxc-cgroup --name=NAME subsystem [value] -n, --name=NAME: container name
Subsystem: target cgroup subsystem
[value]: value to set
Example container name: cjhlxc.
1. View current resource list: # lxc-cgroup -n cjhlxc devices.list 2. Check CPU usage:
$ lxc-start -n cjhlxc -d
$ lxc-cgroup -n cjhlxc cpuset.cpusAssign CPU 1 only: # lxc-cgroup -n cjhlxc cpuset.cpus "1" Alternatively, edit the config file: # sudo vim /var/lib/lxc/cjhlxc/config Add the line:
lxc.cgroup.cpuset.cpus = 1Installing mpstat for Verification
Install sysstat inside the container and run:
$ sudo apt-get install sysstat
$ mpstat -P ALL 2CPU 0 shows lower usage than CPU 1, but both can be utilized.
Fine‑Tuning CPU Allocation
LXC uses cpu.shares and cpuset.cpus. Adjust shares to control time slices:
# lxc-cgroup -n cjhlxc2 cpu.shares 512
# lxc-cgroup -n cjhlxc1 cpu.shares 1024This gives cjhlxc1 ~66% and cjhlxc2 ~33% of CPU resources. LXC supports dynamic adjustment without stopping applications and can preempt idle containers.
Configuring Memory Resources
The memory subsystem provides reports and limits via two parameters: memory.limit_in_bytes: maximum memory (including cache) memory.memsw.limit_in_bytes: maximum combined memory and swap
Example
Set container cjhlxc memory to 320 MB using the command:
# lxc-cgroup -n cjhlxc memory.limit_in_bytes 320000000Or edit the config file: lxc.cgroup.memory.limit_in_bytes = 320000000 Disable swap by adding:
lxc.cgroup.memory.limit_in_bytes = 300000000
lxc.cgroup.memory.memsw.limit_in_bytes = 300000000Adding Disk Storage to LXC
Create a mount point on the host and mount a new disk:
$ sudo mkdir /var/lib/lxc/cjhlxc/rootfs/mnt/mydisk
$ sudo mount /dev/sdb1 /var/lib/lxc/cjhlxc/rootfs/mnt/disk1Create a file inside the mount:
$ sudo touch /var/lib/lxc/cjhlxc/rootfs/mnt/disk1/abcStart the container to apply the changes:
$ sudo lxc-start -n cjhlxc -d
$ sudo lxc-console -n cjhlxcLXC Monitoring
List active containers: # lxc-ls --active Show container details: # lxc-info -n cjhlxc Monitor state changes with lxc-monitor.
Summary
LXC offers low‑overhead virtualization, easy CPU and memory adjustments via cgroup, and comparable performance to native environments. It cannot hot‑migrate, emulate different architectures, or provide strong isolation. The article compares LXC with other Linux virtualization technologies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
