What Is Kubernetes Deployment And How To Use It?

Behdad Kardgar
14 min readApr 8, 2023

--

A Kubernetes Deployment is a resource object in Kubernetes that provides a way to declaratively manage a set of identical pods. Essentially, a Deployment is responsible for managing the deployment and scaling of a set of pod replicas, ensuring that the desired state of the application is maintained at all times.

In simpler terms, a Deployment is a way to automate the process of creating and managing multiple replicas of your application, making it easier to manage, update, and scale your application without worrying about the underlying infrastructure.

For example, let’s say you have a web application that you want to deploy to a Kubernetes cluster. Instead of manually creating and managing multiple replicas of the application, you can use a Deployment to define the desired state of the application, including the number of replicas and the desired version of the container image. The Deployment will then ensure that the desired state is achieved and maintained, automatically creating or deleting replicas as needed.

Deployments also provide a number of other features, such as rolling updates and rollbacks, scaling, and health checks, which we’ll cover in more detail below.

Overall, Deployments are a powerful tool for managing complex applications in a Kubernetes environment, providing a flexible and automated way to manage the lifecycle of your application.

How do Deployments work?

A Kubernetes Deployment works by managing a set of identical replicas of a pod template, ensuring that the desired number of replicas are running at all times. Here’s a step-by-step breakdown of how Deployments work:

  1. You create a Deployment resource in Kubernetes, specifying the desired state of your application, including the number of replicas and the container image to use.
  2. Kubernetes creates a set of identical pod replicas based on the pod template specified in the Deployment.
  3. The Deployment controller monitors the state of the replicas, ensuring that the desired number of replicas are running at all times. If a replica fails or is terminated, the controller will automatically create a new replica to replace it.
  4. If you update the Deployment with a new version of the container image or other configuration changes, the controller will perform a rolling update, gradually replacing the existing replicas with the new ones to ensure minimal downtime.
  5. If there are any issues with the new version of the application, you can perform a rollback to the previous version using the Deployment’s rollback feature.
  6. You can also scale the Deployment up or down to adjust the number of replicas running based on changes in traffic or other factors.

Deployments provide a flexible and automated way to manage the lifecycle of your application in a Kubernetes environment, making it easier to manage updates, scaling, and other aspects of your application without manual intervention.

What are the Benefits of using Deployments?

There are several benefits of using Deployments in Kubernetes. Here are some of the key advantages:

  1. Automatic management of replicas: Deployments automate the management of multiple replicas of your application, ensuring that the desired state is maintained at all times. This eliminates the need for manual intervention and reduces the risk of errors.
  2. Rolling updates and rollbacks: Deployments make it easy to perform rolling updates of your application, gradually replacing old replicas with new ones to minimize downtime. If there are any issues with the new version of the application, you can also perform a rollback to the previous version.
  3. Scaling: Deployments make it easy to scale your application up or down to handle changes in traffic or other factors. You can adjust the number of replicas running based on demand, without having to manually create or delete pods.
  4. Health checks and self-healing: Deployments include built-in health checks that monitor the state of the replicas and automatically replace any replicas that fail or become unresponsive. This helps ensure that your application is always available and responsive to users.
  5. Declarative configuration: Deployments use a declarative configuration model, which means you define the desired state of your application and let Kubernetes handle the details of managing the infrastructure. This simplifies deployment and management, and reduces the risk of configuration errors.

Deployments provide a powerful and flexible way to manage complex applications in a Kubernetes environment, automating many aspects of deployment, scaling, and management. By using Deployments, you can reduce manual intervention, improve application availability and resilience, and simplify the management of your application.

How to create a Deployment?

Here’s a step-by-step guide on how to create a Deployment in Kubernetes:

  1. Define a Pod template: First, you need to define the Pod template that your Deployment will manage. This includes specifying the container image to use, any environment variables, and any other configuration options. You can define the Pod template using a YAML or JSON file.
  2. Create a Deployment resource: Next, you need to create a Deployment resource in Kubernetes, which will manage the replicas of your Pod template. You can create the Deployment using the kubectl command-line tool, specifying the name of the Deployment and the Pod template file.

Here’s an example YAML file that defines a simple Pod template for a web application:

