【kubernetes/k8s概念】pod 资源 request 与 limit

Managing Compute Resources for Containers

https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/

requests

  • requests用于schedule阶段,在调度pod保证所有pod的requests总和小于node能提供的计算能力
  • requests.cpu被转成docker的--cpu-shares参数,与cgroup cpu.shares功能相同
    • 设置容器的cpu的相对权重
    • 该参数在CPU资源不足时生效,根据容器requests.cpu的比例来分配cpu资源
    • CPU资源充足时,requests.cpu不会限制container占用的最大值,container可以独占CPU
  • requests.memory没有对应的docker参数,作为k8s调度依据
  • 使用requests来设置各容器需要的最小资源

limits

  • limits限制运行时容器占用的资源
  • limits.cpu会被转换成docker的–cpu-quota参数。与cgroup cpu.cfs_quota_us功能相同
    • 限制容器的最大CPU使用率。
    • cpu.cfs_quota_us参数与cpu.cfs_period_us结合使用,后者设置时间周期
    • k8s将docker的–cpu-period参数设置100毫秒。对应着cgroup的cpu.cfs_period_us
    • limits.cpu的单位使用m,千分之一核
  • limits.memory会被转换成docker的–memory参数。用来限制容器使用的最大内存
  • 当容器申请内存超过limits时会被终止

如下代码,转换为docker资源限制

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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值