Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
containers:
- name: api
image: registry.example.com/shop/api:1.4.2
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
memory: 512Mikubectl 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.
Step by step
- What each one does:
| Setting | Effect |
|---|---|
requests.cpu | Reserved for scheduling; also the share of CPU under load. |
requests.memory | Reserved for scheduling. |
limits.memory | Hard cap — above it the container is OOMKilled. |
limits.cpu | Throttles the container (it gets slower, not killed). Many teams leave it out to avoid throttling. |
- 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 %.
- QoS classes (who is killed first when a node runs out of memory):
| Class | When |
|---|---|
Guaranteed | requests = limits for CPU and memory. Last to be evicted. |
Burstable | Some requests/limits set. |
BestEffort | Nothing set. First to be evicted. |
- Defaults for a namespace — a LimitRange gives every container values if the team forgets:
apiVersion: v1
kind: LimitRange
metadata:
name: defaults
namespace: shop
spec:
limits:
- type: Container
defaultRequest: { cpu: 100m, memory: 128Mi }
default: { memory: 512Mi }- A budget per namespace — a ResourceQuota stops one team from taking the whole cluster:
apiVersion: v1
kind: ResourceQuota
metadata:
name: shop-quota
namespace: shop
spec:
hard:
requests.cpu: "8"
requests.memory: 16Gi
limits.memory: 32Gikubectl describe resourcequota shop-quota -n shop- Language runtimes — tell Java/Node the limit, or they size memory by the node: JVM
-XX:MaxRAMPercentage=75, Node--max-old-space-size.
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).
Common problems
| Problem | Cause | Fix |
|---|---|---|
Pods OOMKilled | Memory limit below real use (or a leak). | Raise the limit from measured peaks; check the runtime's heap setting. |
Pods Pending: Insufficient cpu | Requests 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 deploying | ResourceQuota reached, or Pods without requests in a quota namespace. | Raise the quota or lower requests; set a LimitRange for defaults. |
| HPA does not scale | No CPU requests. | Set requests.cpu (HPA % is relative to it). |
kubectl top not working | No metrics-server. | Install metrics-server. |
Go further
- Command pages: kubectl.
- Next how-tos: Scale a deployment, Find why a Pod does not start, Limit CPU and memory of a service (systemd), Autoscale Pods with the HPA.
- Learn it: CKA — Resources, CKA — Quotas, CKA — HPA.
- Practise: Lab: troubleshooting drill.
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.