Skip to main content
This lab walks through practical, imperative kubectl commands to create and expose Kubernetes objects quickly. Follow the tasks in order to practice creating Pods, Deployments, Services, and Namespaces using kubectl’s imperative subcommands.
Note: The behavior of kubectl run has changed across versions. Some kubectl releases create a Deployment by default; others create a Pod. If your kubectl creates a Deployment but you need a Pod, add --restart=Never. To explicitly create a Deployment, omit --restart or use kubectl create deployment. See the Kubernetes docs for version-specific behavior.

Quick reference — commands used in this lab: For more background on kubectl command patterns and object types, see: Kubernetes Concepts — Workloads and Services.

1) Create a Pod named nginx-pod using the nginx:alpine image

Command:
Expected output:
Explanation: This creates a Pod resource named nginx-pod that runs the nginx:alpine container image.

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

Use --labels to attach labels at creation time:
Expected output:
Explanation: The Pod receives the label tier=db, which is useful as a Service selector to route traffic to this Pod.

3) Create a ClusterIP Service named redis-service that exposes port 6379

A Service can be created with kubectl create service or kubectl expose. kubectl expose is convenient because it can automatically use the Pod labels to set the Service selector. Command:
Expected output:
Verify the Service:
Sample output:
Describe the Service to confirm the selector and endpoints:
Sample (abridged) output:
Explanation: kubectl expose detected the Pod’s tier=db label and used it as the Service selector. The Service endpoints show the Pod IP(s) that match the selector.
Reminder: The --port flag (used with kubectl run or kubectl expose) sets the container’s containerPort in the Pod spec — it does not automatically publish the port outside the cluster. To make a Service route traffic, you must create or expose a Service that targets that port.

4) Create a Deployment named webapp with 3 replicas

Create the Deployment:
Expected output:
Check the Deployment status:
Sample output (after pods are up):
Tip: If you see 0/3 initially, wait a few seconds and re-run kubectl get deploy webapp until pods become READY.

5) Create a Pod named custom-nginx using nginx and set container port 8080

Use --port to populate the Pod’s containerPort field:
Expected output:
Explanation: The --port flag sets the containerPort in the Pod spec. That alone does not create a Service — use kubectl expose or kubectl create service to route traffic to this port.

6) Create a namespace named devns

Command:
Expected output:
Explanation: Namespaces provide logical separation for resources in the cluster.

7) Create a Deployment named redis-deploy in the devns namespace with 2 replicas

Target the devns namespace using -n or --namespace:
Expected output:
Verify:
Sample output:
Explanation: This creates the Deployment inside the devns namespace rather than default.

8) Create a Pod named httpd (httpd:alpine) and create a ClusterIP Service of the same name targeting port 80, using as few steps as possible

You can create a Pod and a matching ClusterIP Service in one step with kubectl run plus --expose (requires --port):
Expected output (both created):
Verify the Pod and Service:
Describe the Service to confirm selector and endpoint:
Sample (abridged) output:
Explanation: The command creates a Pod named httpd and an internal ClusterIP Service named httpd that forwards to port 80 on the Pod.
If you need a Service with a specific selector or a different target port, you can:
  • Use kubectl create service with explicit flags, or
  • Generate YAML (--dry-run=client -o yaml), edit it, then apply.
For quick tasks, kubectl expose or --expose on kubectl run is often the fastest approach. Further reading and references:

Watch Video