Docker必知必会

1.为什么需要Docker?Docker与虚拟机的区别?

Docker的定义:
官方解释:Package Software into Standardized Units for Development,Shipment and Deployment.
将软件打包成标准化单元,用于开发、装运和部署。

2.Docker VS 虚拟机

在这里插入图片描述

3.为什么需要Docker

在这里插入图片描述

4.Docker的系统组成

在这里插入图片描述

Docker daemon

Daemon是Docker的守护进程,Docker Client通过命令行与Docker Damon通信,完成Docker相关操作。daemon的主要功能包括镜像管理、镜像构建、REST API、身份验证、安全、核心网络以及编排。

Docker daemon工作机制

Docker Daemon可以认为是通过Docker Server模块接受Docker Client的请求,并在Engine中处理请求,然后根据请求类型,创建出指定的Job并运行,运行过程的作用有以下几种可能:向Docker Registry获取镜像,通过graphdriver执行容器镜像的本地化操作,通过networkdriver执行容器网络环境的配置,通过execdriver执行容器内部运行的执行工作等。

registry

镜像中心,公有镜像中心docker hub,也可以搭建私有的镜像中心。

docker build

将请求发送给Docker Daemon,构建镜像上传到镜像中心

docker run

把某一个镜像运行起来变成一个容器,镜像和容器是相互关联的,容器就是镜像运行时的一个形态,容器是变化的,镜像是不可变的。

Docker namespace隔离

Docker namespace隔离类型

●UTS namespace

UTS(UNIX Time-sharing System)namespace提供了主机名与域名的隔离,这样每个docke容器就可以拥有独立的主机名和域名了,在网络上可以被视为一个独立的节点,而非宿主机上的一个进程。

●IPC namespace

进程间通信(Inter-Process Communication, IPC)涉及的IPC资源包括常见的信号量、消息队列和共享内存。在同一个IPC namespace下的进程彼此可见,不同IPC namespace下的进程则互相不可见。

●PID namespace

PID namespace隔离非常实用,它对进程PID重新标号,即两个不同namespace下的进程可以有相同的PID。每个PID namespace都有自己的计数程序。内核为所有的PID namespace维护了-个树状结构,最顶层的是系统初始时创建的,被称为root namespace,它创建的心PID namespace被称为child namespace(树的子节点)。

通过这种方式,不同的PID namespace会形成一个层级体系。所属的父节点可以看到子节点中的进程,并可以通.过信号等方式对子节点中的进程产生影响。反过来,子节点却不能看到父节点PID namespace中的任何内容。

调用 unshare()进行进程隔离,他与clone()很像,不同的是,unshare()运行在原先的进程上,不需要启动一个新进程
实践,使用 unshare --fork --pid --mount-proc bash打开一个新的namespace命名空间,exit退出