apiVersion: v1
kind: Pod
metadata:
name: my-web-app
spec:
containers:
- name: web
image: my-web-app:latest
ports:
- containerPort: 8080

And here’s an example kubectl command to create a Deployment based on this Pod template:

kubectl create deployment my-web-app --replicas=3 --template=my-web-app.yaml

This will show you the name of the Deployment, the number of replicas, and the status of the Deployment.

Creating a Deployment in Kubernetes is a straightforward process that involves defining a Pod template and creating a Deployment resource to manage the replicas. By using Deployments, you can automate the management of multiple replicas of your application, making it easier to manage and scale your application in a Kubernetes environment.

Updating a Deployment

Updating a Deployment in Kubernetes involves making changes to the Pod template and applying those changes to the running replicas. Here’s a step-by-step guide on how to update a Deployment:

Modify the Pod template: First, you need to modify the Pod template to reflect the changes you want to make. This might involve updating the container image, changing environment variables, or making other configuration changes.

Apply the changes: Once you’ve made the changes to the Pod template, you need to apply those changes to the running Deployment. You can do this using the kubectl command-line tool, specifying the name of the Deployment and the updated Pod template file.

Here’s an example YAML file that updates the Pod template for a web application:

apiVersion: v1
kind: Pod
metadata:
name: my-web-app
spec:
containers:
- name: web
image: my-web-app:v2
ports:
- containerPort: 8080

And here’s an example kubectl command to apply the changes to the Deployment:

kubectl apply -f my-web-app.yaml

This will update the running Deployment with the new Pod template, rolling out the changes to the replicas.

Check the status of the Deployment: Once the update is applied, you can use the kubectl command-line tool to check the status of the replicas and ensure that the Deployment is running as expected. You can use the following command to get information about the Deployment:

kubectl get deployments

This will show you the name of the Deployment, the number of replicas, and the status of the Deployment.

One important thing to note is that when you update a Deployment, Kubernetes uses a rolling update strategy by default. This means that it gradually replaces old replicas with new ones, ensuring that there is always a minimum number of available replicas during the update process. You can customize the rolling update strategy using different parameters and update strategies in your Deployment configuration.

Updating a Deployment in Kubernetes is a straightforward process that involves modifying the Pod template and applying those changes to the running replicas using the kubectl command-line tool. By using rolling updates, Kubernetes ensures that the update process is smooth and minimizes downtime for your application.

Rolling updates and rollbacks

Rolling updates and rollbacks are important concepts related to Deployments in Kubernetes. A rolling update refers to the process of gradually updating a Deployment’s replicas with a new Pod template while ensuring that the application remains available and responsive throughout the update process. A rollback, on the other hand, is the process of reverting to a previous version of the Deployment in case of issues or failures with the current update.

Here’s a more detailed overview of rolling updates and rollbacks in Kubernetes:

Rolling Updates:
By default, when you update a Deployment in Kubernetes, it uses a rolling update strategy to replace the old replicas with new ones. This strategy ensures that a minimum number of replicas are always available during the update process, reducing downtime and ensuring that the application remains available and responsive.

The rolling update process happens in several steps:

Kubernetes creates new replicas with the updated Pod template and gradually scales up the number of new replicas while scaling down the number of old replicas.

Kubernetes monitors the health of the new replicas and ensures that they are ready and available before scaling down the old replicas further.

Once all the new replicas are ready and available, Kubernetes scales down the old replicas to zero, completing the update process.

You can customize the rolling update process using different parameters and update strategies in your Deployment configuration. For example, you can specify the maximum number of unavailable replicas during the update process, the maximum surge in the number of replicas during the update process, and the minimum number of available replicas after the update process.

Rollbacks:
If an update to a Deployment causes issues or failures, you can use the rollback feature in Kubernetes to revert to a previous version of the Deployment. The rollback feature automatically creates a new revision of the Deployment, using the previous Pod template and scaling up the old replicas.

Here’s an example kubectl command to roll back a Deployment to a previous revision:

kubectl rollout undo deployment/my-web-app

This command rolls back the Deployment called “my-web-app” to the previous revision, scaling up the old replicas and scaling down the new replicas. You can also specify a specific revision to roll back to using the — to-revision flag.

