Deployments

Deployments


What Is a Deployment? A K8s object that defines a desired state for a ReplicaSet (a set of replica Pods). The Deployment Controller seeks to maintain the desired state by creating, deleting, and replacing Pods with new configurations.
A Deployment's Desired State Includes:
  1. The number of replica Pods the Deployment will seek to maintain - replicas.
  1. A label selector used to identify the replica Pods managed by the Deployment - selector.
  1. A template Pod definition used to create replica Pods - template.
Use Cases
There are many use cases for Deployments, such as:
  • Easily scale an application up or down by changing the number of replicas.
  • Perform rolling updates to deploy a new software version.
  • Roll back to a previous software version.
Note: Update the replicas under spec, not status in the manifest file.
Kubernetes automatically manages the status based on actual state. The spec defines desired state, and Kubernetes handles scaling to match it.

kubectl scale

kubectl scale --replicas=3 deployment/demo-deployment
Executing this command will adjust the deployment called demo-deployment so it has three running replicas. You can target a different kind of resource by substituting its name instead of deployment:
# ReplicaSet $ kubectl scale --replicas=3 rs/demo-replicaset # ReplicationController $ kubectl scale --replicas=3 rc/demo-replicationcontroller # StatefulSet $ kubectl scale --replicas=3 sts/demo-statefulset
Managing Rolling Updates With Deployments
  1. Edit the deployment spec, changing the image version to 1.19.2.Edit:
    1. kubectl edit deploy my-deployment
      spec: containers: - name: nginx image: nginx:1.19.2
  1. Check the rollout status, deployment status, and pods.
    1. kubectl rollout status deploy/my-deployment kubectl get deploy my-deployment kubectl get po
  1. Perform another rollout, this time using the kubectl set image method. Intentionally use a bad image version.
    1. kubectl set image deployment/my-deployment nginx=nginx:broken --record
  1. Check the rollout status again. You will see the rollout unable to succeed due to a failed image pull.
    1. kubectl rollout status deploy/my-deployment kubectl get pods
  1. Check the rollout history.
    1. kubectl rollout history deploy/my-deployment -------------------------------------------- deployment.apps/my-deployment REVISION CHANGE-CAUSE 1 none 2 kubectl set image deploy/my-deployment nginx=nginx:1.19.2 --record 3 kubectl set image deploy/my-deployment nginx=nginx:broken --record
  1. Roll back to an earlier working version with one of the following methods.Or:
    1. #undo to previous version kubectl rollout undo deploy/my-deployment
      #undo to specific version eg. 1, 2, 3 as shown above kubectl rollout undo deploy/my-deployment --to-revision=<last working revision>