A kubernetes deployment is a kubernetes resource that manages a replicaset which in its turn manages a set of pods. Those pods contain running docker containers.
Kubernates deployment use cases:
- upgrade the docker images seamlessly : if we upgrade them at once, we will have some downtime of the app. So we need to upgrade them one by one. This is called rolling update.
- Rollback changes : if you make an upgrade to your app and you make some regression. You can rollback your modification seemlessly.
In production environment, we will not be creating a pod
definition file or a replicaset definition file.
But, we will be creating directly a deployment definition file.
If you don't know what is a pod or a replicaset, please feel free to check the correspondant tutorials.
Kubernetes deployment desfinition file example:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: tier: backend spec: replicas: 4 template: metadata: name: nginx-pod labels: tier: backend spec: containers: - name: nginx image: nginx selector: matchLabels: tier: backend
Here the deployment has as name nginx-deployment. It creates a replicaset that creates in its turn 4 pods (replicas = 4) based on the nginx image. The created pods have as label tier: backend and the deployment is configured to manage those pods: the selector is configured to manage the pods that have as label tier:backend.
Kubernetes Deployment : rollout and rollback
When a new deployment is created, a new rollout is created
with version 1 (a rollout is the process of deployment of new containers).
When the container is upgraded, a new Rollout with new
revision is created which is version 2.
This enables us to rollback to a previous version of the
deployment if necessary.
You can see the status of the Rollout using this command :
kubectl rollout status deployment <deployment_name>
To see the revisions and the history of the rollout, we run :
kubectl rollout history deployment <deployment_name>
Kubernetes deployment strategies
There are two types of deployment strategies :
- Destroy all the old containers which are running the old version of the app and deploying new ones running the new version of the app : this is called the recreate strategy and this is not the default strategy -> it incurs a down time of the app
- Destroy the old containers one by one and create new container once an old container is destroyed : this strategy is called Rolling update and its is seamless because there is no down time of the app
In order to update a deployment, we make the necessary
changes in the deployment description file (changing the container image
version, changing the deployment name, etc). Then we run this command :
kubectl apply -f deployment-description-file.yml
Here, i will modify the deployment description file above in order to run nginx:1.19-alpine image instead of nginx:latest image. The deployment definition file becomes like this:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: tier: backend spec: replicas: 4 template: metadata: name: nginx-pod labels: tier: backend spec: containers: - name: nginx image: nginx:1.19-alpine selector: matchLabels: tier: backend
Here, i kept the same name of the deployment in order to update the present deployment.
Otherwise, a new deployment will be created.
When, we upgrade the application, a new replicaset is
created and the pods of the old replicaset are destroyed one by one. Once an
old pod is destroyed, a new one is created in the new replicaset.
We notice that the old replicaset has zero pods running and the new replicaset has four pods running.
In order to rollback our changes to the previous version, we run this command :
kubectl rollout undo deployment <deployment_name>
This command destroys the new pods in the new replicaset and creates a pods with the previous version of the app in the old replicaset. Once, a new po dis destroyed, an old pod is created in the old replicaset.
Here we arrived at the end of the kubernetes deployment tutorial.
In order to check our kubernetes tutorial, please like our howto program facebook page and subscribe to our newsletter to get our latest tutorials.
Post a Comment