kubernetes借助系統(tǒng)的OOM KILL提升服務(wù)質(zhì)量胳泉,至于什么是OOM KILL可以去網(wǎng)上搜一下這里不再班門弄斧,下面我們就看一下Kubernetes是按照什么規(guī)則來分別設(shè)置容器的oom_score_adj
。
其實規(guī)則也比較簡單只有一段代碼:
//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)
}
這段代碼就是講的如何計算每個容器的oom score的。
首先看這個容器所屬的Pod是屬于什么級別的,如果是Guaranteed級別的直接返回-998也是最高級最后被Kill掉的,如果是BestEffort級別則直接返回1000是最低級別的市咆,最有可能被殺掉。如果是Burstable則是中間級別需要按照資源的申請量來計算oom score再来。
oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity
就是這段公式計算出容器的score蒙兰,最后得到的值會在2-999之間,從這個公式能夠看出來Burstable級別的如果越是資源申請的越多則給的分越低芒篷,這就意味著容器越不容易被殺掉搜变,如果是申請量越少得出的分越高則越容易被殺掉。