本文共 8679 字,大约阅读时间需要 28 分钟。
Open Container Initiative(OCI)目前有2个标准:以及。前者规定了如何运行解压过的。OCI规定了如何下载OCI镜像并解压到OCI filesystem bundle,这样OCI runtime就可以运行OCI bundle了。OCI(当前)相当于规定了容器的images和runtime的协议,只要实现了OCI的容器就可以实现其兼容性和可移植性。中列出了部分OCI标准的实现。本文不讨论windows下的实现,具体参见
system bundle是个目录,用于给runtime提供启动容器必备的配置文件和文件系统。标准的容器bundle包含以下内容:
config.json:该文件包含了容器运行的配置信息,该文件必须存在bundle的根目录,且名字必须为config.json容器的根目录,可以由config.json中的root.path指定
下面使用runc来运行一个容器,runc是根据OCI标准生成的一个cli工具。前面两个命令用于提取filesystem,最后一个用于生成config.json,两者组织在一起就是一个filesystem bundle
# mkdir rootfs# docker export $(docker create busybox) | tar -C rootfs -xvf - # runc spec
使用runc来运行这个bundle,可以使用state查看该容器的状态
# 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包含,,,
ociVersion:创建容器时的OCI版本
由于runc实现了OCI runtime,使用runc state查看上述busybox可以得到state相关的信息
{ "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 busyboxcannot 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
根据平台不同支持如下配置
POSIX process 支持设置POSIX和Linux平台
Linux process:
apparmorProfile:指定进程的apparmor文件
capabilities:指定进程的capabilities
noNewPrivileges:设置为true后可以防止进程获取额外的权限(如使得suid和文件capabilities失效),该标记位在内核4.10版本之后可以在/proc/$pid/status中查看NoNewPrivs的设置值。更多参见
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掉。(参见和)/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:具体可以参见
CPU:具体可以参见
Block IO:
Huge page limits:
pageSize
:大页大小Network:
name:网卡名称
priority:网卡优先级PIDs:
limit:cgroup限制的pid的数目
RDMA
Sysctl:允许在容器运行过程中修改内核参数
Seccomp:在linux内核中为应用提供了一种沙盒机制。更多参见
seccomp
defaultAction:seccomp的默认动作,允许值类型为syscalls[].action
architectures:系统调用的平台,如下
SCMP_ARCH_X86SCMP_ARCH_X86_64SCMP_ARCH_X32SCMP_ARCH_ARMSCMP_ARCH_AARCH64SCMP_ARCH_MIPSSCMP_ARCH_MIPS64SCMP_ARCH_MIPS64N32SCMP_ARCH_MIPSELSCMP_ARCH_MIPSEL64SCMP_ARCH_MIPSEL64N32SCMP_ARCH_PPCSCMP_ARCH_PPC64SCMP_ARCH_PPC64LESCMP_ARCH_S390SCMP_ARCH_S390XSCMP_ARCH_PARISCSCMP_ARCH_PARISC6
syscalls:匹配seccomp的系统调用,该属性可选
name:系统调用的名称,至少有一个
action:seccomp的动作规则。libseccomp v2.3.2中如下:
SCMP_ACT_KILLSCMP_ACT_TRAPSCMP_ACT_ERRNOSCMP_ACT_TRACESCMP_ACT_ALLOW
args:
index (uint, REQUIRED) - 系统调用的index
value (uint64, REQUIRED) - 系统调用参数的值valueTwo (uint64, OPTIONAL) - 系统调用参数的值op (string, REQUIRED) - 系统调用参数的动作。 libseccomp v2.3.2如下SCMP_CMP_NESCMP_CMP_LTSCMP_CMP_LESCMP_CMP_EQSCMP_CMP_GESCMP_CMP_GTSCMP_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
转载地址:http://lxakz.baihongyu.com/