> ## 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.

# Lab Solution Deploymentoptional

> Explains Kubernetes Deployments, creating and inspecting them, troubleshooting image pull failures, and using kubectl commands to create, scale, and diagnose deployments

In this lesson you'll learn how Kubernetes Deployments work and how to use `kubectl` to inspect, create, and fix them. We cover:

* Inspecting the initial cluster state
* Creating Deployments (manifest and `kubectl` shortcut)
* Diagnosing common pod failures like `ErrImagePull` / `ImagePullBackOff`
* Best practices and quick reference commands

## Initial cluster state — verify there are no resources

Start by confirming the default namespace is empty for Pods, ReplicaSets, and Deployments:

```bash theme={null}
# No Pods
kubectl get pods
# No ReplicaSets
kubectl get rs
# No Deployments
kubectl get deployments
# No resources found in default namespace.
```

This ensures you're working from a clean slate.

## Create a Deployment (frontend-deployment) and inspect controller behavior

When you create a Deployment, the Deployment controller creates a ReplicaSet which in turn creates Pods.

After creating `frontend-deployment`, inspect each resource:

```bash theme={null}
kubectl get deployments
# NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubectl get rs
# NAME                                DESIRED   CURRENT   READY   AGE
kubectl get pods
# NAME                                    READY   STATUS             RESTARTS   AGE
# frontend-deployment-7fd8cdb696-stmbx    0/1     ImagePullBackOff   0          59s
# frontend-deployment-7fd8cdb696-2z6wc    0/1     ErrImagePull       0          59s
# frontend-deployment-7fd8cdb696-jgcbx    0/1     ErrImagePull       0          59s
# frontend-deployment-7fd8cdb696-jbr44    0/1     ErrImagePull       0          59s
```

* Question: How many of the four pods are Ready?
  * Answer: 0/4 are Ready (each pod shows `0/1` in READY).

Inspect one of the pods to see the failure reason:

```bash theme={null}
kubectl describe pod frontend-deployment-7fd8cdb696-stmbx
# ...
# Containers:
#   busybox-container:
#     Image:      busybox888
#     State:      Waiting
#     Reason:     ImagePullBackOff
# ...
```

Explanation: The pods are failing because the container image `busybox888` cannot be found or pulled, causing `ErrImagePull` and `ImagePullBackOff`. Fix the image name in the Deployment to a valid image (for example `busybox`).

## Fixing a manifest and creating a Deployment from a file

Assume you have a manifest `deployment-definition-1.yaml` in `/root`:

```bash theme={null}
pwd
ls
# deployment-definition-1.yaml  sample.yaml
```

Attempting to create the Deployment may fail if the manifest has a typo or wrong casing:

```bash theme={null}
kubectl create -f deployment-definition-1.yaml
# Error from server (BadRequest): error when creating "deployment-definition-1.yaml": deployment in version "v1" cannot be handled as a Deployment: no kind "deployment" is registered for version "apps/v1" in scheme "k8s.io/apimachinery@v1.23.3-k3s1/pkg/runtime/scheme.go:100"
```

<Callout icon="lightbulb" color="#1CB2FE">
  This error commonly occurs due to typos or incorrect casing in the manifest. Kubernetes resource Kind values are case-sensitive — they must match the API (for example, `Deployment` not `deployment`).
</Callout>

A corrected manifest might look like this:

```yaml theme={null}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-1
spec:
  replicas: 2
  selector:
    matchLabels:
      name: busybox-pod
  template:
    metadata:
      labels:
        name: busybox-pod
    spec:
      containers:
      - name: busybox-container
        image: busybox888
        command:
        - sh
        - "-c"
        - echo Hello Kubernetes! && sleep 3600
```

After fixing the manifest (for example, by correcting `kind` casing), create it:

```bash theme={null}
kubectl create -f deployment-definition-1.yaml
# deployment.apps/deployment-1 created
```

Note: This manifest still references `busybox888`, which will likely fail to pull. Expect `ImagePullBackOff` / `ErrImagePull` unless you use a valid image name.

## Create a Deployment using kubectl create (name + image)

The shortcut `kubectl create deployment` simplifies Deployment creation. The `--replicas` flag may or may not be supported depending on your kubectl version. If it’s not supported, create then scale.

The task: create a Deployment named `httpd-frontend` using the `httpd` image with 3 replicas.

Recommended sequence (works in all kubectl versions):

<Callout icon="lightbulb" color="#1CB2FE">
  Example (create then scale):
</Callout>

```bash theme={null}
# Create the Deployment with the specified image
kubectl create deployment httpd-frontend --image=httpd
# Scale to 3 replicas
kubectl scale deployment httpd-frontend --replicas=3
# deployment.apps/httpd-frontend scaled
```

Verify the Deployment is ready:

```bash theme={null}
kubectl get deployments
# NAME               READY   UP-TO-DATE   AVAILABLE   AGE
# httpd-frontend     3/3     3            3           10s
```

## Quick troubleshooting checklist

* If pods show `ErrImagePull` or `ImagePullBackOff`, run:
  * `kubectl describe pod <pod-name>` to see the container image and error reason.
  * `kubectl logs <pod-name>` for container logs (if any).
* Manifests are case-sensitive for `kind` and `apiVersion`.
* Use `kubectl create deployment` + `kubectl scale` or apply a manifest to set replica counts.

## Useful command reference

| Action | Command | Notes |
| - | - | - |
| List Pods | `kubectl get pods` | Shows pod status and READY counts |
| Describe Pod | `kubectl describe pod <pod-name>` | Inspect events and container image pull errors |
| Create from manifest | `kubectl create -f deployment-definition-1.yaml` | Use `apply` to create/update declaratively |
| Create deployment (shortcut) | `kubectl create deployment NAME --image=IMAGE` | Quick creation without full manifest |
| Scale deployment | `kubectl scale deployment NAME --replicas=N` | Alternative when `--replicas` not supported |
| List ReplicaSets | `kubectl get rs` | Shows the ReplicaSet created by a Deployment |

## Summary

* A Deployment manages ReplicaSets, which manage Pods.
* Pods failing to start often indicate image pull problems — verify the image name and registry access.
* Kubernetes manifests are case-sensitive; typos in `kind` or `apiVersion` produce creation errors.
* Use `kubectl create deployment` for quick setups and `kubectl scale` or a manifest to set replica counts.

## Links and references

* [Kubernetes Deployments](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)
* [kubectl Cheat Sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)
* [Troubleshooting Pods](https://kubernetes.io/docs/tasks/debug-application-cluster/debug-pod-replication-controller/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/c6d2ac7d-8192-4cff-aa54-e36d888c5bd9/lesson/bab0855f-cbd7-4a57-909b-a19dec777eba" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.