K8S之容器资源限制(LimitRange)各属性值的小秘密

项目中用到了K8S容器资源限制这一块内容,rancher上没有对应API,因此调用K8S的接口,但实际使用过程中有些情况是保存不了的(前端点保存后有些情况下并未设置成自己需要设置的值,因此测试认为是Bug)

K8S之容器资源限制(LimitRange)各属性值的小秘密_第1张图片

调研之后,得出以下结论:

默认容器CPU限制有值时,默认容器CPU预留不能为空;
最大容器CPU使用有值时,默认容器CPU预留、默认容器CPU限制两者任一均不能为空;
默认容器内存限制有值时,默认容器内存预留不能为空;
最大容器内存使用有值时,默认容器内存预留、默认容器内存限制两者任一均不能为空。

即,在这些情况下设置对应值是设置不成功的。各位开发同仁切不要认为是自己的程序Bug!

附上所查看的源码:

func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.Container, pod *v1.Pod, uid *int64, username string) *runtimeapi.LinuxContainerConfig {
lc := &runtimeapi.LinuxContainerConfig{
    Resources:       &runtimeapi.LinuxContainerResources{},
    SecurityContext: m.determineEffectiveSecurityContext(pod, container, uid, username),
}

// set linux container resources
var cpuShares int64
cpuRequest := container.Resources.Requests.Cpu()
cpuLimit := container.Resources.Limits.Cpu()
memoryLimit := container.Resources.Limits.Memory().Value()
oomScoreAdj := int64(qos.GetContainerOOMScoreAdjust(pod, container,
    int64(m.machineInfo.MemoryCapacity)))
// If request is not specified, but limit is, we want request to default to limit.
// API server does this for new containers, but we repeat this logic in Kubelet
// for containers running on existing Kubernetes clusters.
if cpuRequest.IsZero() && !cpuLimit.IsZero() {
    cpuShares = milliCPUToShares(cpuLimit.MilliValue())
} else {
    // if cpuRequest.Amount is nil, then milliCPUToShares will return the minimal number
    // of CPU shares.
    cpuShares = milliCPUToShares(cpuRequest.MilliValue())
}
lc.Resources.CpuShares = cpuShares
if memoryLimit != 0 {
    lc.Resources.MemoryLimitInBytes = memoryLimit
}
// Set OOM score of the container based on qos policy. Processes in lower-priority pods should
// be killed first if the system runs out of memory.
lc.Resources.OomScoreAdj = oomScoreAdj

if m.cpuCFSQuota {
    // if cpuLimit.Amount is nil, then the appropriate default value is returned
    // to allow full usage of cpu resource.
    cpuQuota, cpuPeriod := milliCPUToQuota(cpuLimit.MilliValue())
    lc.Resources.CpuQuota = cpuQuota
    lc.Resources.CpuPeriod = cpuPeriod
}
return lc
}

 

你可能感兴趣的:(云计算)