hq8501 发表于 2018-5-29 08:42:11

Docker核心原理

  通常情况运行docker容器没有指定具体哪个cpu和内存的分配,其实docker官方给出了,docker容器可以运行在指定CPU上通过docker run --help可以看到:
  先制作docker镜像:
  编写Dockerfile
# cd stress/
# cat Dockerfile
FROM centos
ADDepel-6.repo/etc/yum.repos.d/
RUNyum -y install stress && yum clean all
ENTRYPOINT ["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/6/x86_64/repodata/98697cba0d79c083725de0c38b11c15878b35f5e27262becc02cd7ed1a13ac22-updateinfo.xml.bz2: Timeout on http://mirrors.aliyuncs.com/epel/6/x86_64/repodata/98697cba0d79c083725de0c38b11c15878b35f5e27262becc02cd7ed1a13ac22-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
================================================================================
Install1 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
# 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
#  

  查看docker run命令帮助输出:
# 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)
#  

  启动一个容器:
# docker run-it --rm --cpuset-cpus=0 stress --cpu 1
stress: info: dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd  在另外一个窗口查看使用情况cpu:
# 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      PRNI    VIRT    RES    SHR S%CPU %MEM   TIME+ COMMAND                                                         
5944 root      20   0    7260   92      0 R 100.00.0   0:52.46 stress                                                         
   335 root      20   0       0      0      0 S   0.30.0   1:17.95 kworker/3:1                                                   
1569 root      20   055304413624   2952 S   0.31.8   0:13.46 tuned                                                         
4779 root      20   0       0      0      0 S   0.30.0   0:02.25 kworker/1:1                                                   
5946 root      20   0157948   2504   1548 R   0.30.3   0:00.18 top  同样,再开启一个窗口:
# docker run-it --rm stress --cpu 1
stress: info: dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd  

  再查切换到其他窗口查看cpu使用情况
# 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      PRNI    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]
查看完整版本: Docker核心原理