0%

K8s学习笔记——container之于进程

学习极客时间上的《深入剖析Kubernetes》

秉持眼过千遍不如手过一遍的原则.

对应章节:05 | 白话容器基础(一):从进程说开去

操作

start一个container

1
$ docker run -it -d busybox

查看进程

1
2
3
4
5
$ ps -aux
...
root 2817 0.0 0.2 107700 2296 ? Sl 05:42 0:00 containerd-shim -namespace moby -workdir /var/lib/containerd/io.containerd.run
root 2851 0.0 0.0 1308 4 pts/0 Ss+ 05:42 0:00 sh
...

查看进程树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ pstree  -g
systemd(1)─┬─VGAuthService(562)
├─accounts-daemon(866)─┬─{accounts-daemon}(866)
│ └─{accounts-daemon}(866)
├─atd(928)
├─containerd(1055)─┬─containerd-shim(2817)─┬─sh(2851)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ ├─{containerd-shim}(2817)
│ │ └─{containerd-shim}(2817)
....

查看容器内进程

1
2
3
4
$ ps
PID USER TIME COMMAND
1 root 0:00 sh
6 root 0:00 ps

分别查看两个进程的namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/proc/2817/ns# ls -l
total 0
lrwxrwxrwx 1 root root 0 Jun 11 05:43 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 ipc -> 'ipc:[4026531839]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 mnt -> 'mnt:[4026531840]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 net -> 'net:[4026531993]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 pid -> 'pid:[4026531836]'
lrwxrwxrwx 1 root root 0 Jun 11 06:16 pid_for_children -> 'pid:[4026531836]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 uts -> 'uts:[4026531838]
/proc/2851/ns# ls -l
total 0
lrwxrwxrwx 1 root root 0 Jun 11 05:43 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 ipc -> 'ipc:[4026532571]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 mnt -> 'mnt:[4026532569]'
lrwxrwxrwx 1 root root 0 Jun 11 05:42 net -> 'net:[4026532574]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 pid -> 'pid:[4026532572]'
lrwxrwxrwx 1 root root 0 Jun 11 06:16 pid_for_children -> 'pid:[4026532572]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Jun 11 05:43 uts -> 'uts:[4026532570]'

总结

  1. 启动一个docker容器后,会看到启动了一个2817的进程,这个进程是1055的子进程
  2. 而因为busybox容器启动后,启动了sh,其实际上是2817的子进程2851
  3. 而在容器中,能看到1号进程是sh
  4. 通过/proc下可以看到,进程2817和进程2851的ns下,cgroup是都是4026531835
  5. 而很明显,每个container都会创建ipc, mnt, net, pid, pid_for_children, user, uts这些namespace