Version note: examples use the Kubernetes v1.35 documentation snapshot, not necessarily the newest release. Check current support and component compatibility. These commands were not tested on a live cluster.
What this guide covers
The Recreate strategy stops the old Deployment Pods before starting updated ones. It is useful when two application versions must not run together, but it causes a period with no application Pods.
Before you start
Create recreate-lab in a test cluster. Plan downtime and a backup if the change affects persistent data. Recreate does not by itself coordinate a database migration.
Step 1: Create the Deployment
Save this as recreate.yaml and apply it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: legacy-web
namespace: recreate-lab
spec:
replicas: 1
progressDeadlineSeconds: 180
strategy:
type: Recreate
selector:
matchLabels:
app: legacy-web
template:
metadata:
labels:
app: legacy-web
spec:
terminationGracePeriodSeconds: 45
containers:
- name: web
image: nginx:1.28.0
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
periodSeconds: 5
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
---
apiVersion: v1
kind: Service
metadata:
name: legacy-web
namespace: recreate-lab
spec:
selector:
app: legacy-web
ports:
- port: 80
targetPort: http
Run kubectl apply -f recreate.yaml. The relevant setting is strategy.type: Recreate.
Step 2: Watch an image change
Replace APPROVED_NEW_IMAGE with a real compatible image. Run the watch in one terminal and the update in another:
kubectl -n recreate-lab get pods -l app=legacy-web --watch
# In another terminal, submit an approved image change:
kubectl -n recreate-lab set image deployment/legacy-web web=APPROVED_NEW_IMAGE
kubectl -n recreate-lab rollout status deployment/legacy-web --timeout=240s
kubectl -n recreate-lab get endpointslices -l kubernetes.io/service-name=legacy-web
Observe the gap between old Pods stopping and new Pods becoming ready. Check the Service’s endpoints and a real request after startup.
Step 3: Recover if startup fails
Read the Pod logs and events. Restore the previous image only if it is compatible with current data. Keep users informed during planned downtime rather than describing this strategy as zero-downtime.
Check the result
The new version should be Ready and serve the expected response. Document the outage duration and restore procedure before choosing this strategy for a live service.
Conversation
0 commentsNo comments yet. Start the conversation below.