# Kubernetes Deployments

A Kubernetes Deployment is a crucial resource object that provides a declarative approach to managing application updates, rollbacks, and scaling. Deployments allow you to define an application's lifecycle, ensuring your desired state is always maintained by controlling the creation and replacement of Pods.

**<mark>Step 1: Creating Your First Deployment</mark>**

Let’s begin by setting up a simple Deployment that runs an Nginx web server.

#### **Write the Deployment YAML Configuration**

First, create a YAML file named `deployment.yaml` with the following content:

```plaintext
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels: 
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17
        ports:
        - containerPort: 80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278029376/5531a3f9-c697-43a3-b498-eb42433fab09.png align="center")

#### **Apply the Deployment**

To create the Deployment, run:

```plaintext
kubectl apply -f deployment.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724277275020/8eee057d-d509-4288-95e6-10e04bee0cf0.png align="center")

Once applied, you can verify the status of your Deployment, ReplicaSets, and Pods:

```plaintext
kubectl get deploy
kubectl get rs
kubectl get pod
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724277698183/a75bd142-f979-4554-b82e-2e8a2663ac71.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724277750652/f829a907-5ec5-4334-b1dd-cc98bd2f8da1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724277782211/cbc7b8d7-36aa-4c16-8a72-e2407c5999a8.png align="center")

To check the current Nginx version running in a specific Pod, use:

```plaintext
kubectl describe pod <podname>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724277911734/8ae3d8aa-0f0f-4d30-945b-887bfa19ef00.png align="center")

**<mark>Step 2 : Updating the Deployment</mark>**

With your Deployment up and running, let's update the Nginx version from `nginx:1.17` to `nginx:1.18`.

Open the `deployment.yaml` file and update the Nginx image tag:

```plaintext
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels: 
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.18
        ports:
        - containerPort: 80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278138053/e52be7c1-cad6-41b3-89f3-0c4f03f90bd4.png align="center")

Apply the updated Deployment:

```plaintext
kubectl apply -f deployment.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278216068/a41fe19e-e778-498f-9e45-0cd3795d885f.png align="center")

Verify the updated Deployment and Pods:

```plaintext
kubectl get deploy
kubectl get pod
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278377499/5b5163ee-5059-4a86-98ac-473d39f1545f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278405864/867a9e35-9e9e-4a41-8dab-bdee658170aa.png align="center")

To confirm the Nginx version has been updated:

```plaintext
kubectl describe pod <podname>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278483455/43da988f-b7d4-4602-a8b8-02f37cc79535.png align="center")

**<mark>Step 3 : Rolling Back the Deployment</mark>**

Kubernetes makes it easy to roll back to a previous version of your Deployment.

#### **Listing ReplicaSets:**

Identify the ReplicaSet associated with the version you want to roll back to:

```plaintext
kubectl get rs
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278750726/9108610b-5251-47a3-a45e-d2fd02bec87c.png align="center")

#### **Performing the Rollback**

To roll back to the previous version:

```plaintext
kubectl rollout undo deployment/my-deployment
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278800900/52096ad7-3a36-40ee-b5a1-b83599b913ff.png align="center")

Verify the rollback by checking the ReplicaSets and Pods:

```plaintext
kubectl get rs
kubectl get pod
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278889149/e5c064e1-7653-4372-9947-0f3bc719bdf1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724278910488/9109397c-8114-415c-b57f-244119aa4755.png align="center")

Check the Nginx version to ensure the rollback was successful:

```plaintext
kubectl describe pod <podname>
```

Now, Check the nginx version,

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279042343/84f5f96f-dc80-4e4c-97aa-db5ad3f34d54.png align="center")

**<mark>Step 4 : Clean up</mark>**

Once you're done, you can clean up the resources:

```plaintext
kubectl delete deploy nginx-deployment
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279134770/952a68f6-577c-4d3a-a38d-0e94992364e9.png align="center")

### **Conclusion**

Kubernetes Deployments provide a powerful way to manage the lifecycle of your applications. With the ability to easily update, roll back, and scale your applications, Deployments are an essential tool for any Kubernetes user
