K8S definition: The Deployment
- The pod is basic; the Deployment manages the deployment strategy.
- It controls ReplicaSets and updates.
- Operations and example.
- Keep the YAML manifests consistent with the actual state of the cluster.
Introduction
A Deployment is an object in the kubernetes API. Its "shortname" is deploy, and its full description can be found here.
Principles
A pod can be run as-is and allows you to declare one or more containers. However, it remains a basic object whose deployment strategy must be managed.
This is precisely the purpose of the Deployment object. As its name suggests, it allows you to manage how you want your pod to be deployed (but not only that).
It is an extremely powerful object that has gradually replaced ReplicaSets, which, although still present, are increasingly used directly but rather exploited through Deployments (a Deployment creating ReplicaSets).
The Deployment is declared in a YAML file and will require a pod template section. You will tell the Deployment what to do with its pod template: how many instances of this template you want, and how to manage its upgrades.
Here is an example of a Deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-demodeployment-defaut
namespace: dev-demodeployment-lan
labels:
environment: dev
network: lan
application: demodeployment
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
environment: dev
network: lan
application: demodeployment
template:
metadata:
labels:
environment: dev
network: lan
application: demodeployment
spec:
nodeSelector:
network: lan
containers:
- name: demo-nginx-deployment
image: nginx:1.27
If we look at it closely, we can observe several things, including the desired number of replicas for the pod declared in the pod template section.
replicas: 3
To meet this requirement, the Kubernetes controller will use the parameters present in the selector section.
selector:
matchLabels:
environment: dev
network: lan
application: demodeployment
Kubernetes is based on the concept of labels. In our example, each pod has the following labels:
- environment: dev
- network: lan
- application: demodeployment
is considered as 1 replica.
This means that the label(s) you use as a selector must be present in the metadata of the pod declared in the template section; otherwise, you risk having a problem.
This model also allows you to attach previously launched pods to a Deployment. Imagine that you have deployed a pod directly, without going through a Deployment object. If you decide to have multiple instances of the same pod and eventually use a Deployment, if your running pod already has the necessary labels, it will be counted as an existing replica. For example, if you configure your Deployment with 3 replicas and a pod already exists with the correct labels, Kubernetes will only instantiate two new pods.
Note that the Selector field allows for multiple selection conditions that can be based on combined logic (and/or...).

Click the image to enlarge.
Operations and example
Once your deployment is written, it is very easy to launch it using the simple command:
kubectl apply -f mon-deployment.yaml

Click the image to enlarge.
You can list your deployments with the following command:
kubectl get deployment

Click the image to enlarge.
In our example, if we request the list of pods, we can see 3 instances of our application.

Click the image to enlarge.
You can also monitor the status of your deployments with the following command:
kubectl rollout status deployment mon-deployment

Click the image to enlarge.
Lifecycle management
A Deployment is not limited to managing the number of replicas (by creating objects such as ReplicaSet); it also allows you to manage how you will update your applications.
Imagine that you have deployed an application based on the NGINX web server version 1.25 with 3 replicas, and you want to switch to version 1.26. There are two possibilities:
- Use a Recreate strategy: in this case, you want to stop your 3 pods running v1.25 at the same time to redeploy them as quickly as possible in v1.26. This is efficient, but you will experience a service interruption because all replicas (and therefore your pods) will be stopped simultaneously.
- Use a RollingUpdate strategy: in this case, you want to proceed with the version upgrade, pod by pod. First, the first pod is shut down and switched to v1.26; once it is up, you move on to the second, then the third. This takes longer, but you will always have at least one active pod during the entire update process, so there will be no service interruption. In fact, if you do not specify anything in your deployment file, Kubernetes will apply the RollingUpdate method by default.
The Deployment object allows you to control the lifecycle strategy of your applications with much greater precision.
By adding a 'strategy' section to your YAML file, you can specify your requirements. For example, you might choose not to use the RollingUpdate method, but instead use Recreate.
spec:
replicas: 3
strategy:
type: Recreate
Alternatively, you can keep the RollingUpdate method, but ask Kubernetes not to create more than a certain number of pods at once (maxSurge), or conversely, always ensure that a certain number of pods are active during deployment (maxUnavailable) (these two values can be specified as a percentage or a number).
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
This is just the tip of the iceberg, because by combining other objects, options, and add-ons, we can achieve even more sophisticated deployment strategies, such as the Canary Release pattern, which involves deploying the new version of your application to a limited number of users. This allows for a gradual increase in load, giving you time to test the new revision of your development.
Update and rollback
To upgrade to v1.26, you can reuse the YAML example from the beginning of this article and simply modify the image version in the pod template section.