[root@master ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.2 125640  4040 ?        Ss   07:48   0:01 /usr/lib/systemd/systemd --switched-root --system --deserial
root          2  0.0  0.0      0     0 ?        S    07:48   0:00 [kthreadd]
root          4  0.0  0.0      0     0 ?        S<   07:48   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        R    07:48   0:02 [ksoftirqd/0]
root          7  0.0  0.0      0     0 ?        S    07:48   0:03 [migration/0]
root          8  0.0  0.0      0     0 ?        S    07:48   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    07:48   0:07 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   07:48   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    07:48   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    07:48   0:00 [watchdog/1]
root         13  0.0  0.0      0     0 ?        S    07:48   0:02 [migration/1]
root         14  0.0  0.0      0     0 ?        S    07:48   0:04 [ksoftirqd/1]
root         16  0.0  0.0      0     0 ?        S<   07:48   0:00 [kworker/1:0H]
root         18  0.0  0.0      0     0 ?        S    07:48   0:00 [kdevtmpfs]
root         19  0.0  0.0      0     0 ?        S<   07:48   0:00 [netns]
root         20  0.0  0.0      0     0 ?        S    07:48   0:00 [khungtaskd]
root         21  0.0  0.0      0     0 ?        S<   07:48   0:00 [writeback]
root         22  0.0  0.0      0     0 ?        S<   07:48   0:00 [kintegrityd]
root         23  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root         24  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root         25  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root         26  0.0  0.0      0     0 ?        S<   07:48   0:00 [kblockd]
root         27  0.0  0.0      0     0 ?        S<   07:48   0:00 [md]
root         28  0.0  0.0      0     0 ?        S<   07:48   0:00 [edac-poller]
root         29  0.0  0.0      0     0 ?        S<   07:48   0:00 [watchdogd]
root         36  0.0  0.0      0     0 ?        S    07:48   0:00 [kswapd0]
root         37  0.0  0.0      0     0 ?        SN   07:48   0:00 [ksmd]
root         38  0.0  0.0      0     0 ?        SN   07:48   0:01 [khugepaged]
root         39  0.0  0.0      0     0 ?        S<   07:48   0:00 [crypto]
root         47  0.0  0.0      0     0 ?        S<   07:48   0:00 [kthrotld]
root         49  0.0  0.0      0     0 ?        S<   07:48   0:00 [kmpath_rdacd]
root         50  0.0  0.0      0     0 ?        S<   07:48   0:00 [kaluad]
root         51  0.0  0.0      0     0 ?        S<   07:48   0:00 [kpsmoused]
root         53  0.0  0.0      0     0 ?        S<   07:48   0:00 [ipv6_addrconf]
root         66  0.0  0.0      0     0 ?        S<   07:48   0:00 [deferwq]
root        101  0.0  0.0      0     0 ?        S    07:48   0:02 [kauditd]
root        282  0.0  0.0      0     0 ?        S<   07:48   0:00 [nfit]
root        283  0.0  0.0      0     0 ?        S<   07:48   0:00 [mpt_poll_0]
root        284  0.0  0.0      0     0 ?        S<   07:48   0:00 [mpt/0]
root        285  0.0  0.0      0     0 ?        S<   07:48   0:00 [ata_sff]
root        288  0.0  0.0      0     0 ?        S    07:48   0:00 [scsi_eh_0]
root        290  0.0  0.0      0     0 ?        S<   07:48   0:00 [scsi_tmf_0]
root        295  0.0  0.0      0     0 ?        S    07:48   0:00 [scsi_eh_1]
root        298  0.0  0.0      0     0 ?        S<   07:48   0:00 [scsi_tmf_1]
root        300  0.0  0.0      0     0 ?        S    07:48   0:00 [scsi_eh_2]
root        301  0.0  0.0      0     0 ?        S<   07:48   0:00 [scsi_tmf_2]
root        305  0.0  0.0      0     0 ?        S    07:48   0:00 [irq/16-vmwgfx]
root        306  0.0  0.0      0     0 ?        S<   07:48   0:00 [ttm_swap]
root        377  0.0  0.0      0     0 ?        S<   07:48   0:00 [kdmflush]
root        378  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root        388  0.0  0.0      0     0 ?        S<   07:48   0:00 [kdmflush]
root        389  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root        401  0.0  0.0      0     0 ?        S<   07:48   0:00 [bioset]
root        402  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfsalloc]
root        403  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs_mru_cache]
root        404  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-buf/dm-0]
root        405  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-data/dm-0]
root        406  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-conv/dm-0]
root        407  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-cil/dm-0]
root        408  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-reclaim/dm-]
root        409  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-log/dm-0]
root        410  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-eofblocks/d]
root        411  0.0  0.0      0     0 ?        S    07:48   0:07 [xfsaild/dm-0]
root        412  0.0  0.0      0     0 ?        S<   07:48   0:00 [kworker/1:1H]
root        493  0.0  0.1  39056  3376 ?        Ss   07:48   0:00 /usr/lib/systemd/systemd-journald
root        528  0.0  0.0 198572  1344 ?        Ss   07:48   0:00 /usr/sbin/lvmetad -f
root        533  0.0  0.1  45764  2204 ?        Ss   07:48   0:00 /usr/lib/systemd/systemd-udevd
root        616  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-buf/sda1]
root        617  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-data/sda1]
root        618  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-conv/sda1]
root        619  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-cil/sda1]
root        620  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-reclaim/sda]
root        621  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-log/sda1]
root        622  0.0  0.0      0     0 ?        S<   07:48   0:00 [xfs-eofblocks/s]
root        623  0.0  0.0      0     0 ?        S    07:48   0:00 [xfsaild/sda1]
root        642  0.0  0.0      0     0 ?        S<   07:48   0:00 [kworker/0:1H]
root        643  0.0  0.0  55532  1076 ?        S<sl 07:48   0:10 /sbin/auditd
root        669  0.0  0.0  21684  1304 ?        Ss   07:48   0:00 /usr/sbin/irqbalance --foreground
polkitd     670  0.0  0.5 612232 11164 ?        Ssl  07:48   0:00 /usr/lib/polkit-1/polkitd --no-debug
root        672  0.0  0.0  26384  1784 ?        Ss   07:48   0:00 /usr/lib/systemd/systemd-logind
dbus        674  0.0  0.1  58216  2492 ?        Ss   07:48   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --
root        675  0.0  0.4 548612  9032 ?        Ssl  07:48   0:00 /usr/sbin/NetworkManager --no-daemon
root        677  0.0  0.2 168304  5140 ?        Ss   07:48   0:00 /usr/bin/VGAuthService -s
root        679  0.0  0.2 273188  4872 ?        Ssl  07:48   0:20 /usr/bin/vmtoolsd
root        681  0.0  0.0 126388  1616 ?        Ss   07:48   0:00 /usr/sbin/crond -n
root        689  0.0  0.1  99208  2648 ?        Ss   07:48   0:00 login -- root
root        948  0.0  0.9 574276 17332 ?        Ssl  07:48   0:02 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root        951  0.0  0.2 112900  4340 ?        Ss   07:48   0:00 /usr/sbin/sshd -D
root        952  0.0  0.3 218528  6988 ?        Ssl  07:48   0:01 /usr/sbin/rsyslogd -n
root        960  0.2  2.6 1042440 50028 ?       Ssl  07:48   0:55 /usr/bin/containerd
root       1113  0.0  0.1  89708  2220 ?        Ss   07:48   0:00 /usr/libexec/postfix/master -w
postfix    1115  0.0  0.2  89880  4084 ?        S    07:48   0:00 qmgr -l -t unix -u
root       1228  0.0  4.7 731412 89312 ?        Ssl  07:48   0:20 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/conta
root       1624  0.0  0.0 115540  1844 tty1     Ss+  07:49   0:00 -bash
ntp        8191  0.0  0.1  29960  2160 ?        Ss   07:55   0:00 /usr/sbin/ntpd -u ntp:ntp -g
root      18994  0.0  0.0      0     0 ?        S    13:45   0:00 [kworker/1:2]
root      24747  0.0  0.0      0     0 ?        S    13:50   0:00 [kworker/1:0]
root      31081  0.0  0.0      0     0 ?        S    13:56   0:00 [kworker/1:1]
postfix   39291  0.0  0.2  89812  4072 ?        S    14:03   0:00 pickup -l -t unix -u
root      39299  0.6  0.3 161832  6512 ?        Ds   14:07   0:00 sshd: root@pts/0,pts/1
root      39301  0.0  0.1 115544  2084 pts/0    Ss   14:07   0:00 -bash
root      39316  0.0  0.3 161512  6116 ?        Ss   14:07   0:00 sshd: root@notty
root      39317  0.0  0.1 115544  2016 pts/1    Ss   14:07   0:00 -bash
root      39333  0.0  0.1  72252  2856 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39340  0.0  0.1  72252  2692 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39347  0.0  0.1  72252  2692 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39354  0.0  0.1  72252  2692 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39361  0.0  0.1  72252  2692 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39368  0.0  0.1  72252  2692 ?        Ss   14:07   0:00 /usr/libexec/openssh/sftp-server
root      39431  0.1  0.1 162812  3012 pts/1    S+   14:07   0:00 top
root      40618  0.0  0.0      0     0 ?        S    14:08   0:00 [kworker/0:0]
root      41894  0.0  0.0 115344  1568 ?        Ss   14:09   0:00 bash -c export LANG="en_US";export LANGUAGE="en_US";export L
root      41903  0.0  0.0 108052   352 ?        S    14:09   0:00 sleep 1
root      41904  0.0  0.1 155448  1872 pts/0    R+   14:09   0:00 ps aux
root      48565  0.0  0.4 225492  8872 ?        Sl   08:29   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port
root      48572  0.0  0.6 709724 12760 ?        Sl   08:29   0:02 containerd-shim -namespace moby -workdir /var/lib/containerd
polkitd   48590  0.0 11.8 1578380 220488 ?      Ssl  08:29   0:16 mysqld --default-authentication-plugin=mysql_native_password
root      50817  0.0  0.3 218448  7100 ?        Sl   12:18   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port
root      50823  0.0  0.5 709724  9756 ?        Sl   12:18   0:00 containerd-shim -namespace moby -workdir /var/lib/containerd
root      50841  0.6 21.6 2065792 403320 ?      Ssl  12:18   0:44 java -jar /kubeblog.jar
root      61026  0.0  0.0      0     0 ?        R    12:29   0:00 [kworker/0:1]
root     102059  0.0  0.0      0     0 ?        S    09:15   0:01 [kworker/u256:3]
root     106829  0.0  0.0      0     0 ?        S    13:08   0:00 [kworker/0:3]
root     115370  0.0  0.0      0     0 ?        S    13:16   0:00 [kworker/u256:0]
[root@master ~]# unshare --fork --pid --mount-proc bash
[root@master ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1 115544  2072 pts/0    S    14:09   0:00 bash
root         12  0.0  0.0 155448  1860 pts/0    R+   14:09   0:00 ps aux

Docker的资源配额 CDroups

1.Docker 使用CGroups实现资源的配额管理。

C语言实现的,07年由谷歌工程师研发,08年并入Linux Kernel 2.6.24

2.CGroups 限制进程CPU的使用时间

Docker中的CPU,内存,网络的限制均通过cgroups实现

3.在宿主机上创建一个让CPU飙升到100%的进程
while : ; do : ; done &
[root@master ~]# while : ; do : ; done &
[1] 105599

使用TOP命令查看进程105599,CPU使用状况

top - 15:05:47 up  7:17,  3 users,  load average: 0.65, 0.23, 0.11
Tasks: 126 total,   4 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s): 50.0 us,  0.0 sy,  0.0 ni, 50.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1863032 total,   196420 free,   984232 used,   682380 buff/cache
KiB Swap:  4194300 total,  4192252 free,     2048 used.   709484 avail Mem 
Change delay from 3.0 to  
   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                  
105599 root      20   0  115784    624    160 R 100.0  0.0   0:51.20 -bash                                   

记录下PID105599

cd /sys/fs/cgroup/cpu
mkdir cgroups_test
echo 20000 > /sys/fs/cgroup/cpu/cgroups_test/cpu.cfs_quota_us 限制使用CPU20%,20000代表20毫秒
echo 105599 > /sys/fs/cgroup/cpu/cgroups_test/tasks 关联进程号

使用TOP命令查看进程105599,CPU使用状况,可以看到CPU已经下降到20%

清理该进程

kill -9 105599 
4.docker里如何加参数进行资源配额

docker run -it --cpus=“.5” nginx /bin/sh
进入容器查看是否有对应的cgroup设置
cd /sys/fs/cgroup/cpu
cat cpu.cfs_quota_us
配置显示200000,证明-cpu=".5"的参数已经生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维-阿鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值