Review container memory limits

Check memory limits and their effective values against actual usage.

Description

resources.limits.memory caps a container’s memory. A namespace LimitRange can supply a default cap when Terraform omits it.

Potential impact

Without a cap, a spike can put the node under memory pressure, slowing other Pods or causing evictions. A cap set too low can lead to an out-of-memory (OOM) termination in the container.

Remediation

Size the request and limit for normal and peak memory use. Check the range allowed by LimitRange and the deployed values. Monitor both OOM terminations and node memory pressure after the change.

Examples

Deploy a maintained image and use values suited to your workload.

Before

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"

      resources {
        requests = {
          memory = "50Mi"
        }
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"

      resources {
        limits = {
          cpu    = "0.5"
          memory = "512Mi"
        }

        requests = {
          cpu    = "250m"
          memory = "50Mi"
        }
      }
    }
  }
}

Both Pods request 50Mi of memory. The second also sets a memory limit of 512Mi.

References