Open Container Initiative(OCI)目前有2个标准:runtime-spec以及image-spec。前者规定了如何运行解压过的filesystem bundle。OCI规定了如何下载OCI镜像并解压到OCI filesystem bundle,这样OCI runtime就能够运行OCI bundle了。OCI(当前)至关于规定了容器的images和runtime的协议,只要实现了OCI的容器就能够实现其兼容性和可移植性。implements中列出了部分OCI标准的实现。本文不讨论windows下的实现,具体参见Open Container Initiative Runtime Specificationhtml
system bundle是个目录,用于给runtime提供启动容器必备的配置文件和文件系统。标准的容器bundle包含如下内容:node
config.json:该文件包含了容器运行的配置信息,该文件必须存在bundle的根目录,且名字必须为config.json
容器的根目录,能够由config.json中的root.path指定
下面使用runc来运行一个容器,runc是根据OCI标准生成的一个cli工具。前面两个命令用于提取filesystem,最后一个用于生成config.json,二者组织在一块儿就是一个filesystem bundlelinux
# mkdir rootfs # docker export $(docker create busybox) | tar -C rootfs -xvf -
# runc spec
使用runc来运行这个bundle,可使用state查看该容器的状态git
# runc run busybox # runc state busybox { "ociVersion": "1.0.0", "id": "busybox", "pid": 41732, "status": "running", "bundle": "/home/test", "rootfs": "/home/test/rootfs", "created": "2018-12-25T14:41:58.82202891Z", "owner": ""
OCI runtime包含runtime,runtime-linux,config,config-linuxgithub
ociVersion:建立容器时的OCI版本
因为runc实现了OCI runtime,使用runc state查看上述busybox能够获得state相关的信息docker
{ "ociVersion": "1.0.0", "id": "busybox", "pid": 41732, "status": "running", "bundle": "/home/test", "rootfs": "/home/test/rootfs", "created": "2018-12-25T14:41:58.82202891Z", "owner": "" }
state <container-id>,参见上述state描述
create <container-id> <path-to-bundle>,runtime应该提供检测id惟一性的功能。该操做中会用到config.json除process以外的配置属性(由于process实在start阶段用到的)。实现中可能会与本规范不一致,如在create操做以前实现了pre-create
start <container-id>,执行config.json的process中定义的程序,若是process没有设定,则返回错误
kill <container-id> <signal>,向一个非running状态的容器发送的信号会被忽略。此操做用于向容器进程发送信号
delete <container-id>,尝试删除一个非stopped的容器会返回错误。容器删除后其id可能会被后续的容器使用
# runc delete busybox
cannot delete container busybox that is not stopped: running
// Linux is platform-specific configuration for Linux based containers. Linux *Linux `json:"linux,omitempty" platform:"linux"` // Solaris is platform-specific configuration for Solaris based containers. Solaris *Solaris `json:"solaris,omitempty" platform:"solaris"` // Windows is platform-specific configuration for Windows based containers. Windows *Windows `json:"windows,omitempty" platform:"windows"` // VM specifies configuration for virtual-machine-based containers. VM *VM `json:"vm,omitempty" platform:"vm"`
consoleSize:指定terminal的长宽规格,width和height
根据平台不一样支持以下配置json
POSIX process 支持设置POSIX和Linux平台windows
Linux process:数组
apparmorProfile:指定进程的apparmor文件
capabilities:指定进程的capabilities
noNewPrivileges:设置为true后能够防止进程获取额外的权限(如使得suid和文件capabilities失效),该标记位在内核4.10版本以后能够在/proc/$pid/status中查看NoNewPrivs的设置值。更多参见no_new_privs
oomScoreAdj
:给进程设置oom_score_adj值,进程的oom涉及如下3个文件,oom_adj和oom_score_adj功能相似,oom_adj主要用于兼容老版本,oomScoreAdj的功能就是设置/proc/$PID/oom_score_adj中的值(范围-1000~1000),系统经过该值和oom_score来决定kill进程的优先级。oom_score为只读文件,oom经过对系统全部进程的oom_score进行排序,值越大,越可能在内存不足时被kill掉。(参见linux oom机制分析和oom介绍)/proc/$PID/oom_adj /proc/$PID/oom_score /proc/$PID/oom_score_adj
能够经过以下命令查看系统全部进程的oom_score网络
ps -eo pid,comm,pmem --sort -rss | awk '{"cat /proc/"$1"/oom_score" | getline oom; print $0"\t"oom}'
selinuxLabel
:设置进程的SELinux 标签,即MAC值Path Type /proc proc /sys sysfs /dev/pts devpts /dev/shm tmpfs
可使用resources字段来配置cgroup,注意:只有在须要更新cgroup的时候才配置该字段内容
"cgroupsPath": "/myRuntime/myContainer", "resources": { "memory": { "limit": 100000, "reservation": 200000 }, "devices": [ { "allow": false, "access": "rwm" } ] }
Device whitelist:用于配置设备白名单
Memory:具体能够参见cgroup memory
CPU:具体能够参见cgroup CPU
Block IO:
Huge page limits:
pageSize
:大页大小Network:
name:网卡名称
priority:网卡优先级
PIDs:
limit:cgroup限制的pid的数目
RDMA
Sysctl:容许在容器运行过程当中修改内核参数
Seccomp:在linux内核中为应用提供了一种沙盒机制。更多参见seccomp
seccomp
defaultAction:seccomp的默认动做,容许值类型为syscalls[].action
architectures:系统调用的平台,以下
SCMP_ARCH_X86
SCMP_ARCH_X86_64
SCMP_ARCH_X32
SCMP_ARCH_ARM
SCMP_ARCH_AARCH64
SCMP_ARCH_MIPS
SCMP_ARCH_MIPS64
SCMP_ARCH_MIPS64N32
SCMP_ARCH_MIPSEL
SCMP_ARCH_MIPSEL64
SCMP_ARCH_MIPSEL64N32
SCMP_ARCH_PPC
SCMP_ARCH_PPC64
SCMP_ARCH_PPC64LE
SCMP_ARCH_S390
SCMP_ARCH_S390X
SCMP_ARCH_PARISC
SCMP_ARCH_PARISC6
syscalls:匹配seccomp的系统调用,该属性可选
name:系统调用的名称,至少有一个
action:seccomp的动做规则。libseccomp v2.3.2中以下:
SCMP_ACT_KILL
SCMP_ACT_TRAP
SCMP_ACT_ERRNO
SCMP_ACT_TRACE
SCMP_ACT_ALLOW
args:
index (uint, REQUIRED) - 系统调用的index
value (uint64, REQUIRED) - 系统调用参数的值
valueTwo (uint64, OPTIONAL) - 系统调用参数的值
op (string, REQUIRED) - 系统调用参数的动做。 libseccomp v2.3.2以下
SCMP_CMP_NE
SCMP_CMP_LT
SCMP_CMP_LE
SCMP_CMP_EQ
SCMP_CMP_GE
SCMP_CMP_GT
SCMP_CMP_MASKED_EQ
Rootfs Mount Propagation
rootfsPropagation:设置rootfs的mount Propagation类型,slave,private或shared
Masked Paths
maskedPaths:容器没法读取该设置的路径
"maskedPaths": [
"/proc/kcore" ]
Readonly Paths
readonlyPaths:容器只读该设置的路径
TIPS:
参考:
https://cizixs.com/2017/11/05/oci-and-runc/
https://github.com/opencontainers/runtime-spec/blob/master/config.md
https://github.com/opencontainers/runtime-spec/blob/master/specs-go/config.go