Runc 与 Cgroups

Runc 可以算是启动创建容器的最后一步,其中设置 Cgroups,隔离 namespaces,配置网络,挂载相应的卷 等一系列操作
在这里插入图片描述本文将主要讲 runc 是如何去操作系统中的 Cgroups,实现对资源的限制和管理的

Runc 支持三种方式来限制管理资源,分别是使用 Cgroups V1, Cgroups V2, Systemd
本文将主要讲解 Cgroups V1, 关于 Cgroups V1 相关的基本概念可以参考
Linux Cgroups V1 介绍与使用

Cgroup Manager

Cgroup Manager 是 runc 实现对系统的 cgroup 操作抽象
实现了设置资源的配置,PID 加入到指定控制组控制组的销毁,控制组进程的暂停恢复,获取配置,获取统计信息等操作

type Manager interface{
   
	Apply(pid int) error			// 将 pid 加入到控制组中
	Set(container *config.Config) error	// 设置控制组的配置
	GetCgroups()(*configs.Cgroup, error)	// 获取控制组的配置
	
	GetPids()([]int, error)			// 返回控制组(cgroup) 中的 PID, 不包括子控制组
	GetAllPids()([]int, error)		// 返回控制组和它的子控制组的所有 PID
	GetStats() (*Stats, error)		// 获取控制组统计信息
	
	Destroy() error					// 删除控制组
	
	GetPaths()map[string]string		// 获取保存 cgroup 状态文件的路径
	GetUnifiedPath()(string, error)	// 如果容器组没有挂载任何控制器(子系统), 则返回值同 GetPaths,否则,返回 error

	Freeze(state configs.FreezerState) error	// 任务的暂停和恢复
}

GetStats 方法会返回控制组中的统计信息,记录了 CPU, Memroy, Blkio 之类的一些状态

type Stats struct {
   
    CpuStats    CpuStats    `json:"cpu_stats,omitempty"`
    MemoryStats MemoryStats `json:"memory_stats,omitempty"`
    PidsStats   PidsStats   `json:"pids_stats,omitempty"`
    BlkioStats  BlkioStats  `json:"blkio_stats,omitempty"`
    // the map is in the format "size of hugepage: stats of the hugepage"
    HugetlbStats map[string]HugetlbStats `json:"hugetlb_stats,omitempty"`
}

Set 方法在设置控制组 资源的时候需要传递 config.Config实际上该方法只会使用 Config.Cgroup.Resources 中的数据

// Config 定义容器的配置
type Config struct {
   
	// ....
	Cgroups *Cgroup `json: "cgroup"`
	// ....
}

type Cgroup struct {
   
        Path string `json:"path"`	// cgroup 路径,相对于`层级`的地址
        ScopePrefix string `json:"scope_prefix"`	// ScopePrefix describes prefix for the scope name
        Paths map[string]string		// 所属各个控制器的 cgroup 路径,需要注意该路径是绝对路径
        
        // 包含了各个子系统资源的设置
        *Resources
}
type Resources struct {
   
    AllowAllDevices *bool `json:"allow_all_devices,omitempty"`
    AllowedDevices []*Device `json:"allowed_devices,omitempty"`
    DeniedDevices []*Device `json:"denied_devices,omitempty"`
    Devices []*Device `json:"devices"`
    Memory int64 `json:"memory"`
    MemoryReservation int64 `json:"memory_reservation"`
    MemorySwap int64 `json:"memory_swap"`
    KernelMemory int64 `json:"kernel_memory"`
    KernelMemoryTCP int64 `json:"kernel_memory_tcp"`
    CpuShares uint64 `json:"cpu_shares"`
    CpuQuota int64 `json:"cpu_quota"`
    CpuPeriod uint64 `json:"cpu_period"`
    CpuRtRuntime int64 `json:"cpu_rt_quota"`
    CpuRtPeriod uint64 `json:"cpu_rt_period"`
    CpusetCpus string `json:"cpuset_cpus"`
    CpusetMems string `json:"cpuset_mems"`
    PidsLimit int64 `json:"pids_limit"`
    BlkioWeight uint16 `json:"blkio_weight"`
    BlkioLeafWeight uint16 `json:"blkio_leaf_weight"`
    BlkioWeightDevice []*WeightDevice `json:"blkio_weight_device"`
    BlkioTh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值