容器的一生

135 阅读1分钟

容器基于两方面构成:

  1. Namespaces
  2. cgroups

image.png

上图中,父进程可以看到一个活跃的shell会话。 子进程可以看作在该会话中执行的任何命令。

fork 建立一个子进程,同时返回子进程pid。

1. 关于ns

ns 可以用于隔离不同类型的资源。 在linux中大概有7种ns

  • Network namespace
  • Mount
  • UTS or Hostname namespace
  • Process ID or PID namespace
  • Inter process communication or IPC namespace
  • cgroup namespace
  • User namespace

通过unshare命令你可以创建以上ns


unshare -n 
# 创建network ns

2. 关于cgroups

用于控制资源可以使用到的资源的配额, 比如 常见的cpu,内存等

# ls /sys/fs/cgroup/ -lh
total 0
dr-xr-xr-x 5 root root  0 Feb 17 01:05 blkio
lrwxrwxrwx 1 root root 11 Feb 17 01:05 cpu -> cpu,cpuacct
lrwxrwxrwx 1 root root 11 Feb 17 01:05 cpuacct -> cpu,cpuacct
dr-xr-xr-x 5 root root  0 Feb 17 01:05 cpu,cpuacct
dr-xr-xr-x 2 root root  0 Feb 17 01:05 cpuset
dr-xr-xr-x 5 root root  0 Feb 17 01:05 devices
dr-xr-xr-x 2 root root  0 Feb 17 01:05 freezer
dr-xr-xr-x 2 root root  0 Feb 17 01:05 hugetlb
dr-xr-xr-x 9 root root  0 Feb 20 00:24 memory
lrwxrwxrwx 1 root root 16 Feb 17 01:05 net_cls -> net_cls,net_prio
dr-xr-xr-x 2 root root  0 Feb 17 01:05 net_cls,net_prio
lrwxrwxrwx 1 root root 16 Feb 17 01:05 net_prio -> net_cls,net_prio
dr-xr-xr-x 2 root root  0 Feb 17 01:05 perf_event
dr-xr-xr-x 5 root root  0 Feb 17 01:05 pids
dr-xr-xr-x 2 root root  0 Feb 17 01:05 rdma
dr-xr-xr-x 5 root root  0 Feb 17 01:05 systemd
dr-xr-xr-x 5 root root  0 Feb 17 01:06 unified

参考: indradhanush.github.io/blog/life-o…