Recreate Strategy in Kubernetes: What It Is and When to Use
Recreate strategy in Kubernetes is a way to update pods by first deleting all existing pods before creating new ones. This ensures no old and new pods run at the same time, which is useful when pods cannot run together safely.How It Works
The Recreate strategy works by stopping all the current pods of a deployment before starting any new pods. Imagine you have a team working on a project, and you want everyone to switch to a new version of the project at the same time. Instead of some people working on the old version while others start the new one, everyone stops first, then everyone starts fresh with the new version.
This approach avoids conflicts that might happen if old and new pods run together, such as using the same resources or causing errors. However, it causes downtime because there is a moment when no pods are running until the new ones are ready.
Example
This example shows a Kubernetes deployment using the Recreate strategy to update pods.
apiVersion: apps/v1 kind: Deployment metadata: name: example-app spec: replicas: 3 strategy: type: Recreate selector: matchLabels: app: example template: metadata: labels: app: example spec: containers: - name: example-container image: nginx:1.21 ports: - containerPort: 80
When to Use
Use the Recreate strategy when your application cannot safely run multiple versions at the same time. For example, if your pods use shared resources that can cause conflicts or data corruption when accessed by different versions simultaneously.
This strategy is also helpful when you want a clean restart of all pods without mixing old and new versions. However, be aware it causes downtime, so it is not ideal for applications that require high availability.
Key Points
- Recreate deletes all old pods before creating new ones.
- It prevents old and new pods from running together.
- Causes downtime during updates.
- Best for apps that cannot run multiple versions simultaneously.
- Simple and safe but not suitable for zero-downtime needs.