Click the image to enlarge.
Then, run the command again:
kubectl apply -f mon-deployment.yaml

Click the image to enlarge.
Because the file has been modified, you will see the output configured, indicating that Kubernetes has detected the change and has updated the deployment by redeploying the associated pods.
Conversely, if you type the command again: kubectl apply -f mon-deployment.yaml
Without making any changes, the output will be unchanged.
You can track the history of your deployments using the following command:
Kubectl rollout history mon-deployment

Click the image to enlarge.
If you want to upgrade to v1.27 now, you can perform the same operation by updating the version of your image in your YAML file, and then apply the change again.
kubectl apply -f mon-deployment.yaml
This time, you can complete your previous command with:
Kubectl annotate deployment mon-deployment kubernetes.io/change-cause="info sur le changement"

Click the image to enlarge.
In this case, not only will you find the iteration of your change in the history of your deployments, but it will also be associated with a comment providing more information about that change.
Kubectl rollout history mon-deployment

Click the image to enlarge.
Note that the annotate command only applies to the last deployment.
However, you can make multiple changes and indicate what has been modified in each annotation.
This way, you can track your deployments.

Click the image to enlarge.
Let's go a little further. Imagine that an incident occurs as a result of one of the updates.
It is urgent to return to the previous state as quickly as possible. In this case, you can use the following command:
kubectl rollout undo deployment mon-deployment

Click the image to enlarge.
This will revert you to your "previous version".
Let's continue with the hypotheses, assuming that you have made several consecutive updates and have gotten lost along the way. Nothing prevents you from reverting to a specific version of your deployment using the command:
kubectl rollout undo deployment mon-deployment --to-revision=1

Click the image to enlarge.
In our example, you could edit the YAML file each time to indicate the version of the container image to use. In more complex cases where you have multiple containers in your pod, and you may have modified things other than just the image version (commands, exposed ports, metadata, etc.), or if you haven't "versioned" your file, the command kubectl rollout can save your life.
Not to mention that once you have reverted to your revision, you can use the command:
kubectl get deployment mon-deployment -o yaml

Click the image to enlarge.
To retrieve the YAML corresponding to your current deployment. This is very useful for rebuilding your YAML if it has been lost. (Note that this trick works with practically all objects).
This is also something to keep in mind. You can interact with Kubernetes through YAML files or directly via commands, or even edit certain parameters using the command kubectl edit.
When you manipulate a Kubernetes object directly in the CLI without going through a YAML file, you are opting for an imperative approach; you give your instructions one by one. Conversely, when you use a YAML file, you opt for a declarative approach where you describe the entire object.
It is recommended to follow a declarative logic, but above all, you should avoid mixing the two strategies for the same object, as this could create an inconsistent state or break any links it may have with other objects.
Prefer using an imperative approach for debugging or testing purposes.
If you urgently need to revert to a version of your deployment, but for some reason the last YAML file used does not reflect this change, and if one of your colleagues relies on that file to launch a deployment after you, it is possible that you and your colleague may ultimately have different views of the version to be deployed.
That is why, in the previous example, once you have reverted to a specific version using the Kubectl rollout command, and the issue has been resolved, take the time to update the YAML file (remember to use the -o yaml option with the kubectl get command if necessary) and then proceed with a declarative deployment declaration.
Conclusion
The Deployment object is one of the most powerful and interesting objects in the Kubernetes API. I encourage you to avoid using pod objects directly, except for testing or debugging purposes; always wrap your pods within a Deployment.