Rolling updates and rollbacks are important features of Deployments in Kubernetes that ensure the smooth and reliable operation of your application in a dynamic environment. By using rolling updates, you can update your application while minimizing downtime and ensuring that the application remains available and responsive. And in case of issues or failures, rollbacks allow you to quickly and easily revert to a previous version of the Deployment.

Scaling a Deployment

Scaling a Deployment in Kubernetes refers to increasing or decreasing the number of replicas of a Deployment to handle changes in traffic or resource demands. Kubernetes provides two types of scaling: horizontal scaling and vertical scaling.

Horizontal Scaling:
Horizontal scaling involves increasing or decreasing the number of replicas of a Deployment based on the traffic or resource demands of your application. For example, if your application experiences a sudden increase in traffic, you can use horizontal scaling to add more replicas to the Deployment to handle the additional traffic.

To scale a Deployment horizontally, you can use the kubectl scale command and specify the number of replicas you want to add or remove. For example, the following command scales up a Deployment called “my-web-app” to five replicas:

kubectl scale deployment/my-web-app --replicas=5

Vertical Scaling: Vertical scaling involves increasing or decreasing the amount of resources allocated to each Pod in a Deployment. For example, you can increase the CPU or memory limit of a Pod to handle increased traffic or resource demands.

To scale a Deployment vertically, you can update the Pod template in your Deployment configuration and specify the new resource limits. For example, the following YAML snippet increases the CPU limit of a Pod in a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-app
spec:
replicas: 3
template:
spec:
containers:
- name: my-web-app
image: my-web-app:latest
resources:
limits:
cpu: "2"
memory: "4Gi"

By default, Kubernetes uses horizontal scaling to handle changes in traffic or resource demands. However, you can also use a combination of horizontal and vertical scaling to optimize the performance and resource utilization of your application.

Scaling a Deployment in Kubernetes is a crucial feature that allows you to handle changes in traffic or resource demands and ensure that your application remains available and responsive. By using horizontal and vertical scaling, you can adjust the number of replicas and resource allocation of your application to meet changing needs and optimize resource utilization.

Health checks and self-healing

Health checks and self-healing are important features of Kubernetes Deployments that help to ensure the availability and reliability of applications running in Kubernetes clusters.

Health Checks: A health check is a mechanism that Kubernetes uses to determine whether a Pod in a Deployment is running properly. Kubernetes provides two types of health checks: liveness probes and readiness probes.

A liveness probe is used to determine whether a Pod is running properly. If a liveness probe fails, Kubernetes will restart the Pod. A readiness probe is used to determine whether a Pod is ready to receive traffic. If a readiness probe fails, Kubernetes will stop sending traffic to the Pod until it is ready again.

To configure a health check for a Pod in a Deployment, you can add a livenessProbe and/or a readinessProbe section to the Pod's container specification in the Deployment configuration file. For example, the following YAML snippet shows how to configure a liveness probe for a Pod running a web server:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-app
spec:
replicas: 3
template:
spec:
containers:
- name: my-web-app
image: my-web-app:latest
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /healthz
port: 80

This configuration specifies that Kubernetes should send an HTTP GET request to the /healthz endpoint on port 80 of the container to determine whether it is running properly.

Self-Healing: Self-healing is the ability of Kubernetes to automatically detect and recover from failures in a Deployment. If a Pod in a Deployment fails or becomes unresponsive, Kubernetes will automatically restart the Pod or create a new replica to replace the failed Pod.

In addition to restarting failed Pods, Kubernetes also provides other self-healing mechanisms, such as node failure detection and automatic rescheduling of Pods to healthy nodes.

Health checks and self-healing are important features of Kubernetes Deployments that help to ensure the availability and reliability of applications running in Kubernetes clusters. By configuring health checks and relying on Kubernetes’ self-healing mechanisms, you can minimize downtime and ensure that your applications are always running smoothly.

Deployment strategies

Deployment strategies are techniques that are used to manage the process of deploying new versions of an application to a Kubernetes cluster. There are several deployment strategies that can be used in Kubernetes, each with its own advantages and disadvantages. Some of the most commonly used deployment strategies include:

