Using CGroup to limit VPS CPU usage under CentOS

Using CGroup to limit VPS CPU usage under CentOS

Linux CGroups can allocate resources—such as CPU time, system memory, network bandwidth, or a combination of these—to user-defined groups of tasks (processes) running on the system. The following is mainly to limit the CPU usage.
For the shared CPU usage time and ratio, it is recommended to check the TOS of each VPS provider.

Virmach's CPU usage cannot exceed 50% for more than 2 hours.

1. Install and enable CGroup

 yum install libcgroup libcgroup-tools numactl -y

Enable CGroup

 systemctl start cgconfig.service

2. Limit the user's CPU usage

1. Add configuration

 vi /etc/cgconfig.conf

Add at the end

 group users_normal_limit{
    cpu{
        # Limit CPU usage to 30%
        cpu.cfs_period_us=100000;
        cpu.cfs_quota_us=30000;
    }
}

Mainly modify cpu.cfs_quota_us to be the ratio of cpu.cfs_period_us.

2. Add restricted users

 vi /etc/cgrules.conf

Add at the end

 @user cpu users_normal_limit

Add restricted user

 usermod -a -G users root

or more

 usermod -a -G users www
usermod -a -G users mysql

3. Use configuration to take effect

Set Cgroup service to start at boot

 systemctl enable cgconfig
systemctl enable cgred

Restart Cgroup Service

 systemctl restart cgconfig
systemctl restart cgred


3. Limit the CPU usage of a single program
1. Check the number of threads of VPS CPU

 grep 'processor' /proc/cpuinfo | sort -u | wc -l

2. Create an isolation group

 cd /sys/fs/cgroup/cpu
mkdir cpu_test

Note: After the directory is created, the following files will be automatically generated (Note: This directory cannot be deleted manually. If it is not added to the startup, it will disappear after restart)

 ls cpu_test/

3. Modify parameters

 echo 30000 > /sys/fs/cgroup/cpu/cpu_test/cpu.cfs_quota_us

Note: The default value of -1 is unlimited. Now it is changed to 20000, which means cpu.cfs_quota_us is set to 30000, which is 30% relative to cpu.cfs_period_us of 100000. It can be understood that the CPU usage is limited to 30%.

4. Limit the CPU usage of a program
View Process

 top

Restriction Program

 echo process pid >> /sys/fs/cgroup/cpu/cpu_test/tasks

Writing multiple processes into tasks means sharing 30% of the CPU.

4. Run a CPU-consuming script

 vi cpu.sh

Write

 #/bin/bash
x=0
while [ True ];do
    x=$x+1
done;

Set permissions and run

 chmod +x cpu.sh
sh cpu.sh &

Main references: http://note.jues.org.cn/index.php/comment/12, https://www.cnblogs.com/llwxhn/p/12558577.html

<<:  DogYun: Double 12 event, 40% discount on all items, lucky draw, etc.

>>:  【Black Friday】NetShop ISP: €3.49/month/512MB memory/10GB space/600GB traffic/100Mbps port/KVM/Singapore/Cyprus/Malta/UK/Netherlands, etc.

Recommend

PieLayer: $35/year/2GB RAM/45GB SSD space/1TB traffic/OpenVZ/Los Angeles

PieLayer, which has been introduced many times, i...

HostXNow: £10/year/512MB RAM/4GB SSD space/250GB bandwidth/OpenVZ/UK

HostXNow, a British hosting provider, was founded...

vpsdime: $14/month/2GB/1TB space/8TB traffic/OpenVZ

vpsdime has been introduced many times on this si...

ZXhost: $45/year/2GB RAM/1TB storage/unlimited traffic/OpenVZ/UK

ZXhost is a British hosting company, founded in 2...

G-CORE 512MB RAM 50Mbps Bandwidth Hong Kong KVM VPS Review

Details : G-CORE: €4.49/month/512MB memory/20GB S...