Open In App

Configure CGroups in CentOS Linux

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

“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.

Important Features of CGroups

Administrators can set resource limits for individual processes or groups of related processes using CGroups. This helps avoid system instability or performance degradation caused by a single process or group of processes using up all system resources. It is organized in a hierarchical structure, allowing for the creation of nested groups. This makes it easy to apply different resource constraints at various levels of the hierarchy. It provides different types of resource controllers for managing a specific set of resources. Some common controllers include:

  1. CPU: manages CPU time usage.
  2. memory: controls memory usage.
  3. blkio: manages block I/O (disk I/O) access.
  4. net_cls: tags network packets with a class identifier.

Some Common Uses of CGroups

In a cloud environment:

CGroup is useful in cloud computing environments where multiple users share the same physical infrastructure. For instance, to avoid one customer’s workload adversely affecting others, a cloud provider that hosts virtual machines (VMs) for multiple customers can use CGroups to assign CPU, memory, and other resources to each VM. In this way, it helps ensure fair resource allocation.

Resource scarcity prevention:

It can be explained with an example, let’s suppose a process uses too much CPU or memory, leading to degraded performance for other processes on the system. Administrators can guarantee that no single process monopolizes resources by imposing restrictions on resource usage through CGroups, thereby preserving a more responsive and stable system.

Energy Efficiency in Data Centres:

In Data Centres, power consumption can also be optimized by using CGroups to control CPU frequency and other power-related settings about the workload. This is very helpful for large-scale data centers, where cutting energy costs can result in significant savings.

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:

Screenshot-2023-11-24-233046

Checking Support

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:

Screenshot-2023-11-24-233734

Installing CGroup

Output 2:

Screenshot-2023-11-24-233815

Installed Successfully

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:

Screenshot-2023-11-24-234212

Starting and Enabling the Service

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:

Screenshot-2023-11-24-234406

Nano Text Editor

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:

Screenshot-2023-11-25-004156

Editing Configuration File

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:

Screenshot-2023-12-01-163754

Running process within a CGroup

In this example:

  1. cgexec -g cpu,memory:kaal: Executes the command within the specified CGroup (here kaal) with CPU and memory constraints.
  2. 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:

Screenshot-2023-12-01-165158

Checking the Status

Conclusion:

The configuration of Control Groups (CGroups) involves defining resource constraints for specific groups of processes, allowing for smooth control over system resource allocation. The most important file where CGroup specifications are given is cgconfig.conf, which contains settings for memory and CPU limitations. Since It ensures optimal performance and stability in contexts with various workloads, this level of resource isolation is especially valuable.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads