14-RolloutStrategyRecreate
Let's walk through the Kubernetes deployment lab exercise with the specified scenarios. We'll create a Deployment, update its version, modify the rollout strategy, and observe how pods behave during the process.
Table of Contents
Create a Deployment with the Default Strategy (Rolling Update):
Create a YAML file (e.g.,
my-deployment.yaml) with the following content:apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: nginx:1.19Apply the deployment using
kubectl apply -f my-deployment.yaml.
Update the Version of the Deployment:
Edit the
my-deployment.yamlfile and change theimageto a newer version (e.g.,nginx:1.20).Apply the updated deployment using
kubectl apply -f my-deployment.yaml.Observe how the pods are recreated one by one with the new image.
Modify the Rollout Strategy to Recreate: Here's the updated
my-deployment.yaml:
After applying this updated YAML, you can verify the changes using the following commands:
Apply the updated deployment:
Check the status of the deployment:
Observe how the pods are recreated using the
Recreatestrategy:
The Recreate strategy ensures that all old pods are terminated before new ones are created.
Change the Image Used for the Deployment:
Edit the
my-deployment.yamlfile and update theimageto a different one (e.g.,nginx:1.21).Apply the updated deployment using
kubectl apply -f my-deployment.yaml.Observe how the pods are recreated using the new image.
Check Pods During Recreation:
Run
kubectl get pods -l app=my-app -wto watch the pods.Notice how the old version of the pod is deleted immediately, without waiting for the new pods to create.
View Deployment YAML Output:
Run
kubectl get deployment my-deployment -o yamlto verify that the changes were successful.
Remember to adjust the YAML file names and labels according to your environment. This exercise will help you understand deployment strategies and how Kubernetes handles rolling updates and recreates pods. 😊
Last updated