332.3 Resource Control
Weight: 3
Description: Candidates should be able to restrict the resources services and programs can consume.
Key Knowledge Areas:
Understand and configure ulimits
Understand cgroups, including classes, limits and accounting
Manage cgroups and process cgroup association
Understand systemd slices, scopes and services
Use systemd units to limit the system resources processes can consume
Awareness of cgmanager and libcgroup utilities
Partial list of the used files, terms and utilities:
ulimit
/etc/security/limits.conf
pam_limits.so
/sys/fs/group/
/proc/cgroups
systemd-cgls
systemd-cgtop
Managing system resources
Linux operating systems have the ability to limit the amount of various system resources available to a user process. These limitations include how many files a process can have open, how large of a file the user can create, and how much memory can be used by the different components of the process. ulimit is the command used to accomplish this.
ulimit
The ulimit command provides control over the resources available to the shell and/or to processes started by it.
To get the report in details, add the “-a” flag at the end. This will print all the resource limits for the current user.
To set ulimit value on a parameter use the below command:
ulimit -<letter Option> <NewValue>
as an example lets put limits on file size in the current shell:
/etc/security/limits.conf
For the ulimits to persists across reboots we need to set the ulimit values in the configuration file /etc/security/limits.conf
. it is also used for system wide limits:
There are two types of limits: A soft limit is like a warning and hard limit is a real max limit. For example, following will prevent anyone in the faculty group from having more than 50 processes, and a warning will be given at 20 processes.
note: soft limit cannot be higher than the hard limit.
this part was from my lpic2 book: https://borosan.gitbook.io/lpic1-exam-guide/1101-perform-security-administration-tasks#ulimit
ulimits is a part of pluggable authentication module(PAM) system.
to have a full review of PAM visit my lpic2 book: https://borosan.gitbook.io/lpic2-exam-guide/2102-pam-authentication#pam-pluggable-authentication-modules
pam_limits.so
The pam_limits PAM module sets limits on the system resources that can be obtained in a user-session. Users of uid=0 are affected by this limits, too. By default limits are taken from the /etc/security/limits.conf
config file. Then ndividual files from the/etc/security/limits.d/
directory are read.
As you can see pam__limists.so is used in session module
so instead of manipulating that module which would have effects on other services we put our setting inside /etc/security/limits.conf
cgroups
What are cgroups?
Red Hat Enterprise Linux 6 provides a new kernel feature: control groups, which are called by their shorter name cgroups in this guide. Cgroups allow you to allocate resources — such as CPU time, system memory, network bandwidth, or combinations of these resources — among user-defined groups of tasks (processes) running on a system.
Why we do need cgroups?
the Linux kernel is responsible for all of the hardware interacting reliably on a system. That means, aside from just the bits of code (drivers) that enable the operating system (OS) to understand the hardware, it also sets limits on how many resources a particular program can demand from the system. This is most easily understood when talking about the amount of memory (RAM) a system has to divide up amongst all of the applications your computer may execute. In its most basic form, a Linux system is allowed to run most applications without restriction. This can be great for general computing if all applications play nicely together. But what happens if there is a bug in a program, and it starts to consume all of the available memory? The kernel has a facility called the Out Of Memory (OOM) Killer. Its job is to halt applications in order to free up enough RAM so that the OS may continue to function without crashing.
That's great, but what does this have to do with cgroups? Well, the OOM process acts as a last line of defense before your system comes crashing down around you. It's useful to a point, but since the kernel can control which processes must survive the OOM, it can also determine which applications cannot consume too much RAM in the first place.
Cgroups are, therefore, a facility built into the kernel that allow the administrator to set resource utilization limits on any process on the system.
How do cgroups work?
For the purposes of this discussion, we are talking about cgroups V1. While version 2 is available in Red Hat Enterprise Linux 8 (RHEL 8), it is disabled by default. Most container technologies such as Kubernetes, OpenShift, Docker, and so on still rely on cgroups version 1.
We have already discussed that cgroups are a mechanism for controlling certain subsystems in the kernel. These subsystems, such as devices, CPU, RAM, network access, and so on, are called controllers in the cgroup terminology.
Each type of controller (cpu
, blkio
, memory
, etc.) is subdivided into a tree-like structure. Each branch or leaf has its own weights or limits. A control group has multiple processes associated with it, making resource utilization granular and easy to fine-tune.
NOTE: Each child inherits and is restricted by the limits set on the parent cgroup.
In the diagram above, you can see that it is possible to have PID 1
in memory
, disk i/o
, and cpu
control groups. The cgroups are created per resource type and have no association with each other. That means you could have a database
group associated with all of the controllers, but the groups are treated independently. Like GIDs, these groups are assigned a numeric value upon creation and not a friendly name. Under the hood, the kernel uses these values to determine resource allocation.
/procs/cgroups
You may see enabled control groups on your computer via proc filesystem:
/sys/fs/group
or via sysfs cat /sys/fs/group/
Obtaining Information about Control Groups
the systemd-cgls
command is provided to view the hierarchy of control groups and systemd-cgtop
to monitor their resource consumption in real time.
systemd-cgls
When systemd-cgls
is issued without parameters, it returns the entire cgroup hierarchy. The highest level of the cgroup tree is formed by slices and can look as follows:
Note that machine slice is present only if you are running a virtual machine or a container.
For more information on the cgroup tree, read about “Systemd Unit Types”.
To reduce the output of systemd-cgls
, and to view a specified part of the hierarchy, execute:
Replace name with a name of the resource controller you want to inspect.
systemd-cgtop
The systemd-cgls
command provides a static snapshot of the cgroup hierarchy. To see a dynamic account of currently running cgroups ordered by their resource usage (CPU, Memory, and IO), use:
The behavior, provided statistics, and control options of systemd-cgtop
are akin of those of the top
utility. See systemd-cgtop
(1) manual page for more information.
that's all.
.
.
.
resources:
https://www.redhat.com/sysadmin/cgroups-part-one
https://0xax.gitbooks.io/linux-insides/content/Cgroups/linux-cgroups-1.html
.
Last updated