OpenVZ allows flexible allocation of CPU resources among containers by using two parameters: CPUUnits and CPULimit.
1. CPUUnits: This parameter indicates the relative power of container’s CPU compared with other containers. The larger the value, the more CPU time the container gets. The default value is 1000, with a valid range between 8 and 500,000.
Example:
If you have two containers and you want one container to have 3 times more CPU power than the other, you could set:
- vzctl set 101 —cpuunits 3000 —save
- vzctl set 102 —cpuunits 1000 —save
In this scenario, container 101 will get three times more CPU resources than container 102.
1. CPULimit: This parameter represents the maximum percentage of the total CPU time that the container can use. The valid range for CPULimit is between 1 and 400, where 100 represents 1 CPU, 200 – 2 CPUs, 300 – 3 CPUs, and 400 – 4 CPUs.
Example:
- vzctl set 101 —cpulimit 200 —save
The above command will limit container 101 to use no more than 200% CPU, in other words – no more than 2 CPUs.
It’s important to remember that these settings should be implemented carefully to avoid resource shortage for critical containers/tasks. For permanent changes make sure to add —save flag. Also, changes are applied online and do not require container restarts.
To keep CPU resources distribution among OpenVZ containers fair, make sure to adjust the CPUUnits and CPULimit factors according to your particular needs and tasks run by each container. Ensuring fair allocation of resources will make your system more stable and prevent potential problems due to resource overload.