Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 130Kubernetes · Workloads

How to set resource requests and limits

Without resource settings, Kubernetes places Pods blindly: nodes get overloaded, one Pod can starve the others, and the autoscaler can not work. Requests tell the scheduler what a container needs; limits cap what it may use. This how-to explains both, finds good numbers from real usage, sets them, and adds namespace defaults and quotas.

  • 5 Lessen
  • 2 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

130.1

The quick answer

deployment.yaml (part)
      containers:
        - name: api
          image: registry.example.com/shop/api:1.4.2
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              memory: 512Mi
kubectl apply -f deployment.yaml
kubectl top pods -n shop
kubectl describe node worker1 | grep -A8 'Allocated resources'

250m = a quarter of a CPU core. Mi = mebibytes.

130.2

Step by step

  1. What each one does:
SettingEffect
requests.cpuReserved for scheduling; also the share of CPU under load.
requests.memoryReserved for scheduling.
limits.memoryHard cap — above it the container is OOMKilled.
limits.cpuThrottles the container (it gets slower, not killed). Many teams leave it out to avoid throttling.
  1. Find real numbers — look at usage for some days (normal and peak):
kubectl top pods -n shop --containers

(Or Prometheus/Grafana for history.) A good start: request ≈ normal usage, memory limit ≈ peak + 30 %.

  1. QoS classes (who is killed first when a node runs out of memory):
ClassWhen
Guaranteedrequests = limits for CPU and memory. Last to be evicted.
BurstableSome requests/limits set.
BestEffortNothing set. First to be evicted.
  1. Defaults for a namespace — a LimitRange gives every container values if the team forgets:
limitrange.yaml
apiVersion: v1
kind: LimitRange
metadata:
  name: defaults
  namespace: shop
spec:
  limits:
    - type: Container
      defaultRequest: { cpu: 100m, memory: 128Mi }
      default: { memory: 512Mi }
  1. A budget per namespace — a ResourceQuota stops one team from taking the whole cluster:
quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: shop-quota
  namespace: shop
spec:
  hard:
    requests.cpu: "8"
    requests.memory: 16Gi
    limits.memory: 32Gi
kubectl describe resourcequota shop-quota -n shop
  1. Language runtimes — tell Java/Node the limit, or they size memory by the node: JVM -XX:MaxRAMPercentage=75, Node --max-old-space-size.

130.3

Check that it worked

kubectl describe pod shows the requests/limits and QoS class, kubectl top shows usage below the limits, no new OOMKilled restarts, and new Pods schedule (not Pending for Insufficient cpu/memory).

130.4

Common problems

ProblemCauseFix
Pods OOMKilledMemory limit below real use (or a leak).Raise the limit from measured peaks; check the runtime's heap setting.
Pods Pending: Insufficient cpuRequests too high for free node capacity.Lower requests to real usage, or add nodes.
App slow under load, CPU never 100 %CPU limit throttling.Raise or remove limits.cpu; keep a CPU request.
exceeded quota when deployingResourceQuota reached, or Pods without requests in a quota namespace.Raise the quota or lower requests; set a LimitRange for defaults.
HPA does not scaleNo CPU requests.Set requests.cpu (HPA % is relative to it).
kubectl top not workingNo metrics-server.Install metrics-server.

130.5

Go further

1What happens when a container uses more memory than its limit, and more CPU than its limit?

Memory: it is OOMKilled. CPU: it is throttled (slower), not killed.

2Why does the HPA need CPU requests?

The CPU target in percent is measured relative to the request.

Alle commando's in dit hoofdstuk