Launching your Kubernetes Cluster with Deployment

Launching your Kubernetes Cluster with Deployment

#90daysofdevopschallenge

#day32

Previous_Blog: devunnatig.hashnode.dev/launching-your-firs..

What is Deployment in k8s?

A Deployment provides a configuration for updates for Pods and ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new replicas for scaling or to remove existing Deployments and adopt all their resources with new Deployments.

Why do we require deployments in Kubernetes?

Let's take a scenario, you have a web application that is already deployed on the production environment, and now you need to make some changes to upgrade your application. so if you upgrade your application one by one that process is called Rolling updates.

Suppose one of the upgrades you are performing resulted in an unexpected error and you want to undo the recent change. You would like to be able to roll back the changes that were recently carried out.

Another Scenario is that you don't want to apply all the changes immediately after the command is run. Instead, you would apply a pause to your environment and make the changes after that resume so that all the changes are rolled out together.

So, all the above scenarios are covered in Kubernetes deployment.

Features Provided By K8S deployment:

a) Versioning and History: Deployments keep a revision history of changes, allowing you to track and manage different versions of your application.

b) Pause and Resume: Deployments can be paused and resumed, allowing you to temporarily halt the deployment process. This can be useful for troubleshooting or making manual adjustments.

c) Pod Template Updates: You can modify the pod template in a Deployment to apply changes to the environment in which your application runs. This includes changes to the container image, environment variables, and other pod specifications.

d) Scaling: Deployments make it easy to scale your application by adjusting the number of replicas. You can scale up to handle increased load or scale down during periods of lower demand.

Kubernetes Deployments Command:

a) Create a Deployment:

kubectl create deployment <deployment-name> --image=<container-image>

b) Get Deployments:

kubectl get deployments

c) Describe a Deployment:

kubectl describe deployment <deployment-name>

d) Update a Deployment (Rolling Update):

kubectl set image deployment/<deployment-name> <container-name>=<new-container-image>

e) Rollback a Deployment:

kubectl rollout undo deployment/<deployment-name>

f) Scale a Deployment:

kubectl scale deployment <deployment-name> --replicas=<new-replica-count>

g) Pause/Resume a Deployment:

kubectl rollout pause deployment/<deployment-name> 
kubectl rollout resume deployment/<deployment-name>

h) Rolling Restart (Force Restart):

kubectl rollout restart deployment/<deployment-name>

i) Delete a Deployment:

kubectl delete deployment <deployment-name>

j) Exposing a Deployment via a Service:

kubectl expose deployment <deployment-name> --type=NodePort --port=<port>

❄Tasks:

Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" feature.

By Using Command:

kubectl create deployment todoapp --image=<image-name>
kubectl get deployments
kubectl get pods

By Using yaml file:

Step 1: Create deployment.yaml file.

vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: todoapp
spec:
 replicas: 2
 selector:
   matchLabels:
     app: todo
 template:
   metadata:
     name: todoapp
     labels:
       app: todo
   spec:
    containers:
      - name: todoapp
        image: chanchal8765/django-docker

Step 2: Apply and Verify that deployment is created successfully.

kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get pods

Congratulations! Successfully created your first deployment on Kubernetes Cluster.

In the Next Article, we will go deep down in K8S Namespace.......

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!