Unix/Ubuntu

유저들의 메모리 사용량 총량 제한하기

ForceCore 2024. 8. 21. 12:27

예전에 구글링하면서 시도했었는데 잘 안 됐다. 설정파일 편집하고 어쩌고... 이런건 걸렸는데 적용하는 방법을 몰랐다. 하지만 지금은 챗gpt가 있으니까~

 

https://askubuntu.com/questions/1377502/limit-cpu-and-memory-using-cgroup-in-ubuntu-20-04-lts-server-edition

 

Limit CPU and Memory using cgroup in Ubuntu 20.04 LTS server edition

I have 3 groups in Ubuntu 20.04 LTS server which is group1, group2, group3. I would like to limit CPU and memory for each groups. Group1 only allowed to use 20% CPU and 10GB RAM Group2 only allowe...

askubuntu.com

 

https://gist.github.com/AliKhadivi/bd1fa0fee51a06841f03961b5945f3cf

 

 

/etc/cgconfig.conf 파일을 만들자.

group rnd_group {
    memory {
        memory.limit_in_bytes = 20G;
    }
}

 

 

/etc/cgrules.conf 파일도 만들자.

user1   memory  rnd_group
user2  memory  rnd_group
user3        memory  rnd_group

 

 

* sudo cgconfigparser -l /etc/cgconfig.conf      (cgconfig.conf 적용)

* sudo cgrulesengd -vvv        (cgrules.conf 적용)

* stress -m 4 --vm-bytes 10G --vm-keep -t 10   (4개 worker + 10G per worker로 총 40G의 램 점유 시도)

 

stress는 우분투 기준 stress 패키지가 필요하다. 나머지는 cgroup관련 명령어들은 cgroup-tools 패키지가 필요함.

 

위에서 제시한 cgconfig.conf에서 메모리 점유를 20G로 세팅했으니 40G를 점유하려는 시도는 실패할 것이다. 이렇게:

$ stress -m 4 --vm-bytes 10G --vm-keep -t 10
stress: info: [3753582] dispatching hogs: 0 cpu, 0 io, 4 vm, 0 hdd
stress: FAIL: [3753582] (415) <-- worker 3753585 got signal 9
stress: WARN: [3753582] (417) now reaping child worker processes
stress: FAIL: [3753582] (451) failed run completed in 3s

 

 

나중에 램 허용량을 100G까지 올리고 다시 stress test를 해보면

$ stress -m 4 --vm-bytes 10G --vm-keep -t 10
stress: info: [3884900] dispatching hogs: 0 cpu, 0 io, 4 vm, 0 hdd
stress: info: [3884900] successful run completed in 11s

이렇게 결과가 바뀐다.

 

재부팅후에도 규칙들이 적용되게 하려면 systemctl 세팅을 만들어야하는데 좀 귀찮다...;; askubuntu.com 링크에 적힌대로 하면 됨...;; 이하 복붙한다.

 

 

-------------------------------

 

We have to make this persistent (so that this is all not lost after reboots). We'll create systemd services for doing this.

Create a service for parsing

vi /etc/systemd/system/cgconfigparser.service
Enter the following

[Unit]
Description=cgroup config parser
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/sbin/cgconfigparser -l /etc/cgconfig.conf
Type=oneshot

[Install]
WantedBy=multi-user.target
Create a service for applying the rules

vi /etc/systemd/system/cgrulesgend.service
Enter the following

[Unit]
Description=cgroup rules generator
After=network.target cgconfigparser.service

[Service]
User=root
Group=root
Type=forking
EnvironmentFile=-/etc/cgred.conf
ExecStart=/usr/sbin/cgrulesengd
Restart=on-failure

[Install]
WantedBy=multi-user.target
Now reload the systemd daemon, enable the services, and start them

# Reload context daemon to 'discover' everything
sudo systemctl daemon-reload
# Enable services to run on boot
sudo systemctl enable cgconfigparser
sudo systemctl enable cgrulesgend
# Start them
sudo systemctl start cgconfigparser
sudo systemctl start cgrulesgend