> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Deployments

> This lesson covers creating a Kubernetes deployment using an existing ReplicaSet definition to streamline the process.

In this lesson, we will create a Kubernetes deployment by leveraging an existing ReplicaSet definition. This approach helps streamline the process by reusing a familiar structure.

## Step 1: Set Up the Deployment Directory

First, navigate to your project directory and create a new folder called `deployments`. Inside this folder, create a file named `deployment.yaml`.

## Step 2: Review the ReplicaSet Definition

For reference, open the existing ReplicaSet definition on the right side of your split editor:

```yaml theme={null}
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 4
  template:
    metadata:
      name: nginx-2
      labels:
        app: myapp
    spec:
      containers:
      - name: nginx
        image: nginx
```

<Callout icon="lightbulb" color="#1CB2FE">
  Review this ReplicaSet definition carefully, as you will reuse its structure when defining your new deployment.
</Callout>

## Step 3: Create the Deployment Configuration

In your new `deployment.yaml` file, start with the `apiVersion` (apps/v1) as used in the ReplicaSet. Set the kind to `Deployment`, and then define the metadata with a unique name and appropriate labels. In our example, we use the name `myapp-deployment` with labels like `tier: frontend` and `app: nginx`.

For the spec section, copy the structure from the ReplicaSet but modify the configuration to meet the deployment requirements. In this case, we reduce the number of replicas from four to three.

Below is the final configuration for your deployment:

```yaml theme={null}
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    tier: frontend
    app: nginx
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 3
  template:
    metadata:
      name: nginx-2
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx
          image: nginx
```

<Callout icon="lightbulb" color="#1CB2FE">
  The deployment uses the same selector as the ReplicaSet, matching on `app: myapp`. This ensures that the deployment's pods are correctly managed.
</Callout>

## Step 4: Reference the Original ReplicaSet Definition

For comparison, here is the unchanged ReplicaSet definition saved in `replicaset.yaml`:

```yaml theme={null}
# replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 4
  template:
    metadata:
      name: nginx-2
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx
          image: nginx
```

## Step 5: Deploy the Configuration to Your Cluster

Save your deployment configuration and create the deployment by running the following command in your terminal:

```bash theme={null}
kubectl create -f deployment.yaml
```

After creating the deployment, verify it with:

```bash theme={null}
kubectl get deployments
```

The output should resemble:

```bash theme={null}
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deployment    3/3     3            3           10s
```

## Step 6: Validate the Pods

To inspect the pods created by the deployment, execute:

```bash theme={null}
kubectl get pods
```

Expected output:

```bash theme={null}
NAME                     READY   STATUS    RESTARTS   AGE
myapp-replicaset-pjs89   1/1     Running   0          34m
myapp-replicaset-pwv6h   1/1     Running   0          34m
myapp-replicaset-zr6c7   1/1     Running   0          23s
```

For more detailed information about your deployment, use:

```bash theme={null}
kubectl describe deployment myapp-deployment
```

This command provides comprehensive details on metadata, pod specifications, and events. You will see that the deployment uses the same selector (`app: myapp`), ensuring three desired and three available pods are running.

## Step 7: Review All Cluster Objects

Finally, run the following command to list all objects created in the cluster:

```bash theme={null}
kubectl get all
```

A sample output might look like this:

```bash theme={null}
NAME                                   READY   STATUS      RESTARTS   AGE
pod/myapp-replicaset-pjs89             1/1     Running     0          35m
pod/myapp-replicaset-pwv6h             1/1     Running     0          35m
pod/myapp-replicaset-zr6c7             1/1     Running     0          104s

NAME                                   TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
service/kubernetes                     ClusterIP   10.96.0.1     <none>        443/TCP    65m

NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp-deployment       3/3     3            3           105s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-replicaset       3         3         3       35m
```

This output confirms that both the deployment and its corresponding ReplicaSet have been successfully created, with the expected pods up and running.

<Callout icon="lightbulb" color="#1CB2FE">
  You have now successfully deployed your application using Kubernetes Deployments. Experiment with these configurations in your practice environment for a deeper understanding of Kubernetes object management.
</Callout>

## Additional Resources

* [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* [Kubernetes Documentation](https://kubernetes.io/docs/)
* [Docker Hub](https://hub.docker.com/)
* [Terraform Registry](https://registry.terraform.io/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kubernetes-for-the-absolute-beginners-hands-on-tutorial/module/0919dae3-bc94-479f-b205-d52156817c98/lesson/7e954681-d6dc-4997-9c21-d7ed27daff64" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kubernetes-for-the-absolute-beginners-hands-on-tutorial/module/0919dae3-bc94-479f-b205-d52156817c98/lesson/a04dde16-f00b-454b-8314-8e7357b3d7ad" />
</CardGroup>
