kubenetes oom pod

  • note
 1.request resource == 0 and limit resouoce==0 ,pod will be bestefford status
 2.request resource < limit resource ,pod will be burstable status
 2.request resource == limit resource ,pod will be Guaranteed status
 4.all pods is same oomScoreAdjust in node,oom will follow order to kill process
        bestefford  pod -> burstable pod  ->guaranteed pod
  • K8s.io/kubernetes/pkg/kubelet/qos/policy.go

    /\*
    Copyright 2015 The Kubernetes Authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    \*/

    package qos

    import (
     "k8s.io/api/core/v1"
     v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
    )

    const (
     // PodInfraOOMAdj is very docker specific. For arbitrary runtime, it may not make
     // sense to set sandbox level oom score, e.g. a sandbox could only be a namespace
     // without a process.
     // TODO: Handle infra container oom score adj in a runtime agnostic way.
     PodInfraOOMAdj int = -998
     KubeletOOMScoreAdj int = -999
     DockerOOMScoreAdj int = -999
     KubeProxyOOMScoreAdj int = -999
     guaranteedOOMScoreAdj int = -998
     besteffortOOMScoreAdj int = 1000
    )

    // GetContainerOOMAdjust returns the amount by which the OOM score of all processes in the
    // container should be adjusted.
    // The OOM score of a process is the percentage of memory it consumes
    // multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000
    // and 1000\. Containers with higher OOM scores are killed if the system runs out of memory.
    // See https://lwn.net/Articles/391222/ for more information.
    func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int {
     switch v1qos.GetPodQOS(pod) {
     case v1.PodQOSGuaranteed:
          // Guaranteed containers should be the last to get killed.
     return guaranteedOOMScoreAdj
     case v1.PodQOSBestEffort:
          return besteffortOOMScoreAdj
     }

     // Burstable containers are a middle tier, between Guaranteed and Best-Effort. Ideally,
     // we want to protect Burstable containers that consume less memory than requested.
     // The formula below is a heuristic. A container requesting for 10% of a system's
     // memory will have an OOM score adjust of 900\. If a process in container Y
     // uses over 10% of memory, its OOM score will be 1000\. The idea is that containers
     // which use more than their request will have an OOM score of 1000 and will be prime
     // targets for OOM kills.
     // Note that this is a heuristic, it won't work if a container has many small processes.
     memoryRequest := container.Resources.Requests.Memory().Value()
     oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity
       // A guaranteed pod using 100% of memory can have an OOM score of 10\. Ensure
     // that burstable pods have a higher OOM score adjustment.
     if int(oomScoreAdjust) < (1000 + guaranteedOOMScoreAdj) {
     return (1000 + guaranteedOOMScoreAdj)
     }
     // Give burstable pods a higher chance of survival over besteffort pods.
     if int(oomScoreAdjust) == besteffortOOMScoreAdj {
     return int(oomScoreAdjust - 1)
     }
     return int(oomScoreAdjust)
    }

  • K8s.io/kubernetes/pkg/apis/core/v1/helper/qos/qos.go
/*
Copyright 2015 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package qos

import (
    "k8s.io/api/core/v1"
    "k8s.io/apimachinery/pkg/api/resource"
    "k8s.io/apimachinery/pkg/util/sets"
    "k8s.io/kubernetes/pkg/apis/core"
)

var supportedQoSComputeResources = sets.NewString(string(core.ResourceCPU), string(core.ResourceMemory))

// QOSList is a set of (resource name, QoS class) pairs.
type QOSList map[v1.ResourceName]v1.PodQOSClass

func isSupportedQoSComputeResource(name v1.ResourceName) bool {
    return supportedQoSComputeResources.Has(string(name))
}

// GetPodQOS returns the QoS class of a pod.
// A pod is besteffort if none of its containers have specified any requests or limits.
// A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
// A pod is burstable if limits and requests do not match across all containers.
func GetPodQOS(pod *v1.Pod) v1.PodQOSClass {
    requests := v1.ResourceList{}
    limits := v1.ResourceList{}
    zeroQuantity := resource.MustParse("0")
    isGuaranteed := true
    for _, container := range pod.Spec.Containers {
        // process requests
        for name, quantity := range container.Resources.Requests {
            if !isSupportedQoSComputeResource(name) {
                continue
            }
            if quantity.Cmp(zeroQuantity) == 1 {
                delta := quantity.Copy()
                if _, exists := requests[name]; !exists {
                    requests[name] = *delta
                } else {
                    delta.Add(requests[name])
                    requests[name] = *delta
                }
            }
        }
        // process limits
        qosLimitsFound := sets.NewString()
        for name, quantity := range container.Resources.Limits {
            if !isSupportedQoSComputeResource(name) {
                continue
            }
            if quantity.Cmp(zeroQuantity) == 1 {
                qosLimitsFound.Insert(string(name))
                delta := quantity.Copy()
                if _, exists := limits[name]; !exists {
                    limits[name] = *delta
                } else {
                    delta.Add(limits[name])
                    limits[name] = *delta
                }
            }
        }

        if !qosLimitsFound.HasAll(string(v1.ResourceMemory), string(v1.ResourceCPU)) {
            isGuaranteed = false
        }
    }
    if len(requests) == 0 && len(limits) == 0 {
        return v1.PodQOSBestEffort
    }
    // Check is requests match limits for all resources.
    if isGuaranteed {
        for name, req := range requests {
            if lim, exists := limits[name]; !exists || lim.Cmp(req) != 0 {
                isGuaranteed = false
                break
            }
        }
    }
    if isGuaranteed &&
        len(requests) == len(limits) {
        return v1.PodQOSGuaranteed
    }
    return v1.PodQOSBurstable
}

你可能感兴趣的:(kubenetes oom pod)