Kubernetes Deployments
Welcome back to my blog! Today, we are going explore Kubernetes Deployments and the features that are available to us by using a deployment. Let's get started!
What is a Kubernetes Deployment?
A deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features. In essence, a deployment provides declarative updates to ReplicaSets and Pods.
As per the Kubernetes community, the following are the typical use cases where one would find themselves using a deployment.
- Create a Deployment to rollout a ReplicaSet. The ReplicaSet creates Pods in the background. Check the status of the rollout to see if it succeeds or not.
- Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment. A new ReplicaSet is created and the Deployment manages moving the Pods from the old ReplicaSet to the new one at a controlled rate. Each new ReplicaSet updates the revision of the Deployment.
- Rollback to an earlier Deployment revision if the current state of the Deployment is not stable. Each rollback updates the revision of the Deployment.
- Scale up the Deployment to facilitate more load.
- Pause the Deployment to apply multiple fixes to its PodTemplateSpec and then resume it to start a new rollout.
- Use the status of the Deployment as an indicator that a rollout has stuck.
- Clean up older ReplicaSets that you don't need anymore.
Create a Deployment Manifest file
The following example creates an nginx deployment, which results in 3 replicas of the nginx container.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
In order to deploy this manifest file in your own Kubernetes cluster, use the following command.kubectl apply -f nginx-deployment.yaml
Note: Detailed information on how we can create a Manifest spec can be found here.
Working with Deployments
In order to see the details of the deployment, use the follwing command.
kubectl get deployments -o wide
OUTPUT:
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 3/3 3 3 25s nginx nginx:1.14.2 app=nginx
In order to check the rollout status of the deployment, use the following command.
kubectl rollout status deployment/nginx-deployment
OUTPUT:
deployment "nginx-deployment" successfully rolled out
To see the replicaset that is created by the deployment, use the following command.
kubectl get rs
OUTPUT:
NAME DESIRED CURRENT READY AGE
nginx-deployment-66b6c48dd5 3 3 3 4m2s
Note: Notice that the name of the ReplicaSet is always formatted as
[DEPLOYMENT-NAME]-[RANDOM-STRING]
. The random string is randomly generated and uses the pod-template-hash as a seed.
Another useful command to see details of the deployment:kubectl describe deployments nginx-deployment
In order to delete a deployment use the following command.kubectl delete -f nginx-deployment.yaml
Updating a deployment and rollouts
In order to update a deployment, a change in the pod template in the manifest file is needed. A rollout is triggered only if there is a change in the manifest file that contains the spec for the pod.
Deployment ensures that only a certain number of Pods are down while they are being updated. By default, it ensures that at least 75% of the desired number of Pods are up (25% max unavailable).
Deployment also ensures that only a certain number of Pods are created above the desired number of Pods. By default, it ensures that at most 125% of the desired number of Pods are up (25% max surge).
For example, if you look at the above Deployment closely, you will see that it first created a new Pod, then deleted some old Pods, and created new ones. It does not kill old Pods until a sufficient number of new Pods have come up, and does not create new Pods until a sufficient number of old Pods have been killed. It makes sure that at least 2 Pods are available and that at max 4 Pods in total are available.
Each time a new Deployment is observed by the Deployment controller, a ReplicaSet is created to bring up the desired Pods. If the Deployment is updated, the existing ReplicaSet that controls Pods whose labels match .spec.selector but whose template does not match .spec.template are scaled down. Eventually, the new ReplicaSet is scaled to .spec.replicas and all old ReplicaSets is scaled to 0.
Let us look at an example of updating your deployment:
Say you want to update your nginx version from 1.14.2 to 1.16.1, you can do so by changing the manifest and applying the change, or by using the following command.
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
To see the rollout status, use the following command.
kubectl rollout status deployment/nginx-deployment OUTPUT: Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
- Watch the Deployment, ReplicaSet, and Pods during a rolling update (CTRL-C to exit)
watch kubectl get deploy,rs,pods
Rolling back a deployment
In order to rollback a deployment, use the following steps.
- Check the rollout history of a deployment.
kubectl rollout history deployment/nginx-deployment
. - To see the details of a specific revision.
kubectl rollout history deployment.v1.apps/nginx-deployment --revision=2
- To rollback to a previous version:
kubectl rollout undo deployment.v1.apps/nginx-deployment
- Rollback to a specific revision using
--to-revision
attribute.kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2
Thank you for reading to the end! If you want mode advanced informtion on deployments, please refer to the source here.
Hope this helps! ☺️