هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Check what controls the replica count first — otherwise your change is undone:
If an HPA exists, change its minReplicas/maxReplicas. If Argo CD/Flux manage the app, change replicas in Git.
- 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 shopStatefulSets scale one by one, in order (postgres-0, -1, -2) — the app must support it (a database cluster needs its own setup).
- Autoscale with an HPA (needs metrics-server and CPU requests on the containers):
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: 70kubectl 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- Scale to zero (stop an app but keep its config — test environments at night):
kubectl scale deploy/api -n test --replicas=0- 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=2Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Replicas go back to the old number | An 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 Pending | No free CPU/memory on the nodes. | Cluster Autoscaler / more nodes, or smaller requests. |
| All Pods on one node | No spread rules. | topologySpreadConstraints or Pod anti-affinity. |
| Scaling up does not help | The bottleneck is elsewhere (database, an external API). | Check where the time goes (traces, DB load) before adding Pods. |
| Scale-down cut user sessions | Sessions stored in the Pod's memory. | Store sessions in Redis/DB; use readiness and preStop for a clean stop. |
Go further
- Command pages: kubectl.
- Next how-tos: Roll back a deployment, Check memory and CPU usage, Find why a Pod does not start, Set resource requests and limits, Autoscale Pods with the HPA.
- Learn it: CKA — Autoscaling (HPA), CKA — Resources, CKA — Quotas.
- Full guides: Kubernetes with kubeadm, k3s, Prometheus.
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.