LinuxKit

LinuxKit是Docker最新发布的一个用于为容器构建安全、便携、可移植操作系统的工具包。它根据用户编写的yaml(指定kernel和基于docker image的一些列服务)自动构建一个常见虚拟化平台或云平台的虚拟机镜像,并自动运行起来。主要特性包括

  • 增强安全性
  • 易用、可扩展
    • 所有服务均可定制,且用户服务和系统服务都是基于docker image
    • 构建过程基于docker
    • 基于Infrakit方便部署生成的镜像

安装

git clone https://github.com/linuxkit/linuxkit $GOPATH/src/github.com/linuxkit/linuxkit
make && make install

原理

编写yaml

LinuxKit需要编写一个yaml文件,来配置所需要的服务。可选的配置包括

  • kernel:指定内核镜像,镜像里面需要包含bzImage和kernel.tar
  • init:指定根系统的docker镜像( base init process Docker images),比如init、runc、containerd等。在运行时,这些都是只读的
  • onboot:启动过程中执行的系统服务,按顺序逐个运行
  • services:基于docker镜像的系统服务,这些服务在build的时候会由docker将其转换为OCI格式,以便后续runc来启动
  • files:指定额外的文件
  • outputs:构建完成后的输出

比如一个简单的nginx服务为

kernel:
image: "mobylinux/kernel:4.9.x"
cmdline: "console=ttyS0 console=tty0 page_poison=1"
init:
- linuxkit/init:42fe8cb1508b3afed39eb89821906e3cc7a70551
- mobylinux/runc:b0fb122e10dbb7e4e45115177a61a3f8d68c19a9
- linuxkit/containerd:60e2486a74c665ba4df57e561729aec20758daed
- mobylinux/ca-certificates:eabc5a6e59f05aa91529d80e9a595b85b046f935
onboot:
- name: sysctl
image: "mobylinux/sysctl:2cf2f9d5b4d314ba1bfc22b2fe931924af666d8c"
net: host
pid: host
ipc: host
capabilities:
- CAP_SYS_ADMIN
readonly: true
- name: binfmt
image: "linuxkit/binfmt:8881283ac627be1542811bd25c85e7782aebc692"
binds:
- /proc/sys/fs/binfmt_misc:/binfmt_misc
readonly: true
- name: dhcpcd
image: "linuxkit/dhcpcd:48e249ebef6a521eed886b3bce032db69fbb4afa"
binds:
- /var:/var
- /tmp/etc:/etc
capabilities:
- CAP_NET_ADMIN
- CAP_NET_BIND_SERVICE
- CAP_NET_RAW
net: host
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
services:
- name: rngd
image: "mobylinux/rngd:3dad6dd43270fa632ac031e99d1947f20b22eec9"
capabilities:
- CAP_SYS_ADMIN
oomScoreAdj: -800
readonly: true
- name: nginx
image: "nginx:alpine"
capabilities:
- CAP_NET_BIND_SERVICE
- CAP_CHOWN
- CAP_SETUID
- CAP_SETGID
- CAP_DAC_OVERRIDE
net: host
files:
- path: etc/docker/daemon.json
contents: '{"debug": true}'
trust:
image:
- mobylinux/kernel
outputs:
- format: kernel+initrd
- format: iso-bios
- format: iso-efi

构建

$ moby build linuxkit.yml
Extract kernel image: mobylinux/kernel:4.9.x
Add init containers:
Process init image: linuxkit/init:42fe8cb1508b3afed39eb89821906e3cc7a70551
Process init image: mobylinux/runc:b0fb122e10dbb7e4e45115177a61a3f8d68c19a9
Process init image: linuxkit/containerd:60e2486a74c665ba4df57e561729aec20758daed
Process init image: mobylinux/ca-certificates:eabc5a6e59f05aa91529d80e9a595b85b046f935
Add onboot containers:
Create OCI config for mobylinux/sysctl:2cf2f9d5b4d314ba1bfc22b2fe931924af666d8c
Create OCI config for linuxkit/binfmt:8881283ac627be1542811bd25c85e7782aebc692
Create OCI config for linuxkit/dhcpcd:48e249ebef6a521eed886b3bce032db69fbb4afa
Add service containers:
Create OCI config for mobylinux/rngd:3dad6dd43270fa632ac031e99d1947f20b22eec9
Create OCI config for nginx:alpine
Add files:
etc/docker/daemon.json
Create outputs:
linuxkit-bzImage linuxkit-initrd.img linuxkit-cmdline
linuxkit.iso
linuxkit-efi.iso

编译完成后的文件大约58M。

$ ls -lh linuxkit*
-rw-r--r-- 1 root root 6.7M Apr 19 02:39 linuxkit-bzImage
-rw-r--r-- 1 root root 40 Apr 19 02:39 linuxkit-cmdline
-rw-r--r-- 1 root root 58M Apr 19 02:40 linuxkit-efi.iso
-rw-r--r-- 1 root root 50M Apr 19 02:39 linuxkit-initrd.img
-rw-r--r-- 1 root root 57M Apr 19 02:39 linuxkit.iso
-rw-r--r-- 1 root root 1.6K Apr 19 02:19 linuxkit.yml

运行

LinuxKit目前支持在gcp/hyperkit/qemu/vmware/packet/Hyper-V等多个平台上运行。比如用qemu的方式运行:

$ moby run qemu linuxkit
....

默认运行后会自动进入VM的console:

/ # pstree
init-+-containerd---containerd-shim---nginx---nginx
|-containers---ctr
|-sh---pstree
`-sh

参考文档

Feisky wechat
微信公众号订阅