policy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package qos
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
  17. "k8s.io/kubernetes/pkg/kubelet/types"
  18. )
  19. const (
  20. // KubeletOOMScoreAdj is the OOM score adjustment for Kubelet
  21. KubeletOOMScoreAdj int = -999
  22. // KubeProxyOOMScoreAdj is the OOM score adjustment for kube-proxy
  23. KubeProxyOOMScoreAdj int = -999
  24. guaranteedOOMScoreAdj int = -998
  25. besteffortOOMScoreAdj int = 1000
  26. )
  27. // GetContainerOOMScoreAdjust returns the amount by which the OOM score of all processes in the
  28. // container should be adjusted.
  29. // The OOM score of a process is the percentage of memory it consumes
  30. // multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000
  31. // and 1000. Containers with higher OOM scores are killed if the system runs out of memory.
  32. // See https://lwn.net/Articles/391222/ for more information.
  33. func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int {
  34. if types.IsCriticalPod(pod) {
  35. // Critical pods should be the last to get killed.
  36. return guaranteedOOMScoreAdj
  37. }
  38. switch v1qos.GetPodQOS(pod) {
  39. case v1.PodQOSGuaranteed:
  40. // Guaranteed containers should be the last to get killed.
  41. return guaranteedOOMScoreAdj
  42. case v1.PodQOSBestEffort:
  43. return besteffortOOMScoreAdj
  44. }
  45. // Burstable containers are a middle tier, between Guaranteed and Best-Effort. Ideally,
  46. // we want to protect Burstable containers that consume less memory than requested.
  47. // The formula below is a heuristic. A container requesting for 10% of a system's
  48. // memory will have an OOM score adjust of 900. If a process in container Y
  49. // uses over 10% of memory, its OOM score will be 1000. The idea is that containers
  50. // which use more than their request will have an OOM score of 1000 and will be prime
  51. // targets for OOM kills.
  52. // Note that this is a heuristic, it won't work if a container has many small processes.
  53. memoryRequest := container.Resources.Requests.Memory().Value()
  54. oomScoreAdjust := 1000 - (1000*memoryRequest)/memoryCapacity
  55. // A guaranteed pod using 100% of memory can have an OOM score of 10. Ensure
  56. // that burstable pods have a higher OOM score adjustment.
  57. if int(oomScoreAdjust) < (1000 + guaranteedOOMScoreAdj) {
  58. return (1000 + guaranteedOOMScoreAdj)
  59. }
  60. // Give burstable pods a higher chance of survival over besteffort pods.
  61. if int(oomScoreAdjust) == besteffortOOMScoreAdj {
  62. return int(oomScoreAdjust - 1)
  63. }
  64. return int(oomScoreAdjust)
  65. }