docker 简介

docker 实践 #

一、Namespace 隔离: #

Network Namespace :

  1. 独立的附属网络设备, lo, veth 等虚拟/物理设备
  2. 独立协议栈,Ip 地址和路由表
  3. iptables 规则,ipvs 等
  4. 网桥技术实现等 UserNamespace: 用户 uid 映射问题

MountNamespace

Pid NameSapce: 确保应用pid=1,这也间接导致,容器没有 systemd 进程,应用进程被kill之后,整个容器停止工作

IPC namespace

MountNamespace #

1
2
3
4
5
# new a bash process with an unshared file system
sudo unshare --mount --fork /bin/bash

# mount a disk to path /tmp/tmpfs
mount -t tmpfs -o size=20m tmpfs /tmp/tmpfs

虚拟 namespace 下的挂载情况

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
df -h

# new
文件系统类型       容量  已用  可用 已用% 挂载点
/dev/vda1        40G   18G   21G   47% /
/dev/loop1       62M   62M     0  100% /var/.../1169
tmpfs            10M     0   10M    0% /tmp/tmpfs

# old
文件系统类型       容量  已用  可用 已用% 挂载点
/dev/vda1        40G   18G   21G   47% /
/dev/loop1       62M   62M     0  100% /var/.../1169

完整的 namespace 隔离 #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
sudo unshare --mount --fork /bin/bash
sudo unshare --pid --fork --mount-proc /bin/bash
sudo unshare --uts --fork /bin/bash
sudo unshare --ipc --fork /bin/bash
sudo unshare --user -r /bin/bash
sudo unshare --net --fork /bin/bash

# 隔离出完整独立的 namespace
sudo unshare \
    --mount \
    --uts \
    --ipc \
    --net \
    --pid --mount-proc \
    --user -r  \
    --fork /bin/bash

此时我们观察前后命名空间 ID 的区别

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
ls -l /proc/$$/ns/

# new
lrwxrwxrwx 1 root root 0 11月 28 20:40 ipc -> ipc:[4026532228]
lrwxrwxrwx 1 root root 0 11月 28 20:40 mnt -> mnt:[4026532226]
lrwxrwxrwx 1 root root 0 11月 28 20:40 net -> net:[4026532231]
lrwxrwxrwx 1 root root 0 11月 28 20:40 pid -> pid:[4026532229]
lrwxrwxrwx 1 root root 0 11月 28 20:40 user -> user:[4026532225]
lrwxrwxrwx 1 root root 0 11月 28 20:40 uts -> uts:[4026532227]

# old
lrwxrwxrwx 1 root root 0 11月 28 20:32 ipc -> ipc:[4026531839]
lrwxrwxrwx 1 root root 0 11月 28 20:32 mnt -> mnt:[4026531840]
lrwxrwxrwx 1 root root 0 11月 28 20:32 net -> net:[4026531956]
lrwxrwxrwx 1 root root 0 11月 28 20:32 pid -> pid:[4026531836]
lrwxrwxrwx 1 root root 0 11月 28 20:32 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 11月 28 20:32 uts -> uts:[4026531838]

此后,还可以观察进程PID、网络端口情况、进程IPC通信,用户标识。

二、Control Groups隔离: #

cgroups

三、镜像简述 #

coreutils #

coreutils[GNU Core Utilities]: 工具套件

bash: (help: type cd echo) #

  1. 提供常规命令 help: type cd echo …
  2. sh 命令语言解释器

busybox #

Busybox is a package that replaces a dozen standard packages, but it is not by itself a complete bootable system.

alpine #

  1. busybox (ls rm mkdir);

  2. 包管理工具:apk /etc/apk/repositories

centos #

  1. bash: (help: type cd echo)

    1. 提供常规命令 help: type cd echo …
    2. sh 命令语言解释器
  2. coreutils[GNU Core Utilities]: 工具套件 (cat rm mkdir)几件套, 工具通常在存放在 /bin/*

  3. 包管理工具:yum

  4. i18n相关文件,9.6M。// 国际化, alpine 优化,干掉了

杂项 #

需要吧 namespace 打开,这样 unshare 工具才能正常使用 echo 100 > /proc/sys/user/max_user_namespaces # 临时开一点,重启就没了 grubby –args=“user_namespace.enable=1” –update-kernel="$(grubby –default-kernel)" # 永久的

  1. 一文搞懂容器运行时
  2. Docker技术原理

st=>start: Start|past:>http://www.google.com[blank]
e=>end: End:>http://www.google.com
op1=>operation: get_hotel_ids|past
op2=>operation: get_proxy|current
sub1=>subroutine: get_proxy|current
op3=>operation: save_comment|current
op4=>operation: set_sentiment|current
op5=>operation: set_record|current

cond1=>condition: ids_remain空?
cond2=>condition: proxy_list空?
cond3=>condition: ids_got空?
cond4=>condition: 爬取成功??
cond5=>condition: ids_remain空?

io1=>inputoutput: ids-remain
io2=>inputoutput: proxy_list
io3=>inputoutput: ids-got

st->op1(right)->io1->cond1
cond1(yes)->sub1->io2->cond2
cond2(no)->op3
cond2(yes)->sub1
cond1(no)->op3->cond4
cond4(yes)->io3->cond3
cond4(no)->io1
cond3(no)->op4
cond3(yes, right)->cond5
cond5(yes)->op5
cond5(no)->cond3
op5->e