docker内存及cpu限制

在一台物理机上启动了多个docker容器时,就需要对内存及cpu做出相关的限制,以达到容器互不影响的目的

限制内存:-m选项

注:限制内存后进入容器中(free -m)查看内存,显示的内存为物理机的内存

[root@120 ~]# docker ps
CONTAINER ID        IMAGE                                                                                         COMMAND                  CREATED             STATUS              PORTS                                        NAMES
1aeade88d27a        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-product:2.1.8-tag             "/bin/sh -c 'java $J…"   25 hours ago        Up 3 hours                                                       mofang-product-2.1.8-tag
8610b298396c        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-data-task:2.1.8-tag           "/bin/sh -c 'java $J…"   2 days ago          Up 2 days                                                        mofang-data-task-2.1.8-tag
101e1f5ee972        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-order:2.1.8-tag               "/bin/sh -c 'java $J…"   2 days ago          Up 2 days                                                        mofang-order-2.1.8-tag
e994363d153e        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-poster:2.1.8-tag              "/bin/sh -c 'java $J…"   5 days ago          Up 5 days                                                        mofang-poster-2.1.8-tag
4877fd9dbb4c        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-favorable:2.1.8-tag           "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-favorable-2.1.8-tag
7607f38b6370        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-favorable-service:2.1.8-tag   "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-favorable-service-2.1.8-tag
25766b7207d8        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-train-order:2.1.8             "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-train-order-2.1.8
48c320d6c632        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-pay-h5:2.1.8                  "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-pay-h5-2.1.8
8717fae49094        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-api:2.1.8                     "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-api-2.1.8
8fc24cf77676        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-user-service:2.1.8            "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-user-service-2.1.8
8af954b1c40d        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-pro-service:2.1.8             "/bin/sh -c 'java $J…"   8 days ago          Up 8 days                                                        mofang-pro-service-2.1.8
ebe4174da4bd        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-task:2.1.7-tag                "/bin/sh -c 'java $J…"   3 weeks ago         Up 3 weeks                                                       mofang-task-2.1.7-tag
c352d4d80891        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-base:2.1.7                    "/bin/sh -c 'java $J…"   3 weeks ago         Up 3 weeks                                                       mofang-base-2.1.7
d11759013859        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-user:2.1.5                    "/bin/sh -c 'java $J…"   4 weeks ago         Up 4 weeks                                                       mofang-user-2.1.5
6ed77309b86d        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-order-service:2.1.5           "/bin/sh -c 'java $J…"   4 weeks ago         Up 4 weeks                                                       mofang-order-service-2.1.5
eccc0a54b58e        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-direct-tickets-order:2.1.2    "/bin/sh -c 'java $J…"   2 months ago        Up 2 months                                                      mofang-direct-tickets-order-2.1.2
e7a9333952b0        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-direct-presale-order:2.1.2    "/bin/sh -c 'java $J…"   2 months ago        Up 2 months                                                      mofang-direct-presale-order-2.1.2
b03aa8bd0f06        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-pc:2.0.9-2-tag                "/bin/sh -c 'java $J…"   6 months ago        Up 6 months                                                      mofang-pc-2.0.9-2-tag
92ff30ad1fd1        registry.cn-shenzhen.aliyuncs.com/mofang-images-shenzhen/mofang-permission:2.0.9-2-tag        "/bin/sh -c 'java $J…"   6 months ago        Up 6 months                                                      mofang-permission-2.0.9-2-tag
08939d203840        registry.cn-shenzhen.aliyuncs.com/mofang-images/mofang-ueditor:master                         "sh -c 'java $JAVA_O…"   17 months ago       Up 17 months        8778/tcp, 0.0.0.0:5555->5555/tcp, 9779/tcp   mofang-ueditor
[root@120 ~]# 

查看限制结果:docker stats命令  可以看到此容器的内存使用量以及最大可以内存

[root@120 ~]# docker stats 1aeade88d27a

 限制cpu的使用个数:--cpuset-cpus

--cpuset-cpus使用方法:

    0,1,3,5:指定使用0,1,3,5的cpu

    0-3:使用0,1,2,3的cpu

测试限制cpu是否成功:

注:限制cpu后进入容器中通过top查看的还是物理机的cpu

测试方法:agileek/cpuset-test

 下载CPU测试p_w_picpath;agileek/cpuset-test给出了一种用于测试CPU的p_w_picpath,功能就是将指定的CPU资源用满

[root@47 ~]# docker pull agileek/cpuset-test
Using default tag: latest
Trying to pull repository docker.io/agileek/cpuset-test ... 
latest: Pulling from docker.io/agileek/cpuset-test
93ac0475d7f8: Pull complete 
Digest: sha256:e35ce26d2c801af5e22dbb994dbaa4a17688caa5d7c8faeb1e8dad35f2e7b56e
Status: Downloaded newer image for docker.io/agileek/cpuset-test:latest
[root@47 ~]# 

