Recreate Strategy:
In the "Recreate" deployment strategy, all the pods will be terminated simultaneously and recreated with the new version of the pods.
Advantages: It's quick as the pods will get terminated in parallel, and deployment happens in parallel after the termination.
Disadvantage: It gives downtime to the application due to all pods being down at the same time.
Please find the code here: Recreate.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploymentdemo
labels:
type: web
spec:
replicas: 4
strategy:
type: Recreate
selector:
matchLabels:
type: web1
template:
metadata:
name: nginxpods
labels:
type: web1
spec:
containers:
- image: iam7hills/learnkubernetes:podsdemo-1.0
name: podsdemo
imagePullPolicy: Always
Step 1: Run your yaml file
When you run for the first time,
kubectl create -f Recreate.yaml
deployment.apps/deploymentdemo created
NAME DESIRED CURRENT READY AGE
deploymentdemo-75f6466945 4 4 4 56s
kubectl get pods
NAME READY STATUS RESTARTS AGE
deploymentdemo-75f6466945-brrjn 1/1 Running 0 12s
deploymentdemo-75f6466945-njqbw 1/1 Running 0 12s
deploymentdemo-75f6466945-ns466 1/1 Running 0 12s
deploymentdemo-75f6466945-qx6kj 1/1 Running 0 12s
Step 2: Modify the image to 2.0
Now, edit the image version as 2.0 and rerun the Recreate.yaml
- image: iam7hills/learnkubernetes:podsdemo-2.0
Initial State
kubectl get rs
NAME DESIRED CURRENT READY AGE
deploymentdemo-6c59d8d76d 0 0 0 54s # NOTE - NEW REPLICASET
deploymentdemo-75f6466945 4 4 4 2m46s
If you notice here, there is a new replica set was created. And also all the pods in the old replica will be terminated at the same time.
kubectl get pods
NAME READY STATUS RESTARTS AGE
deploymentdemo-75f6466945-brrjn 0/1 Terminating 0 112s
deploymentdemo-75f6466945-njqbw 0/1 Terminating 0 112s
deploymentdemo-75f6466945-ns466 1/1 Terminating 0 112s
deploymentdemo-75f6466945-qx6kj 0/1 Terminating 0 112s
All the running pods get terminated first before creating the new pods with version 2.0 in the new replicaset.
Final State - After few minutes
kubect get pods
NAME READY STATUS RESTARTS AGE
deploymentdemo-6c59d8d76d-l76v6 1/1 Running 0 4m19s
deploymentdemo-6c59d8d76d-qlbck 1/1 Running 0 4m20s
deploymentdemo-6c59d8d76d-w88fx 1/1 Running 0 4m19s
deploymentdemo-6c59d8d76d-xbr9j 1/1 Running 0 4m19s
kubectl get rs
NAME DESIRED CURRENT READY AGE
deploymentdemo-6c59d8d76d 4 4 4 21m - # Now the new replicaset have all the pods ready
deploymentdemo-75f6466945 0 0 0 23m