Rolling Update Deployment Strategy:
The Rolling Update deployment strategy is the default strategy in Kubernetes. In this strategy, the new version of the application is gradually rolled out to the cluster by updating one or a few Pods at a time, while keeping the rest of the Pods running the old version. This allows for a gradual and controlled rollout of new versions and minimizes downtime.

Recreate Deployment Strategy: The Recreate deployment strategy in Kubernetes is one of the simplest strategies for deploying a new version of an application. With the Recreate strategy, all the existing replicas of the previous version are terminated and replaced with new replicas of the new version.

The Recreate strategy is a good choice when you don’t need to worry about downtime or if you’re deploying to a low-traffic environment. This strategy involves taking down the entire application while the new version is being deployed, which can result in downtime for users.

To use the Recreate strategy, you need to create a new deployment with the updated version of your application, just like you would with any other deployment strategy. When you apply the new deployment configuration, Kubernetes will terminate all the replicas of the old version of the application and then create new replicas of the new version.

One advantage of the Recreate strategy is that it is easy to implement and requires no additional configuration or setup. However, it does have some drawbacks, including potential downtime for users and the risk of losing data or stateful information stored in the old replicas.

Blue/Green Deployment Strategy:
In a Blue/Green deployment strategy, two identical environments (Blue and Green) are set up in the cluster, with one environment running the current version of the application (Blue) and the other environment running the new version (Green). Once the new version has been deployed to the Green environment and tested, traffic is routed to the Green environment while the Blue environment is decommissioned.

Canary Deployment Strategy:
The Canary deployment strategy is used to test new versions of an application in a small subset of the production environment before rolling out the new version to the entire cluster. In this strategy, a small percentage of traffic is redirected to the new version while the majority of traffic continues to be served by the old version. If the new version performs well, the percentage of traffic served by the new version can be gradually increased until it is serving all traffic.

A/B Testing Deployment Strategy:
The A/B testing deployment strategy is similar to the Canary strategy, but instead of redirecting traffic to the new version, traffic is split between two different versions of the application (A and B). This allows for comparison of the two versions and testing of new features before deciding whether to roll out the new version to the entire cluster.

To implement these deployment strategies, Kubernetes provides features such as labels, selectors, and services that can be used to control the routing of traffic between different versions of the application. For example, a service can be created to route traffic to the current version of the application, and this service can be updated to route traffic to the new version once it has been tested and approved.

Deployment strategies are important tools for managing the deployment of new versions of an application to a Kubernetes cluster. By choosing the appropriate deployment strategy, you can minimize downtime, reduce the risk of errors, and ensure a smooth rollout of new versions.

Summary

Kubernetes Deployments are a critical component of managing and deploying applications in a Kubernetes cluster. Deployments provide a declarative way to manage the deployment and scaling of an application by defining a desired state and letting Kubernetes manage the underlying resources to ensure that state is always maintained.

Deployments provide many benefits, including automatic rollouts and rollbacks, scaling of applications, automatic self-healing, and support for various deployment strategies.

To create a Deployment, you define a deployment configuration file that specifies the desired state of the application, including the number of replicas, the container image, and other configuration options. Once created, Kubernetes will manage the creation and scaling of the Pods needed to meet the desired state.

Updating a Deployment is done by creating a new deployment configuration with the updated settings and applying it to the cluster. Kubernetes will automatically perform a rolling update to update the Pods, minimizing downtime and ensuring availability.

Rolling updates and rollbacks are critical features of Deployments, allowing you to update and revert changes with minimal disruption to the application. Scaling a Deployment is also straightforward, as you can simply adjust the number of replicas to meet changing demands.

Health checks and self-healing are also an essential feature of Deployments. Kubernetes provides mechanisms for monitoring the health of applications and automatically performing self-healing actions, such as restarting failed Pods or replacing them with new ones.

Finally, Deployment strategies provide different ways to manage the deployment and testing of new versions of an application to minimize downtime and reduce the risk of errors. Kubernetes supports several deployment strategies, including Rolling Update, Blue/Green, Canary, and A/B Testing.

Overall, Kubernetes Deployments provide a powerful and flexible way to manage the deployment and scaling of applications in a Kubernetes cluster, with support for automatic updates, scaling, self-healing, and deployment strategies.

--

--