Review container resource requests and limits

Configure CPU and memory requests and usage limits for the actual workload.

Description

Kubernetes uses requests to schedule a Pod and limits to bound a container’s CPU and memory use. A namespace LimitRange can supply defaults when Terraform omits these values.

Potential impact

Large requests can leave Pods unscheduled. Small requests can leave workloads competing for resources on the same node. A low limit can throttle CPU or cause an out-of-memory termination.

Remediation

Measure each container at normal and peak load. Set its requests and any required limits, keeping each request within its limit. Check LimitRange defaults and the namespace totals allowed by ResourceQuota, then test performance with the deployed Pod’s values.

Examples

Use a maintained image and adjust the example values for your workload.

Before

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

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

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"
        }
      }
    }
  }
}

The first Pod omits resources. The second requests 250m CPU and 50Mi of memory, with limits of 0.5 CPU and 512Mi of memory.

References