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 36Kubernetes · kubectl

How to scale a deployment up and down

More traffic is coming (a sale, a launch), or a test environment should cost nothing at night. Scaling changes the number of Pod replicas. You can do it by hand in one command, or let the Horizontal Pod Autoscaler (HPA) do it from CPU or memory. This how-to shows both, explains why scaling sometimes "does nothing" (HPA, GitOps, no free capacity), and the safe way to scale down.

  • 5 Lessen
  • 1 Commando's

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

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

36.1

The quick answer

kubectl scale deploy/api -n shop --replicas=6
kubectl get deploy api -n shop
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
api    6/6     6            6           21d
kubectl autoscale deploy/api -n shop --min=3 --max=12 --cpu-percent=70

36.2

Step by step

  1. Check what controls the replica count first — otherwise your change is undone:
kubectl get hpa -n shop
kubectl get deploy api -n shop -o jsonpath='{.metadata.annotations}{"\n"}'

If an HPA exists, change its minReplicas/maxReplicas. If Argo CD/Flux manage the app, change replicas in Git.

  1. Scale by hand (Deployment or StatefulSet):
kubectl scale deploy/api -n shop --replicas=6
kubectl scale statefulset/postgres -n shop --replicas=3
kubectl rollout status deploy/api -n shop

StatefulSets scale one by one, in order (postgres-0, -1, -2) — the app must support it (a database cluster needs its own setup).

  1. Autoscale with an HPA (needs metrics-server and CPU requests on the containers):
hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api
  namespace: shop
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api
  minReplicas: 3
  maxReplicas: 12
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
kubectl apply -f hpa.yaml
kubectl get hpa api -n shop -w
NAME   REFERENCE        TARGETS       MINPODS   MAXPODS   REPLICAS
api    Deployment/api   cpu: 85%/70%  3         12        5
  1. Scale to zero (stop an app but keep its config — test environments at night):
kubectl scale deploy/api -n test --replicas=0
  1. Scale down safely: keep at least 2 replicas for anything users need, and protect it with a PodDisruptionBudget so node maintenance never takes all Pods at once:
kubectl create pdb api -n shop --selector=app=api --min-available=2

36.3

Check that it worked

kubectl get pods -n shop -l app=api -o wide
kubectl top pods -n shop -l app=api

All replicas are Running and ready, spread over several nodes, and the CPU per Pod went down.

36.4

Common problems

ProblemCauseFix
Replicas go back to the old numberAn HPA or GitOps controls replicas.Change the HPA min/max, or the value in Git.
HPA shows <unknown>/70%No metrics-server, or no CPU requests on the containers.Install metrics-server; set resources.requests.cpu.
New Pods stay PendingNo free CPU/memory on the nodes.Cluster Autoscaler / more nodes, or smaller requests.
All Pods on one nodeNo spread rules.topologySpreadConstraints or Pod anti-affinity.
Scaling up does not helpThe bottleneck is elsewhere (database, an external API).Check where the time goes (traces, DB load) before adding Pods.
Scale-down cut user sessionsSessions stored in the Pod's memory.Store sessions in Redis/DB; use readiness and preStop for a clean stop.

36.5

Go further

1You scaled a Deployment to 6, and a minute later it is back at 3. Why?

Something else controls the replicas: an HPA (min/max) or GitOps applying Git again.

2What does an HPA need to scale on CPU?

metrics-server, and CPU requests on the containers.

Alle commando's in dit hoofdstuk