🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

kubernetes deployment strategy explained with example

Fearture of Kubernetes Deployment

  • Replication
  • Controller
  • Versioning
  • Rollout
  • Rollback

Note:
ReplicaSets = Replication+Controller in the Deployment


$ kubectl explain deploy.spec.strategy.type
KIND: Deployment
VERSION: apps/v1

FIELD: type

DESCRIPTION:
Type of deployment. Can be “Recreate” or “RollingUpdate”. Default is
RollingUpdate.


Kubernetes Deployement Strategy

Type of deployment

  • Recreate
  • RollingUpdate

.spec.strategy specifies the strategy used to replace old Pods by new ones. .spec.strategy.type can be “Recreate” or “RollingUpdate”. “RollingUpdate” is the default value

Recreate Deployment

All existing Pods are killed before new ones are created when .spec.strategy.type==Recreate.

This will only guarantee Pod termination previous to creation for upgrades. If you upgrade a Deployment, all Pods of the old revision will be terminated immediately. Successful removal is awaited before any Pod of the new revision is created. If you manually delete a Pod, the lifecycle is controlled by the ReplicaSet and the replacement will be created immediately (even if the old Pod is still in a Terminating state). If you need an “at most” guarantee for your Pods, you should consider using a StatefulSet.

Rolling Update Deployment

The advantage of the rolling update strategy is that the update is applied Pod-by-Pod so the greater system can remain active.

There is a minor performance reduction during this update process because the system is consistently one active Pod short of the desired number of Pods. This is often much preferred to a full system deactivation.

The rolling update strategy is used as the default update strategy but isn’t suited for all situations.


The Deployment updates Pods in a rolling update fashion when .spec.strategy.type==RollingUpdate. You can specify maxUnavailable and maxSurge to control the rolling update process.

   maxSurge     <string>
     The maximum number of pods that can be scheduled above the desired number
     of pods. Value can be an absolute number (ex: 5) or a percentage of desired
     pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
     is calculated from percentage by rounding up. Defaults to 25%. Example:
     when this is set to 30%, the new ReplicaSet can be scaled up immediately
     when the rolling update starts, such that the total number of old and new
     pods do not exceed 130% of desired pods. Once old pods have been killed,
     new ReplicaSet can be scaled up further, ensuring that total number of pods
     running at any time during the update is at most 130% of desired pods.

   maxUnavailable       <string>
     The maximum number of pods that can be unavailable during the update. Value
     can be an absolute number (ex: 5) or a percentage of desired pods (ex:
     10%). Absolute number is calculated from percentage by rounding down. This
     can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set
     to 30%, the old ReplicaSet can be scaled down to 70% of desired pods
     immediately when the rolling update starts. Once new pods are ready, old
     ReplicaSet can be scaled down further, followed by scaling up the new
     ReplicaSet, ensuring that the total number of pods available at all times
     during the update is at least 70% of desired pods.

kubernetes deployment strategy example: Recreate


apiVersion: apps/v1  
kind: Deployment
metadata:
  name: hello-deploy
spec:
  replicas: 10
  selector:
    matchLabels:
      app: hello-world
  minReadySeconds: 10
  strategy:
    type: Recreate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-Pod
        image: scmgalaxy/nginx-devopsschoolv1
        ports:
        - containerPort: 80

kubernetes deployment strategy example: Rollingupdate


apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld
spec:
  replicas: 3
  strategy:
    rollingUpdate:
    maxSurge: 1
    maxUnavailable: 1
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: helloworld
        track: stable
    spec:
      containers:
      - name: helloworld
        image: scmgalaxy/nginx-devopsschoolv1
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 50m
          limits:
            cpu: 100m

Kubernetes Deployment & Replication
$ kubectl create -h
$ kubectl create deployment -h
$ kubectl get deploy
$ kubectl get pods
$ kubectl create deployment my-dep --image=scmgalaxy/nginx-devopsschoolv1 --replicas=3
$ kubectl get pods
$ kubectl get deploy
$ kubectl edit deploy my-dep
$ kubectl get deploy
$ kubectl
$ kubectl scale
$ kubectl scale -h
$ kubectl get deploy
$ kubectl scale--replicas=2 deployment/my-dep
$ kubectl scale --replicas=2 deployment/my-dep
Kubernetes Deployment & Controller
$ kubectl get deploy
$ kubectl get pods
$ kubectl delete pod my-dep-747b4ffb56-t92dp my-dep-747b4ffb56-tsz8s
$ kubectl get pods
Kubernetes Deployment & Rollout & versioning
$ kubectl rollout -h
$ kubectl rollout history deploy/my-dep
$ kubectl get -o wide
$ kubectl get pods --o wide
$ kubectl get pods -o wide
$ curl http://10.44.0.2
$ curl http://10.44.0.1
$ kubectl rollout history deploy/my-dep
$ kubectl edit deploy/my-dep
$ kubectl get pods -o wide
$ kubectl get pods -o wide
$ curl http://10.44.0.2
$ curl http://10.44.0.3
$ kubectl rollout history deploy/my-dep
$ kubectl explain
$ kubectl explain ns
$ kubectl explain ns.spec
$ kubectl explain pods
$ kubectl explain pods.spec
$ kubectl explain pods.spec.containers
$ kubectl explain deploy
$ kubectl explain deploy.spec
$ kubectl explain deploy.spec.strategy
$ kubectl explain deploy.spec.strategy.rollingUpdate
$ kubectl get deploy
$ kubectl delete deploy my-dep
Kubernetes Deployment & Rollout & Rollback
$ kubectl rollout undo -h
$ kubectl get deploy
$ kubectl create deployment my-dep --image=scmgalaxy/nginx-devopsschoolv1 --replicas=15
$ kubectl rollout history deploy/my-dep
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://10.44.0.5
$ kubectl edit deploy/my-dep
$ kubectl rollout history deploy/my-dep
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://10.44.0.12
$ kubectl rollout status deploy/my-dep
$ kubectl rollout history deploy/my-dep
$ clear
$ kubectl rollout undo deploy/my-dep --to-revision=1
$ kubectl get pods -o wide
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://10.44.0.10
Rollout
$ kubectl create deployment my-dep --image=scmgalaxy/nginx-devopsschoolv1 --replicas=15
$ kubectl rollout history deploy/my-dep
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://ip-of-pod
$ kubectl edit deploy/my-dep # Change image to scmgalaxy/nginx-devopsschoolv2
$ kubectl rollout history deploy/my-dep
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://ip-of-pod
Rollback
$ kubectl rollout history deploy/my-dep
$ kubectl rollout undo deploy/my-dep --to-revision=1
$ kubectl rollout status deploy/my-dep
$ kubectl get pods -o wide
$ curl http://ip-of-pod

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.