wiki:k8srollingback2023

Version 2 (modified by admin, 12 months ago) ( diff )

--

Kubernetes Deployment Strategies: Rolling Updates and Rollbacks

Kubernetes offers robust deployment strategies, allowing for smooth updates and quick rollbacks in case of issues. The most common strategy is the Rolling Update, which incrementally updates Pods with new ones. Let's dive into how to implement Rolling Updates and Rollbacks.

Part 1: Rolling Updates

Step 1: Create a Deployment

  1. Create a Deployment YAML File: myappdeployment2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp2
  template:
    metadata:
      labels:
        app: myapp2
    spec:
      containers:
      - name: myapp-container2
        image: nginx:1.16
        ports:
        - containerPort: 80
  • Deploy it:

kubectl apply -f myappdeployment2.yaml

Step 2: Update the Deployment

  1. Update the Image: Change the image in your deployment to a new version. For example, update nginx:1.16 to nginx:1.17.
  1. Apply the Updated Deployment:

kubectl apply -f myappdeployment2.yaml

  • Monitor the Rollout:

kubectl rollout status deployment/myapp-deployment2

Kubernetes will stop the old Pods and start new ones with the updated configuration.

Step 3: Verify the Update

  1. Check the Deployed Pods:

kubectl get pods

You should see the Pods with the new image running.

Part 2: Rollbacks

Step 1: Trigger a Faulty Update

  1. Update the Deployment with a Faulty Image: Change the image to a non-existent or faulty one, like nginx:1.18-faulty.
  1. Apply the Faulty Deployment:

kubectl apply -f myappdeployment2.yaml

Step 2: Rollback the Update

  1. Check the Rollout History:

kubectl rollout history deployment/myapp-deployment2

  • Rollback to a Previous Revision: Find the revision number you want to rollback to (e.g., 2) and execute:

kubectl rollout undo deployment/myapp-deployment2 --to-revision=2

  • Verify the Rollback:

kubectl get pods

The Pods should be running with the image from the previous stable revision.

Note: See TracWiki for help on using the wiki.