测试--cpuset-cpus=0

另开一窗口观察cpu的压力情况:mpstat -P ALL 3(mpstat安装包为sysstat)

可以看到cpu0的在完全占用的情况,其余的cpu并没有受到影响

由此判定--cpuset-cpus的高定生效了

[root@47 ~]# mpstat -P ALL 3
Linux 3.10.0-862.14.4.el7.x86_64 (47.113.108.247) 	01/28/2022 	_x86_64_	(8 CPU)

02:18:25 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:18:28 PM  all   13.25    0.00    0.84    0.00    0.00    0.08    0.00    0.00    0.00   85.83
02:18:28 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
02:18:28 PM    1    0.33    0.00    0.00    0.00    0.00    0.33    0.00    0.00    0.00   99.33
02:18:28 PM    2    1.35    0.00    2.02    0.00    0.00    0.00    0.00    0.00    0.00   96.63
02:18:28 PM    3    0.33    0.00    0.33    0.00    0.00    0.33    0.00    0.00    0.00   99.00
02:18:28 PM    4    1.35    0.00    1.68    0.00    0.00    0.00    0.00    0.00    0.00   96.97
02:18:28 PM    5    0.34    0.00    0.34    0.00    0.00    0.00    0.00    0.00    0.00   99.33
02:18:28 PM    6    1.67    0.00    2.01    0.00    0.00    0.00    0.00    0.00    0.00   96.32
02:18:28 PM    7    0.34    0.00    0.67    0.00    0.00    0.00    0.00    0.00    0.00   98.99

02:18:28 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:18:31 PM  all   13.67    0.00    1.42    0.00    0.00    0.54    0.00    0.00    0.00   84.36
02:18:31 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
02:18:31 PM    1    0.32    0.00    0.65    0.00    0.00    4.21    0.00    0.00    0.00   94.82
02:18:31 PM    2    1.69    0.00    2.03    0.00    0.00    0.00    0.00    0.00    0.00   96.27
02:18:31 PM    3    1.34    0.00    0.67    0.33    0.00    0.00    0.00    0.00    0.00   97.66
02:18:31 PM    4    2.34    0.00    2.68    0.00    0.00    0.00    0.00    0.00    0.00   94.98
02:18:31 PM    5    0.34    0.00    1.01    0.00    0.00    0.00    0.00    0.00    0.00   98.65
02:18:31 PM    6    2.04    0.00    2.72    0.00    0.00    0.00    0.00    0.00    0.00   95.24
02:18:31 PM    7    0.68    0.00    0.68    0.00    0.00    0.00    0.00    0.00    0.00   98.65

02:18:31 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:18:34 PM  all   13.44    0.00    1.00    0.08    0.00    0.33    0.00    0.00    0.00   85.14
02:18:34 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
02:18:34 PM    1    0.00    0.00    0.33    0.00    0.00    1.97    0.00    0.00    0.00   97.70
02:18:34 PM    2    2.01    0.00    2.01    0.33    0.00    0.00    0.00    0.00    0.00   95.65
02:18:34 PM    3    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
02:18:34 PM    4    1.35    0.00    1.68    0.00    0.00    0.00    0.00    0.00    0.00   96.97
02:18:34 PM    5    0.34    0.00    0.34    0.34    0.00    0.00    0.00    0.00    0.00   98.99
02:18:34 PM    6    1.67    0.00    2.00    0.33    0.00    0.00    0.00    0.00    0.00   96.00
02:18:34 PM    7    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00   98.01

02:18:34 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
02:18:37 PM  all   14.35    0.00    1.13    0.38    0.00    0.13    0.00    0.00    0.00   84.02
02:18:37 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
02:18:37 PM    1    0.00    0.00    0.00    0.00    0.00    1.33    0.00    0.00    0.00   98.67
02:18:37 PM    2    1.01    0.00    2.02    0.00    0.00    0.00    0.00    0.00    0.00   96.97
02:18:37 PM    3    8.70    0.00    1.67    2.68    0.00    0.00    0.00    0.00    0.00   86.96
02:18:37 PM    4    2.00    0.00    2.33    0.00    0.00    0.00    0.00    0.00    0.00   95.67
02:18:37 PM    5    0.99    0.00    1.32    0.00    0.00    0.00    0.00    0.00    0.00   97.68
02:18:37 PM    6    1.35    0.00    1.69    0.00    0.00    0.00    0.00    0.00    0.00   96.96
02:18:37 PM    7    0.34    0.00    0.34    0.00    0.00    0.00    0.00    0.00    0.00   99.32

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值