Updated April 13, 2023
Introduction to Kubernetes Deployment
In Kubernetes, Deployment is used to provide declarative updates to Pods as well as ReplicaSets. Also, a Deployment Controller is the higher version of a Replication Controller, as it Removes, Adds, Updates Pods in ReplicaSets. We need this to work faster and dynamically when we have ReplicaSets with tens of pods, and we are in need to modify them. By using Kubernetes Deployment, this can be achieved with very little effort if used in a correct way.
What is Kubernetes Deployment?
- Kubernetes Deployment works based on the specification YAML file, and you describe a required state in that. Then Deployment Controller changes the current state of Pods or ReplicaSets to the required state. We can define Deployments to create new ReplicaSets or to delete existing ReplicaSets or many other operations.
- In Kubernetes, Deployment is the recommended way to deploy a Pod or RS. It represents a set of multiple pods which are identical in configuration but have unique identities. Deployment runs multiple replicas of your application, also automatically scales in or out the number of pods.
- Also, when doing scaling, it also ensures that your application is UP at a moment of time by limiting its actions on few pods at a time.
- Deployments are managed by Deployment Controller, which uses a pod template where specifications are mentioned for pods. This specification is in YAML format. In this YAML, you can mention how a Pod or ReplicaSet should look like, what application should run inside containers, which volumes should be mounted inside containers etc.
- When a Deployment change and is applied, then Pods will be automatically created. Older Pods will be removed with controller speed.
How does Kubernetes Deployment Works?
We should know Kubernetes’s way of working before we start using Deployment. Also, to work with Kubernetes Deployment, we need to have a Kubernetes environment which should contain at least below, even when you are doing a lab setup:
- Kubernetes Cluster with Pods.
- Kubectl command line.
- If you have more than one node in your cluster, then networking should be working.
- Kubernetes version should be higher than 9.
Also, for creating a specification YAML file to work with Deployment, it must have these fields known beforehand and should be mentioned in the Deployment specification YAML file.
- apiVersion: To tell the api version.
- kind: This must be Deployment.
- metadata: Under this, you specify details of Deployment.
- name: This specifies the name of Deployment.
- Spec: Fields under this specify the labels to match and a number of replicas to.
- selector: Under this field, you mention which labels to match before doing any scaling.
- selector.matchLabels: These are the labels in key-value pair form.
- replicas: The number of pods to create.
- template: The pod’s template specification, labels to attach to pods, come under.
- spec: Image and version etc., come under this.
- metadata.labels: Labeling of pods in key-value pair.
An example of a Deployment specification YAML file using the above fields is as below, where this Deployment will create 2 replicated pods with label app: nginx. Where container image is nginx, and its version is 1.14.2.
When you apply this file using Kubectl like below:
kubectl apply –f deploy.yaml
Your Deployment will create and command will give you output like below:
Now, if you check the number of pods running, you will see the same number of pods have been created, which you specified in spec.replica field (two in this case).
kubectl get pods
Also, you can wait a few seconds or minutes as per your number of pods to see output like below, where all your pods are ready to use.
kubectl get deployments
When you check, you can see the ReplicaSet is created, the command to check, the output is like below:
kubectl get rs
Check the labels of the pods created, which is the same which you mentioned in Deployment’s YAML file.
kubectl get pods --show-labels
Examples of Kubernetes Deployment
In Kubernetes, when working with Deployment, we can perform many operations. A few of them are listed below. The list is long but not exhaustive.
- To rollout an RS.
- Pause the Deployment to apply multiple fixes in the YAML file and then resume to start a new rollout.
- Removing old Pods or RS that are not needed.
- New State Declaration for pods.
- Rollback to an older version.
- Scale up the Deployment to add more pods for the load.
Now, we can see few examples of working with Deployment, by which you can understand it in more detail:
Example #1 – Creating a Deployment
- Create a Deployment YAML file like below, where you specify that you need 2 pods with redis image and container name start as redis and labeled with “name: redis.”
- Apply this file using Kubectl. The output should be like below:
kubectl apply –f deploy.yaml
- Now when you check the current Pod’s status, you will see Pods starts.
kubectl get pods
- After some time, you can see all running if no issue.
kubectl get pods
- Deployment works as rollout so its status can be checked as:
Example #2 – Updating the Deployment
- For updating an existing Deployment like the one created in the previous example, you need to do the below. Change the Deployment YAML file. In this case, we changed the spec.containers.name only from redis to redis-1.
cat deploy.yaml
- Apply this YAML file using.
kubectl apply –f deploy.yaml
- You will see old pods being terminating slowly and new pods being.
kubectl get pods
- After some time, you get new pods, uniquely identified by the hash value attached to the end of pod’s.
kubectl get pods
- Also, in events, you can see that containers with new names got.
kubectl describe pod redis-deploy-exam-75568b7c7b-pgbf6 | tail
Example #3 – Scaling out the Replica Count
In peak hours, you will get a heavy load, and you need additional pods; deployment does that easily like below:
- Create a Deployment YAML file like below where you have modified the replica field from 2 to.
- Use Kubectl to apply.
kubectl apply –f deploy.yaml
- Check Deployment status. You will find now five pods running under this.
kubectl get deployment
- New pods have been added, check by the AGE.
Example #4 – Deleting a Deployment
When Pods are not needed and so is Deployment, you can delete it simply using Kubectl.
- Get Deployment status and pods running under it.
kubectl get deployments
- Use Kubectl like below to delete the.
kubectl delete deployments redis-deploy-exam
Check the current pod’s status. You will see Pods under termination.
kubectl get pod
Check again, and you will not find any.
kubectl get pod
Conclusion
Kubernetes Deployment is the preferred way to create a replicated application, which in turn uses a ReplicaSet (RS), which then works on Pods under it. Using Deployment efficiently makes your rollouts smooth and super easy. All you need to know is how it works and do good preparation. Features like Rolling Back A Deployment saves you in case of an unsuccessful upgrade and similar scenarios.
Recommended Articles
We hope that this EDUCBA information on “Kubernetes Deployment” was beneficial to you. You can view EDUCBA’s recommended articles for more information.