متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 35Kubernetes · kubectl

How to roll back a Kubernetes deployment

The new version is live and errors go up. The fastest safe action is a rollback to the last good version — fix the bug later. Kubernetes keeps old ReplicaSets of every Deployment, so going back takes seconds. This how-to shows the rollback with kubectl, how to see what each revision was, the Helm and GitOps way (where a kubectl rollback alone is not enough), and how to make the next rollback even easier.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

35.1

The quick answer

kubectl rollout history deploy/api -n shop
kubectl rollout undo deploy/api -n shop
deployment.apps/api rolled back
kubectl rollout status deploy/api -n shop
deployment "api" successfully rolled out

rollout undo goes back one revision. For an older one: kubectl rollout undo deploy/api -n shop --to-revision=7.

الأوامر في هذا الدرس

35.2

Step by step

  1. See the revisions and what is in them:
kubectl rollout history deploy/api -n shop
REVISION  CHANGE-CAUSE
7         image 1.4.2
8         image 1.4.3
kubectl rollout history deploy/api -n shop --revision=7

The second command shows the full Pod template (image, env, resources) of revision 7.

  1. Roll back and watch it:
kubectl rollout undo deploy/api -n shop --to-revision=7
kubectl rollout status deploy/api -n shop --timeout=5m

The rollback is itself a new rollout (a new revision number, with the old template), so the normal rolling update and probes protect you.

  1. Check the running image:
kubectl get deploy api -n shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
registry.example.com/shop/api:1.4.2
  1. Write the reason on each change so the history is readable next time:
kubectl annotate deploy/api -n shop kubernetes.io/change-cause="image 1.4.3, ticket OPS-311" --overwrite
  1. Helm-installed app? Roll back the Helm release, not only the Deployment (otherwise the next helm upgrade brings the bad version back):
helm history shop-api -n shop
helm rollback shop-api 12 -n shop
  1. GitOps (Argo CD / Flux)? The cluster follows Git — a kubectl rollback is undone by the next sync. Roll back in Git: revert the commit that changed the image, and let Argo CD / Flux sync:
git revert 3f9c2e1
git push
  1. Stop a bad rollout in the middle (pause, look, then decide):
kubectl rollout pause deploy/api -n shop
kubectl rollout resume deploy/api -n shop

35.3

Check that it worked

rollout status says successfully rolled out, all Pods run the old image, and your error rate and latency (dashboard, logs) are back to normal. Then write down what happened, and fix the bug in a new version.

35.4

Common problems

ProblemCauseFix
unable to find specified revisionOld ReplicaSets were cleaned up.Keep more: spec.revisionHistoryLimit: 10; or deploy the old tag again.
The bad version comes back after some minutesArgo CD / Flux or a CI job applies Git again.Roll back in Git (revert) or in Helm.
Rollback done, still errorsThe problem is not the image: a ConfigMap, Secret, DB migration or a dependency.Check what else changed; ConfigMaps are not part of the revision.
CHANGE-CAUSE is emptyNobody set the annotation.Annotate on each deploy (CI can do it).
Rollout hangs at "1 old replicas are pending termination"New Pods are not ready (probe fails).kubectl describe pod; fix the probe or roll back.
Database migration can not be undoneSchema changed in a non-backward-compatible way.Make migrations backward compatible (expand, then contract).

الأوامر في هذا الدرس

35.5

Go further

1Why is a kubectl rollout undo not enough in a GitOps setup?

Argo CD / Flux apply what is in Git again; you must revert the change in Git.

2Are ConfigMap changes rolled back by rollout undo?

No. Only the Pod template of the Deployment; ConfigMaps and Secrets keep their current content.

كل الأوامر في هذا الفصل