K8s Hands-On: Deployments
Tags: beginners, kubernetes, tutorial, handson
Published: 2023-10-04 05:12:12

**Introduction**
Hi, I am Akshay Rao. This series will help you in hands on experience.
**Pre-requisites**
We have done with service and multi-container pod.
https://dev.to/aksrao1998/k8s-hands-on-multi-container-pod-and-service-4kih
**Agenda**
In this blog we will learn about the deployment with 5W2H framework. this blog is a bit theory, because i think in real world we will not creating the pods, deployment will do it for us and while building a project deployment is important.

**Let's Start**

**Theory**

**What is Kubernetes Deployment?**
A Kubernetes Deployment is a resource object in Kubernetes that provides declarative updates to applications. It allows you to define an application's desired state and then automatically manages the deployment, scaling, and updating of the application to match that desired state.

**Why use Kubernetes Deployments?**
Deployments are essential for ensuring the reliability and availability of containerized applications. They provide the following benefits:

- Rolling Updates: Deployments enable rolling updates, allowing you to change application versions without downtime.
- Automatic Rollbacks: In case of errors or failures, Deployments can automatically roll back to the previous version.
- Scaling: They allow you to scale your application up or down based on demand.
- Self-healing: If a pod fails, Deployments automatically replace it to maintain the desired state.
- Replication: You can specify the desired number of replicas, ensuring high availability.

**When should you use Kubernetes Deployments?**
Kubernetes Deployments are used when you want to manage the lifecycle of your containerized applications, ensuring they run reliably and are easy to update. You should use them whenever you have stateless applications that can be horizontally scaled.

**Where can Kubernetes Deployments be used?**
Kubernetes Deployments can be used in any Kubernetes cluster, whether it's on-premises or in the cloud. They are designed to work in various environments.

**How do you create and manage Kubernetes Deployments?**
To create and manage a Kubernetes Deployment, you typically follow these steps:
Define a Deployment: Create a YAML file describing the desired state of your application, including the container image, replicas, and update strategy.

- Apply the Deployment: Use the kubectl apply command to create or update the Deployment in your Kubernetes cluster.
- Monitor and Scale: Use kubectl or Kubernetes dashboard to monitor the Deployment's status and scale it as needed.
- Update the Application: To update the application, you modify the Deployment YAML to specify a new container image version and reapply it.

**Hands-On**
create a basic deployment
```
[Downloads (⎈|minikube:hands-on)]$ kubectl create deployment pod1 --image=nginx
deployment.apps/pod1 created

[Downloads (⎈|minikube:hands-on)]$ kubectl get deploy
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
pod1   1/1     1            1           5s

[Downloads (⎈|minikube:hands-on)]$ kubectl get po
NAME                    READY   STATUS    RESTARTS   AGE
pod1-6f7b75cbfc-6pnmv   1/1     Running   0          2m46s
```
lets describe it
```
[Downloads (⎈|minikube:hands-on)]$ kubectl describe deploy pod1
Name:                   pod1
Namespace:              hands-on
CreationTimestamp:      Wed, 04 Oct 2023 13:58:21 +0900
Labels:                 app=pod1
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=pod1
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=pod1
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   pod1-6f7b75cbfc (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  84s   deployment-controller  Scaled up replica set pod1-6f7b75cbfc to 1
```
**change the replicas from 1 to 2**
- edit the deployments in the cluster(it is not bets practices to edit the k8s resource in the cluster but it is a good tool to know as it can come in handy)
```
[Downloads (⎈|minikube:hands-on)]$ kubectl edit deploy pod1

[Downloads (⎈|minikube:hands-on)]$ kubectl get po
NAME                    READY   STATUS    RESTARTS   AGE
pod1-6f7b75cbfc-6pnmv   1/1     Running   0          8m47s
pod1-6f7b75cbfc-r86bb   1/1     Running   0          57s
```
- now we have two pods of same configuration, we did not even create new yaml file or anything.
- Deployments are very important in the real world scenario, i always work with deployments with helm chart in production.

To understand more on the deployments, refer this blog
https://dev.to/aksrao1998/how-does-a-pod-get-launched-when-deployment-is-applied-2m9g

**Thank you**