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:
- The number of replica Pods the Deployment will seek to maintain - replicas.
- A label selector used to identify the replica Pods managed by the Deployment - selector.
- 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
- Edit the deployment spec, changing the image version to
1.19.2
.Edit:
kubectl edit deploy my-deployment
spec: containers: - name: nginx image: nginx:1.19.2
- Check the rollout status, deployment status, and pods.
kubectl rollout status deploy/my-deployment kubectl get deploy my-deployment kubectl get po
- Perform another rollout, this time using the
kubectl set image
method. Intentionally use a bad image version.
kubectl set image deployment/my-deployment nginx=nginx:broken --record
- Check the rollout status again. You will see the rollout unable to succeed due to a failed image pull.
kubectl rollout status deploy/my-deployment kubectl get pods
- Check the rollout history.
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
- Roll back to an earlier working version with one of the following methods.Or:
#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>