After completing this lesson, you will be able to:
Identify the benefits of using deployments
Describe the deployment manifest
Apply appropriate deployment strategies depending on the application downtime
Use the deployment history feature to check or roll back on the previous version of the application
Understand how to scale deployments
Deployments, Overview, and Benefits
Introduction
You have heard of Deployments as a high-level abstraction of Pods and ReplicaSets. Now, you want to learn more about Deployments and how to use them to manage your applications.
Overview
We have already introduced Deployments as higher-level abstractions of ReplicaSets. Deployments can be used to define the desired state of your application declaratively. This includes the number of replicas, the container images, and further configuration. Deployments manage ReplicaSets, and ReplicaSets manage Pods.
Benefits of Deployments
Consider the following scenario:
You have manually created a Pod for an application with version 1.0.0. After some time, you want to update the application to version 1.0.1. You would have to create a new Pod based on the new image, update the service to point to the new Pod, and delete the old Pod. This is a lot of manual work and is error-prone. Deployments can be used to automate this process.
This is where Deployments come into play. Deployments can be used to declaratively define the new desired state of your application to roll out the new version reliably and with zero downtime of the currently running application.
Click on the four buttons below to discover the main benefits of Deployments.
The Deployment Manifest
Since a Deployment manages the ReplicaSet, which is, in turn, managing the Pods, the Deployment manifest is very similar to the ReplicaSet manifest. The only difference is that the Deployment manifest has Deployment-specific fields and objects, for example, the strategyobject.
This example shows a Deployment manifest with the name hello-kyma.
The Deployment has three replicas and the Pod template has the label app: hello-kyma. The Pod template has a container with the name hello-kymaand the image hello-kyma:1.0.0 from the GitHub Container Registry (ghcr.io). The container exposes the port: 8080.
Let's have a look at the Deployment manifest's spec section in detail:.
The Deployment spec section in detail
Field
Description
Required
Possible values
replicas
The number of replicas of the application
Yes
Integer
selector
The selector to identify the Pods managed by the Deployment
Yes
At least a subset of the labels defined in the Pod template
template
The template used to create the Pods managed by the Deployment
Yes
Labels and Pod manifest
If you want to pull the image from a private registry, you can do so by providing the credentials in the Deployment manifest's to spec.template.spec:
The ImagePullPolicy is set to IfNotPresent by default. This means that the image is pulled only if it is not present locally on the cluster node. If you want to pull the image always, you can set the ImagePullPolicy to Always.
Note
If you specify an image with the latest tag and you or the system create a new Pod, Kubernetes always downloads the most recent latest version. This can lead to unexpected behavior since other Pods can still use the old image. Therefore, it is recommended to use a specific version of the image, for example, 'hello-kyma:1.0.0'.
For the strategy.type field, you can either specify RollingUpdate or Recreate. Since, the RollingUpdate is the default and recommended strategy, we will focus on this one.
As mentioned, the RollingUpdate doesn't delete the old Pods before creating the new ones. Instead, it performs the update in a rolling fashion. You can define exactly the percentage of Pods that can be updated at the same time. This is done by the maxSurge and maxUnavailable fields:
The maxSurge field defines the maximum number of Pods that can be created above the desired number of Pods.
The maxUnavailable field defines the maximum number of Pods that can be unavailable during the update.
The default values for both fields are 25%. With this, the old Pods still retrieve traffic, while you are already creating and serving the new Pods.
Note
Serving two versions of an application simultaneously, behind the same service, also comes with some challenges. For example, you have to make sure that the new version of the application is compatible with the old one, also known as API Compatibility. It is highly recommended to have a versioning concept for your APIs to be both forward and backward compatible.
Deployment History
Each deployment also contains an entire deployment history. This allows you to roll back to a previous version of your application if something goes wrong.
You can use kubectl to check the deployment history of a deployment:
Code Snippet
1
kubectl rollout history deployment hello-kyma
You can simply roll back to a previous version of your application by using the kubectl rollout undo command:
Code Snippet
1
kubectl rollout undo deployment hello-kyma
Scaling Deployments
You can scale your deployment by simply updating the replicas field in the deployment manifest. Likewise, you can also use kubectl to scale your deployment:
Code Snippet
1
kubectl scale deployment hello-kyma --replicas=5
This will update the underlying ReplicaSet and create two new Pods. You can verify this by using:
Code Snippet
1
kubectl get deployments hello-kyma
The output should look like this:
Summary
This lesson taught you how to deploy your application to Kubernetes based on a Deployment manifest. You also learned about the different deployment strategies and how to scale your deployment and roll it back.