使用容器运行时runc运行busybox

本文用以记录使用runc运行busybox,并使用busybox的基本功能

创建目录,获取rootfs
mkdir my_container
cd my_container
mkdir rootfs
# 获取 busybox 的 rootfs
docker export $(docker create busybox) | tar -C rootfs -xvf -
获取目录结构
root@ubuntu-master:~/csdn-nginx/my_container# tree -d
.
└── rootfs
    ├── bin
    ├── dev
    │   ├── pts
    │   └── shm
    ├── etc
    │   └── network
    │       ├── if-down.d
    │       ├── if-post-down.d
    │       ├── if-pre-up.d
    │       └── if-up.d
    ├── home
    ├── lib
    ├── lib64 -> lib
    ├── proc
    ├── root
    ├── sys
    ├── tmp
    ├── usr
    │   ├── bin
    │   └── sbin
    └── var
        ├── spool
        │   └── mail
        └── www

25 directories
生成 config.json
root@ubuntu-master:~/csdn-nginx/my_container# runc spec
root@ubuntu-master:~/csdn-nginx/my_container# ls
config.json  rootfs
运行容器
root@ubuntu-master:~/csdn-nginx/my_container# runc run container-name
/ #
重新打开一个terminal session,查看容器id
root@ubuntu-master:~# runc list
ID               PID         STATUS      BUNDLE                          CREATED                          OWNER
container-name   526         running     /root/csdn-nginx/my_container   2023-12-29T03:27:46.396414833Z   root
root@ubuntu-master:~# runc ps container-name
UID          PID    PPID  C STIME TTY          TIME CMD
root         526     516  0 11:27 pts/0    00:00:00 sh
使用buzy box 的部分功能(容器内执行)
/ # bin/lsof -i
1	/bin/sh	0	/dev/pts/0
1	/bin/sh	1	/dev/pts/0
1	/bin/sh	2	/dev/pts/0
1	/bin/sh	10	/dev/tty
/ # bin/iostat -k
Linux 6.5.13-orbstack-00116-gb64875927ef8 (runc) 	12/29/23 	_aarch64_	(8 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.65    0.00    0.45    0.03    0.00   98.87

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
vda               2.15        93.78         0.00      94168          0
vdb               9.32       570.01       346.28     572388     347724
vdb1              9.25       569.97       346.28     572348     347724
vdc               0.01         0.04         0.00         44          0
vdc2              0.00         0.00         0.00          4          0
zram0             0.00         0.01         0.00          8          4

容器内使用top, 宿主机通过 runc list, runc ps [container id] 查看容器及容器内进程
容器内:

Mem: 1952784K used, 3424076K free, 10188K shrd, 388K buff, 1056004K cached
CPU:  0.0% usr  0.0% sys  0.0% nic  100% idle  0.0% io  0.0% irq  0.0% sirq
Load average: 2.39 2.19 1.57 2/557 15
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
    1     0 root     S     3984  0.0   6  0.0 sh
   15     1 root     R     3984  0.0   5  0.0 top

容器外:

root@ubuntu-master:~# runc ps container-name
UID          PID    PPID  C STIME TTY          TIME CMD
root         526     516  0 11:27 pts/0    00:00:00 sh
root         574     526  0 11:38 pts/0    00:00:00 top

可以观察到CMD top在容器内外的pid
至此,我们就通过runc成功运行了busybox

你可能感兴趣的:(k8s,容器,linux,容器,linux,docker)