Kubernetes OOM計(jì)算規(guī)則

kubernetes借助系統(tǒng)的OOM KILL提升服務(wù)質(zhì)量,至于什么是OOM KILL可以去網(wǎng)上搜一下這里不再班門(mén)弄斧,下面我們就看一下Kubernetes是按照什么規(guī)則來(lái)分別設(shè)置容器的oom_score_adj。

其實(shí)規(guī)則也比較簡(jiǎn)單只有一段代碼:

//pkg/kubelet/qos/policy.go
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)
}

這段代碼就是講的如何計(jì)算每個(gè)容器的oom score的。
首先看這個(gè)容器所屬的Pod是屬于什么級(jí)別的,如果是Guaranteed級(jí)別的直接返回-998也是最高級(jí)最后被Kill掉的,如果是BestEffort級(jí)別則直接返回1000是最低級(jí)別的,最有可能被殺掉。如果是Burstable則是中間級(jí)別需要按照資源的申請(qǐng)量來(lái)計(jì)算oom score。

oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity

就是這段公式計(jì)算出容器的score,最后得到的值會(huì)在2-999之間,從這個(gè)公式能夠看出來(lái)Burstable級(jí)別的如果越是資源申請(qǐng)的越多則給的分越低,這就意味著容器越不容易被殺掉,如果是申請(qǐng)量越少得出的分越高則越容易被殺掉。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容