Steps to Configure CGgroup in CentOS
Configuring CGroups in CentOS typically involves installing CGroup, creating a CGroups configuration file, and starting the cgconfig services. Follow the steps given below to configure CGroup in CentOS.
Step 1: Checking CGroups Support:
The very first thing we have to do is to confirm whether the kernel supports CGroups. Most of the modern kernels do support CGroup. To check whether the CGroup is supported or not type the command given below.
grep -i cgroup /proc/mounts
If the kernel supports it, we can see entries like cgroup on /sys/fs/cgroup in the output.
Output:
Step 2: Install the libcgroup Package:
We need to install libcgroup package to manage CGroup. Type the command given below to install the required package.
sudo yum install libcgroup
Output 1:
Output 2:
Step 3: Enabling and Starting the Service:
Now the next step is to start and enable the cgconfig service. It is the responsibility of cgconfig service, to read and implement the requested settings from the CGroups configuration file, which is typically found at /etc/cgconfig.conf. To start the cgconfig service type the command given below.
sudo systemctl start cgconfig
The cgconfig service can be enabled to start automatically at boot time. Systemd will launch the cgconfig service upon system startup, guaranteeing that the CGroups configuration is implemented consistently. To enable the service at boot, type the command given below.
sudo systemctl enable cgconfig
Output:
Step 4: Creating and Editing CGroup Configuration File:
Now we can also create a configuration file for CGroups. The default configuration file is usually located at /etc/cgconfig.conf. To open the default configuration file type the command given below.
sudo nano /etc/cgconfig.conf
This command will open the Nano text editor, allowing you to edit the cgconfig.conf file
Output:
Now we can make the necessary changes to the configuration file. We can specify group names, limit resources to groups, and other settings as well. For example, we can write the following in the cgconfig.conf file.
group <group_name> {
cpu {
cpu.shares="512";
}
memory {
memory.limit_in_bytes="4G";
}
}
In this example, we are specifying CPU and memory resource constraints using the “cpu.shares” and “memory.limit_in_bytes” parameters respectively. The amount of CPU time that a CGroup receives in relation to other CGroups on the system is determined by its CPU shares. The group will also be restricted from using more than the specified amount of memory.
Output:
Once the configuration has been modified, hit Ctrl + O to save the changes to the file then Enter to validate the filename, and then Ctrl + X to close the Nano editor.
Step 5: Restart the CGroups Service:
Once the modification of the configuration file is completed, restart the service. To restart the CGroup service type the command given below.
sudo systemctl restart cgconfig
Step 6: Verifying the CGroup:
To verify the CGroup service “cgexec” command can be used. The cgexec command is used to execute a command within a specified Control Group (CGroup) with specific resource constraints. To run a process within a CGroup type the command given below.
cgexec -g cpu,memory:<group_name> <command>
Replace the <group_name> with the actual group name that we want to use and which must be present in cgconfig.conf file and also replace <command> with the actual command we want to run. For example, we run a simple command.
Output:
In this example:
- cgexec -g cpu,memory:kaal: Executes the command within the specified CGroup (here kaal) with CPU and memory constraints.
- ls /: The command lists the contents of the root directory.
Step 7: Checking the Status:
We can also check the status of CGroup. We can use the “cat” command to show the PIDs currently in the specified CGroup. To check the status type the command given below.
cat /sys/fs/cgroup/cpu/<group_name>/tasks
Just replace the <group_name> with the actual group name (here kaal) that we want to use and which must be present in cgconfig.conf file. The above command is used to display the list of process IDs (PIDs) associated with a specific Control Group (CGroup).
Output:
Configure CGroups in CentOS Linux
“CGroups” or “Control Groups” is a feature of the Linux kernel that allows us to control and restrict system resources (like CPU, memory, disk I/O, and network) for individual processes or groups of related processes. One of the main objectives is to manage and control the resource allocation of processes, ensuring fair and efficient usage of resources.