Appearance

Topology and resource governance

Spread replicas across failure domains and make room in the namespace budget for releases.

What you will understand

  • Explain how a topology key and a label selector determine which copies count toward spread.
  • Use ResourceQuota to reason about how many requested copies admission will accept.
  • Explain how a LimitRange changes the values on Pods that omit resources.

Builds on: Scheduling constraints

How Kubernetes spreads replicas across zones

Imagine a six-Pod API spread over three zones. If the zones have three, two, and one copy, some callers lose more capacity if the busiest zone fails. A topology spread constraint asks the scheduler to balance selected Pods across groups of nodes called topology domains. A node label might identify a zone, or the hostname label could make each node a separate domain. The selector tells the scheduler which Pods belong in the count.

In a simplified three-zone example with counts of two, two, and one, another Pod could bring them to two, two, and two. If the incoming Pod were placed in a zone already at two, its new count of three would differ from the minimum of one by two. A rule with maxSkew: 1 and DoNotSchedule would reject that placement in this example. Look for eligible domains in the scene before doing the arithmetic. Kubernetes accounts for options such as minDomains and which domains qualify under the rule.

Where can one more replica keep the zones balanced?

Count matching Pods in the eligible domains, then test where the next replica keeps the skew within the allowed bound.

  1. Count catalog Pods per zone

    The selector counts api Pods, not every Pod in the namespace. The current eligible zone counts are east=2 and west=1.

  2. Try the crowded zone

    Putting the new replica in east creates counts 3 and 1. Skew 2 exceeds the configured maximum 1.

  3. Try the less crowded zone

    Putting it in west creates counts 2 and 2, so this topology constraint allows the placement.

  4. Apply the remaining placement rules

    • New api PodSelector counts app=api replicas

    The scheduler can choose a fitting west node after all other resource and placement rules pass. Existing Pods do not move merely because this rule exists.

Two eligible zones with consistent labels; examples omit advanced minDomains and inclusion-policy settings.

Explore another case

Add a spread rule to a Deployment

Place the first fragment inside an existing Deployment’s spec.template.spec. Its own Pod template should carry app: api so that each new copy counts toward the distribution. Check the labels on the actual nodes first. The rule in the example uses zone labels, which may be missing in a small local cluster. For a local exercise, a comparable rule can reason about hostnames instead.

# Fragment: spec.template.spec of a Deployment labeled app=api
 topologySpreadConstraints:
   - maxSkew: 1
     topologyKey: topology.kubernetes.io/zone
     whenUnsatisfiable: DoNotSchedule
     labelSelector:
       matchLabels:
         app: api
 containers:
   - name: api
     image: nginx:1.27-alpine
     resources:
       requests: {cpu: 100m, memory: 64Mi}
       limits: {cpu: 500m, memory: 128Mi}

Budget for a rollout

A ResourceQuota limits aggregate quantities in a namespace. The example allows requests of one CPU and 1 GiB of memory, and up to ten counted Pods. A LimitRange can supply defaults and enforce per-object bounds. In this example it adds a request of 100m and 64Mi to a container that does not specify them. Apply quota and defaults in a disposable namespace so other workloads do not consume your budget.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-budget
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    pods: "10"
---
apiVersion: v1
kind: LimitRange
metadata:
  name: container-defaults
spec:
  limits:
    - type: Container
      defaultRequest: {cpu: 100m, memory: 64Mi}
      default: {cpu: 500m, memory: 256Mi}

Ten copies at 100m use the full CPU budget. A Deployment trying to add an eleventh copy during a surge can be blocked by admission, even with room left on a node. Check ResourceQuota usage and ReplicaSet events to distinguish a rejected create from a Pod that exists but could not schedule. Try both constraints in the budget scene and the scheduling and quota lab.

When does an over-budget Pod get blocked?

Defaults can fill in resource requests. Namespace quota can reject a create before the scheduler starts looking for a node.

  1. Request a new Pod

    The submitted Pod has no CPU request, but admission has not yet completed.

  2. Fill missing defaults

    LimitRange supplies a 100m CPU request, changing the amount quota and scheduling must account for.

  3. Compare against namespace quota

    950m plus 100m exceeds the 1 CPU namespace quota. Admission rejects the Pod rather than leaving that Pod Pending.

  4. Recheck when room is available

    • ResourceQuota1 CPU hard; 800m already used before this create

    After accounted requests fall sufficiently, a new create can pass quota. The scheduler still independently checks node capacity.

One namespace and one CPU request budget; real quotas can track many resources.

Inspect YAML or commands

Apply defaults

defaultRequest:
  cpu: 100m

Free budget then place

kubectl describe resourcequota team-budget
Explore another case

Check quota and node capacity

Quotas govern the resources admitted in a namespace. Requests still need to fit physical nodes, and spread or affinity rules can further restrict the eligible nodes. Plan capacity for old and new replicas to overlap when a rollout needs a surge. Adjust tight budgets deliberately rather than relying on an idle usage chart.

Check your understanding

A rollout stalls at ten Pods. There is no eleventh Pod to describe. Which object helps you discover why?

Inspect the new ReplicaSet’s events. If admission rejected its attempt to create the Pod, the event can report which quota the request exceeded. Compare that with namespace quota usage and any LimitRange defaults.

Primary documentation

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu