docker通过cgroup来控制容器使用的资源配额,包括CPU、内存、磁盘三大方面。
1.限制内存
查询系统中已经mount的cgroup的文件系统,这里的t表示type
[[email protected] ~]# mount -t cgroup
搜索cgroup软件包
[[email protected] ~]# yum search cgroup
安装libcgroup
[[email protected] ~]# yum install -y libcgroup-tools.x86_64
[[email protected] ~]# cd /sys/fs/cgroup/memory/
[[email protected] memory]# ls
创建目录
[[email protected] memory]# mkdir x1
[[email protected] memory]# cd x1
[[email protected] x1]# ls
[[email protected] x1]# cat memory.limit_in_bytes
9223372036854771712
[[email protected] x1]# cat memory.memsw.limit_in_bytes
9223372036854771712
限制内存;200M = 1024 * 200 = 209715200
[[email protected] x1]# echo 209715200 > memory.limit_in_bytes
[[email protected] x1]# echo 209715200 > memory.memsw.limit_in_bytes
[[email protected] x1]# cat memory.limit_in_bytes
209715200
[[email protected] x1]# cat memory.memsw.limit_in_bytes
209715200
[[email protected] x1]# cd /dev/shm
[[email protected] shm]# ls
[[email protected] shm]# free -m
[[email protected] shm]# cgexec -g memory:x1 dd if=/dev/zero of=bigfile
[[email protected] shm]# free -m
[[email protected] shm]# cgexec -g memory:x1 dd if=/dev/zero of=bigfile bs=1M count=300
还原
[[email protected] shm]# ls
bigfile
[[email protected] shm]# rm -rf bigfile
[[email protected] shm]# free -m
2.限制cpu
[[email protected] ~]# systemctl start docker
[[email protected] ~]# mount -t cgroup
[[email protected] ~]# cd /sys/fs/cgroup/
[[email protected] cgroup]# ls
blkio cpu,cpuacct freezer net_cls perf_event
cpu cpuset hugetlb net_cls,net_prio pids
cpuacct devices memory net_prio systemd
[[email protected] cgroup]# cd cpu
[[email protected] cpu]# ls
cgroup.clone_children cpu.cfs_period_us machine.slice
cgroup.event_control cpu.cfs_quota_us notify_on_release
cgroup.procs cpu.rt_period_us release_agent
cgroup.sane_behavior cpu.rt_runtime_us system.slice
cpuacct.stat cpu.shares tasks
cpuacct.usage cpu.stat user.slice
cpuacct.usage_percpu docker
建立目录
[[email protected] cpu]# mkdir x1
[[email protected] cpu]# cd x1/
[[email protected] x1]# ls
-1表示无限制
[[email protected] x1]# cat cpu.cfs_quota_us
-1
[[email protected] x1]# cat cpu.cfs_period_us
100000
非交互式限制control group占用时间为20000微秒
[[email protected] x1]# echo 20000 > cpu.cfs_quota_us
[[email protected] x1]# cat cpu.cfs_quota_us
20000
[[email protected] x1]# cat cpu.cfs_period_us
100000
[[email protected] x1]# dd if=/dev/zero of=/dev/null &
[1] 8110
查看cpu为100%
[[email protected] ~]# top
[[email protected] ~]# cd /sys/fs/cgroup/cpu/x1
[[email protected] x1]# ls
cgroup.clone_children cpuacct.usage_percpu cpu.shares
cgroup.event_control cpu.cfs_period_us cpu.stat
cgroup.procs cpu.cfs_quota_us notify_on_release
cpuacct.stat cpu.rt_period_us tasks
cpuacct.usage cpu.rt_runtime_us
[[email protected] x1]# cat tasks
查看id
[[email protected] ~]# top
[[email protected] x1]# pwd
/sys/fs/cgroup/cpu/x1
[[email protected] x1]# echo 8110 > tasks
查看cpu
[[email protected] ~]# top
将dd进程调回并停止
[[email protected] x1]# fg
[[email protected] ~]# docker ps -a
[[email protected] ~]# docker images
--cpu-quota表示限制cpu
[[email protected] ~]# docker run -it --name vm6 --cpu-quota=20000 ubuntu
[email protected]:/# dd if=/dev/zero of=/dev/null
查看cpu;为20%
[[email protected] ~]# top
^C11016001+0 records in
11016000+0 records out
5640192000 bytes (5.6 GB) copied, 79.2576 s, 71.2 MB/s
[email protected]:/# exit
exit
[[email protected] ~]# docker rm vm6
vm6
[[email protected] ~]# docker run -it --name vm6 ubuntu
[email protected]:/# dd if=/dev/zero of=/dev/null
查看cpu;为100%
[[email protected] ~]# top
^C20341261+0 records in
20341260+0 records out
10414725120 bytes (10 GB) copied, 28.9112 s, 360 MB/s
[email protected]:/# exit
exit
[[email protected] ~]# docker rm vm6
vm6
[[email protected] ~]# docker run -it --name vm6 --cpu-quota=20000 ubuntu
[email protected]:/#
[[email protected] docker]# cd /sys/fs/cgroup/cpu/docker
[[email protected] docker]# ls
cgroup.clone_children
cgroup.event_control
cgroup.procs
cpuacct.stat
cpuacct.usage
cpuacct.usage_percpu
cpu.cfs_period_us
cpu.cfs_quota_us
cpu.rt_period_us
cpu.rt_runtime_us
cpu.shares
cpu.stat
d23d8a6edfd2ce61c1d98fc84317d53ab0dcc1eb0a34ab40848ddda61a5cf203
notify_on_release
tasks
[[email protected] docker]# cd d23d8a6edfd2ce61c1d98fc84317d53ab0dcc1eb0a34ab40848ddda61a5cf203
[[email protected] d23d8a6edfd2ce61c1d98fc84317d53ab0dcc1eb0a34ab40848ddda61a5cf203]# cat cpu.cfs_quota_us
20000
3.限制磁盘
默认进入容器后,只享有普通用户权限
此方式权限过大
[[email protected] ~]# docker run -it --rm --privileged=true ubuntu
[email protected]:/# fdisk -l
[email protected]:/# exit
exit
添加权限
[[email protected] ~]# docker run -it --rm --cap-add=NET_ADMIN ubuntu
[email protected]:/# fdisk -l
[email protected]:/# ip addr
[email protected]:/# ip addr add 172.18.0.4/24 dev eth0
[email protected]:/# ip addr
[email protected]:/# exit
exit
限制写入速度:
[[email protected] ~]# cat /proc/partitions
--device-write-bps表示限制写入速度
[[email protected] ~]# docker run -it --rm --device-write-bps /dev/sda:30MB ubuntu
发现写入速度限制为了每秒30
[email protected]:/# dd if=/dev/zero of=file bs=1M count=300
3.限制内存
(1).安装lxcfs
[[email protected] ~]# cd lxcfs/
[[email protected] lxcfs]# ls
lxcfs-2.0.5-3.el7.centos.x86_64.rpm lxcfs-3.0.3.tar.gz
[[email protected] lxcfs]# yum install -y lxcfs-2.0.5-3.el7.centos.x86_64.rpm
[[email protected] lxcfs]# cd /var/lib/lxcfs/
[[email protected]server1 lxcfs]# ls
(2).执行lxcfs
[[email protected] ~]# lxcfs /var/lib/lxcfs &
[1] 11749
[[email protected] ~]# cd /var/lib/lxcfs/
生成了proc目录
[[email protected] lxcfs]# ls
cgroup proc
[[email protected] lxcfs]# cd proc/
[[email protected] proc]# ls
cpuinfo diskstats meminfo stat swaps uptime #cpu 磁盘 内存 状态 swaps uptime
(3).下载并导入镜像
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[[email protected] ~]# ls
docker lxcfs ubuntu.tar
[[email protected] ~]# docker load -i ubuntu.tar
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 07c86167cdc4 3 years ago 188MB
4).创建容器
[[email protected] proc]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[[email protected] proc]# docker run -it --name vm1 -m 200m -v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo
> -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats
> -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo
> -v /var/lib/lxcfs/proc/stat:/proc/stat
> -v /var/lib/lxcfs/proc/swaps:/proc/swaps
> -v /var/lib/lxcfs/proc/uptime:/proc/uptime
> ubuntu
测试:
[email protected]:/# free -m
原文地址:https://www.cnblogs.com/ztxd/p/12276018.html