0
0
Kubernetesdevops~5 mins

Recreate update strategy in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to update your app by stopping the old version completely before starting the new one. The Recreate update strategy in Kubernetes does exactly that. It helps avoid conflicts by making sure only one version runs at a time.
When your app cannot run two versions at the same time because they share the same resources like files or ports.
When you want to avoid any chance of users connecting to two different versions during an update.
When your app needs a clean start without leftover data from the previous version.
When you want a simple update process without rolling multiple pods at once.
When you have a small app or low traffic and can afford a short downtime during updates.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
  labels:
    app: example-app
spec:
  replicas: 2
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-app
        image: nginx:1.21
        ports:
        - containerPort: 80

This file defines a Deployment named example-app with 2 replicas.

The strategy.type: Recreate tells Kubernetes to stop all old pods before starting new ones during updates.

The selector and template.metadata.labels ensure the Deployment manages the correct pods.

The container runs the nginx:1.21 image on port 80.

Commands
This command creates the Deployment with the Recreate update strategy. It sets up the app pods as defined in the file.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/example-app created
This command lists all pods with the label app=example-app to verify they are running.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-app-6d4cfb6b7f-abcde 1/1 Running 0 10s example-app-6d4cfb6b7f-fghij 1/1 Running 0 10s
-l - Filter pods by label
This command updates the Deployment to use a new image version nginx:1.22, triggering the Recreate update strategy.
Terminal
kubectl set image deployment/example-app example-app=nginx:1.22
Expected OutputExpected
deployment.apps/example-app image updated
Check pods again to see that old pods are terminated before new pods start, confirming the Recreate strategy.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-app-7f9d8c9d5f-klmno 1/1 Running 0 5s example-app-7f9d8c9d5f-pqrst 1/1 Running 0 5s
-l - Filter pods by label
Key Concept

If you remember nothing else from this pattern, remember: Recreate update strategy stops all old pods before starting new ones to avoid running multiple versions simultaneously.

Common Mistakes
Not specifying the update strategy, so Kubernetes uses RollingUpdate by default.
This causes old and new pods to run at the same time, which can cause conflicts if your app can't handle it.
Explicitly set strategy.type to Recreate in the Deployment spec to ensure old pods stop before new ones start.
Updating the Deployment image without checking pod status.
You might miss that pods are restarting or stuck, causing downtime or failed updates.
Always check pod status with kubectl get pods after updates to confirm the Recreate strategy worked.
Summary
Create a Deployment with strategy.type set to Recreate to control update behavior.
Apply the Deployment and verify pods are running with kubectl get pods.
Update the Deployment image to trigger the Recreate update, which stops old pods before starting new ones.
Check pods again to confirm the update completed with no overlapping versions.