设为首页 收藏本站
查看: 1423|回复: 0

[经验分享] Docker核心原理-资源隔离和限制

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-8-11 10:01:07 | 显示全部楼层 |阅读模式
通常情况运行docker容器没有指定具体哪个cpu和内存的分配,其实docker官方给出了,docker容器可以运行在指定CPU上通过docker run --help可以看到:
先制作docker镜像:
编写Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[iyunv@linux-node1 ~]# cd stress/
[iyunv@linux-node1 stress]# cat Dockerfile
FROM centos
ADD  epel-6.repo  /etc/yum.repos.d/
RUN  yum -y install stress && yum clean all
ENTRYPOINT ["stress"]
[iyunv@linux-node1 stress]#
[iyunv@linux-node1 stress]# docker build -t stress .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM centos
---> 970633036444
Step 2 : ADD epel-6.repo /etc/yum.repos.d/
---> 77600d6b488e
Removing intermediate container f141f47cb34f
Step 3 : RUN yum -y install stress && yum clean all
---> Running in 0949035c73da
Loaded plugins: fastestmirror, ovl
http://mirrors.aliyuncs.com/epel ... updateinfo.xml.bz2: [Errno 12] Timeout on http://mirrors.aliyuncs.com/epel ... updateinfo.xml.bz2: (28, 'Connection timed out after 30001 milliseconds')
Trying other mirror.
Determining fastest mirrors
* base: mirrors.zju.edu.cn
* epel: mirrors.aliyun.com
* extras: mirrors.zju.edu.cn
* updates: mirrors.cqu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package stress.x86_64 0:1.0.4-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package          Arch             Version                 Repository      Size
================================================================================
Installing:
stress           x86_64           1.0.4-4.el6             epel            36 k
Transaction Summary
================================================================================
Install  1 Package
Total download size: 36 k
Installed size: 89 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : stress-1.0.4-4.el6.x86_64                                    1/1
install-info: No such file or directory for /usr/share/info/stress.info
  Verifying  : stress-1.0.4-4.el6.x86_64                                    1/1
Installed:
  stress.x86_64 0:1.0.4-4.el6                                                   
Complete!
Loaded plugins: fastestmirror, ovl
Cleaning repos: base epel extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
---> 504c747b9b51
Removing intermediate container 0949035c73da
Step 4 : ENTRYPOINT stress
---> Running in ea8d8eb8fb87
---> 7e1e0e5cf825
Removing intermediate container ea8d8eb8fb87
Successfully built 7e1e0e5cf825
[iyunv@linux-node1 stress]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
stress              latest              7e1e0e5cf825        About a minute ago   214 MB
ningx-file          v01                 460b5af495c4        3 days ago           531.9 MB
nginx/ningx         1.9.3v02            097c98e3cb2b        3 days ago           534.6 MB
nginx               1.9.3               d2cfb9ed3f2b        3 days ago           534.6 MB
docker.io/centos    latest              970633036444        11 days ago          196.7 MB
[iyunv@linux-node1 stress]#




查看docker run命令帮助输出:
1
2
3
4
5
6
7
[iyunv@linux-node1 stress]# docker run --help|grep cpu
  --cpu-shares                    CPU shares (relative weight)
  --cpu-period                    Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota                     Limit CPU CFS (Completely Fair Scheduler) quota
  --cpuset-cpus                   CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems                   MEMs in which to allow execution (0-3, 0,1)
[iyunv@linux-node1 stress]#




启动一个容器:
1
2
[iyunv@linux-node1 stress]# docker run  -it --rm --cpuset-cpus=0 stress --cpu 1
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd



在另外一个窗口查看使用情况cpu:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@linux-node1 ~]# top
top - 00:55:15 up 13:39,  2 users,  load average: 0.58, 0.47, 0.69
Tasks: 406 total,   2 running, 404 sleeping,   0 stopped,   0 zombie
%Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   773004 total,   204488 free,   194528 used,   373988 buff/cache
KiB Swap:  2098172 total,  2096324 free,     1848 used.   399644 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                         
  5944 root      20   0    7260     92      0 R 100.0  0.0   0:52.46 stress                                                         
   335 root      20   0       0      0      0 S   0.3  0.0   1:17.95 kworker/3:1                                                     
  1569 root      20   0  553044  13624   2952 S   0.3  1.8   0:13.46 tuned                                                           
  4779 root      20   0       0      0      0 S   0.3  0.0   0:02.25 kworker/1:1                                                     
  5946 root      20   0  157948   2504   1548 R   0.3  0.3   0:00.18 top



同样,再开启一个窗口:
1
2
[iyunv@linux-node1 stress]# docker run  -it --rm stress --cpu 1
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd




再查切换到其他窗口查看cpu使用情况
1
2
3
4
5
6
7
8
9
10
[iyunv@linux-node1 ~]# top
top - 00:57:52 up 13:41,  3 users,  load average: 1.50, 0.96, 0.84
Tasks: 414 total,   2 running, 412 sleeping,   0 stopped,   0 zombie
%Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  2.7 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   773004 total,   195560 free,   199560 used,   377884 buff/cache
KiB Swap:  2098172 total,  2096576 free,     1596 used.   393660 avail Mem
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND




注意命令:
docker run  -it --rm stress --cpu 1
docker run  -it --rm --cpuset-cpus=0 stress --cpu 1
--cpuset-cpus后面跟cpu总个数,为docker容器指定的,而--cpu是用多少个cpu来运行,只能是1、2、3、4 ...N等等


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-256278-1-1.html 上篇帖子: 内网部署支持ssl的docker registry 下篇帖子: corosync+pacemaker+docker 隔离 资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表