> ## 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 Imperative Commands optional

> Hands on lab practicing imperative kubectl commands to create, expose, and inspect Kubernetes Pods, Services, Deployments, and namespaces with verification steps

This hands-on lab practices imperative `kubectl` commands to create and inspect Kubernetes objects. These commands are useful for quick tasks and are commonly asked about in certification exams. Work through each step in order, run the commands shown, and use the verification steps to confirm resources are created and configured correctly.

Helpful links and references:

* [Kubernetes kubectl overview](https://kubernetes.io/docs/reference/kubectl/overview/)
* [kubectl run](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run)
* [kubectl expose](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose)
* [kubectl create deployment](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create)

***

## 1) Deploy a Pod named `nginx-pod` using the `nginx:alpine` image

Create a simple standalone Pod with `kubectl run`:

```bash theme={null}
kubectl run nginx-pod --image=nginx:alpine
```

Expected output:

```bash theme={null}
pod/nginx-pod created
```

Verify the Pod exists:

```bash theme={null}
kubectl get pod nginx-pod
```

You can inspect the Pod YAML to confirm image and other spec details:

```bash theme={null}
kubectl get pod nginx-pod -o yaml
```

***

## 2) Deploy a Redis Pod using the `redis:alpine` image with label `tier=db`

Add labels during creation with `--labels`:

```bash theme={null}
kubectl run redis --image=redis:alpine --labels="tier=db"
```

Expected output:

```bash theme={null}
pod/redis created
```

Verify the label is present:

```bash theme={null}
kubectl get pod redis --show-labels
# or filter by label:
kubectl get pod -l tier=db
```

Inspect YAML to see the label in the Pod metadata:

```bash theme={null}
kubectl get pod redis -o yaml
```

***

## 3) Create a ClusterIP Service named `redis-service` to expose Redis on port 6379

There are two primary imperative approaches for creating Services:

* `kubectl create service clusterip` — useful when you need explicit settings like `--tcp` mappings or a custom `--clusterip`. It does not infer selectors from pods automatically.
* `kubectl expose` — creates a Service based on an existing resource (pod, deployment, etc.) and infers selectors from that object's labels. This is usually the preferred approach when exposing an existing labeled object.

<Callout icon="lightbulb" color="#1CB2FE">
  Use `kubectl expose` when you want the Service to automatically use the source object's labels as the selector. Use `kubectl create service` when you need explicit control (for example, custom `--clusterip` or `--node-port`).
</Callout>

Because the `redis` Pod has the label `tier=db`, expose it as a ClusterIP Service named `redis-service` on port `6379`:

```bash theme={null}
kubectl expose pod redis --port=6379 --name=redis-service
```

Expected output:

```bash theme={null}
service/redis-service exposed
```

Verify the Service:

```bash theme={null}
kubectl get svc redis-service
kubectl describe svc redis-service
```

Example `kubectl get svc` output:

```text theme={null}
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
redis-service   ClusterIP   10.43.56.187    <none>        6379/TCP   12s
```

Example `kubectl describe svc redis-service` (relevant fields):

```text theme={null}
Selector:       tier=db
Type:           ClusterIP
IP:             10.43.56.187
Port:           <unset>  6379/TCP
TargetPort:     6379/TCP
Endpoints:      10.42.0.10:6379
```

***

## 4) Create a Deployment named `webapp` with 3 replicas

Use `kubectl create deployment` and set the number of replicas:

```bash theme={null}
kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3
```

Expected output:

```bash theme={null}
deployment.apps/webapp created
```

Verify the Deployment and its Pods:

```bash theme={null}
kubectl get deployment webapp
kubectl get pods -l app=webapp
```

Example `kubectl get deployment` output:

```text theme={null}
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
webapp  0/3     3            0           17s
# Wait a few seconds and re-run; then it should show READY 3/3
```

Tip: If the Deployment doesn't become fully available immediately, give the cluster a few seconds and re-run `kubectl get pods` to see the rollout progress.

***

## 5) Create a Pod `custom-nginx` using the `nginx` image and set the container port to `8080`

Set the container's port with `--port` (this populates `containerPort` in the Pod spec):

```bash theme={null}
kubectl run custom-nginx --image=nginx --port=8080
```

Expected output:

```bash theme={null}
pod/custom-nginx created
```

Verify the port is set in the Pod spec:

```bash theme={null}
kubectl get pod custom-nginx -o yaml
# Look for spec.containers[0].ports[0].containerPort: 8080
```

***

## 6) Create a new namespace `devns`

Create and verify the namespace:

```bash theme={null}
kubectl create namespace devns
```

Expected output:

```bash theme={null}
namespace/devns created
```

Verify:

```bash theme={null}
kubectl get ns devns
```

<Callout icon="lightbulb" color="#1CB2FE">
  When running commands in a specific namespace, either include `-n <namespace>` or set the namespace in your current context with `kubectl config set-context --current --namespace=<namespace>`.
</Callout>

***

## 7) Create a Deployment `redis-deploy` in namespace `devns` with 2 replicas

Create the Deployment in the `devns` namespace and set replicas to 2:

```bash theme={null}
kubectl create deployment redis-deploy --image=redis --replicas=2 -n devns
```

Expected output:

```bash theme={null}
deployment.apps/redis-deploy created
```

Verify the Deployment and Pods in the namespace:

```bash theme={null}
kubectl get deployment -n devns
kubectl get pods -n devns
```

Example output:

```text theme={null}
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
redis-deploy   2/2     2            2           12s
```

***

## 8) Create a Pod `httpd` (image: `httpd:alpine`) and a ClusterIP Service of the same name targeting port 80, using the fewest steps

Create both the Pod and a ClusterIP Service in one command by using `--expose` with `kubectl run`. `--expose` creates a ClusterIP service by default and requires `--port`:

```bash theme={null}
kubectl run httpd --image=httpd:alpine --expose --port=80
```

Expected output:

```bash theme={null}
pod/httpd created
service/httpd exposed
```

Verify:

```bash theme={null}
kubectl get pod httpd
kubectl get svc httpd
kubectl describe svc httpd
```

Example `kubectl describe svc httpd` (relevant fields):

```text theme={null}
Name:           httpd
Selector:       run=httpd
Type:           ClusterIP
IP:             10.43.x.y
Port:           <unset>  80/TCP
TargetPort:     80/TCP
Endpoints:      10.42.x.y:80
```

***

## Summary command reference

| Task | Command (create) | Verification |
| - | - | - |
| 1. Create nginx Pod | `kubectl run nginx-pod --image=nginx:alpine` | `kubectl get pod nginx-pod` |
| 2. Create redis Pod with label | `kubectl run redis --image=redis:alpine --labels="tier=db"` | `kubectl get pod redis --show-labels` |
| 3. Expose redis as ClusterIP | `kubectl expose pod redis --port=6379 --name=redis-service` | `kubectl get svc redis-service` / `kubectl describe svc redis-service` |
| 4. Create webapp Deployment | `kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3` | `kubectl get deployment webapp` / `kubectl get pods -l app=webapp` |
| 5. Create custom-nginx with port | `kubectl run custom-nginx --image=nginx --port=8080` | `kubectl get pod custom-nginx -o yaml` |
| 6. Create namespace | `kubectl create namespace devns` | `kubectl get ns devns` |
| 7. Create redis-deploy in namespace | `kubectl create deployment redis-deploy --image=redis --replicas=2 -n devns` | `kubectl get deployment -n devns` / `kubectl get pods -n devns` |
| 8. Create httpd Pod and Service | `kubectl run httpd --image=httpd:alpine --expose --port=80` | `kubectl get pod httpd` / `kubectl get svc httpd` |

***

That completes the imperative tasks. Use `kubectl get`, `kubectl describe`, and `kubectl get -o yaml` to inspect resources and confirm labels, selectors, ports, and endpoints for each created object. If a pod or deployment takes time to become ready, wait a few moments and re-run the verification commands.

<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/6d08e202-199d-455a-bbec-b3ed24e7e2ce" />
</CardGroup>


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