在Debian系统中,有多种方法可以进行进程资源隔离。以下是一些常见的方法:
cgroups
(控制组)cgroups
是 Linux 内核提供的一种机制,用于限制、记录和隔离进程组的资源(CPU、内存、磁盘I/O等)。
cgroups
工具sudo apt-get update
sudo apt-get install cgroup-tools
sudo cgcreate -g memory:/mygroup
echo "100M" | sudo tee /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes
sudo cgclassify -g memory:mygroup <pid>
systemd
的 slice
systemd
提供了 slice
概念,可以用来隔离一组服务的资源。
sudo systemctl isolate myslice.slice
编辑 /etc/systemd/system/myslice.slice
文件,添加资源限制配置:
[Slice]
MemoryLimit=100M
CPUQuota=50%
然后重新加载 systemd 配置并重启 slice:
sudo systemctl daemon-reload
sudo systemctl restart myslice.slice
Docker
Docker
是一个流行的容器化平台,可以用来隔离进程及其资源。
sudo apt-get update
sudo apt-get install docker.io
sudo docker run -it --memory="100m" --cpus="1.0" ubuntu:latest /bin/bash
LXC
(Linux Containers)LXC
是一种轻量级的虚拟化技术,可以用来隔离进程。
sudo apt-get update
sudo apt-get install lxc
sudo lxc-create -t download -n mycontainer -d ubuntu -- --dist ubuntu-bionic64
sudo lxc-start -n mycontainer
编辑容器的配置文件 /var/lib/lxc/mycontainer/config
,添加资源限制配置:
lxc.cgroup.devices.allow = a
lxc.mount.auto = proc:rw sys:rw
lxc.mount.auto = devpts:rw
lxc.mount.auto = tmpfs:rw
lxc.mount.auto = tmpfs:ro
lxc.mount.auto = tmpfs:suid
lxc.mount.auto = tmpfs:exec
lxc.mount.auto = tmpfs:dev
lxc.mount.auto = tmpfs:mnt
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto = tmpfs:sbin
lxc.mount.auto = tmpfs:bin
lxc.mount.auto = tmpfs:devpts
lxc.mount.auto = tmpfs:sysfs
lxc.mount.auto = tmpfs:proc
lxc.mount.auto = tmpfs:run
lxc.mount.auto = tmpfs:lock
lxc.mount.auto = tmpfs:ipc
lxc.mount.auto = tmpfs:usr
lxc.mount.auto = tmpfs:opt
lxc.mount.auto = tmpfs:srv
lxc.mount.auto = tmpfs:home
lxc.mount.auto = tmpfs:root
lxc.mount.auto = tmpfs:boot
lxc.mount.auto = tmpfs:etc
lxc.mount.auto = tmpfs:var
lxc.mount.auto = tmpfs:lib
lxc.mount.auto = tmpfs:lib64
lxc.mount.auto = tmpfs:usr/local
lxc.mount.auto