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: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:
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 withkubectl create service or kubectl expose. kubectl expose is convenient because it can automatically use the Pod labels to set the Service selector.
Command:
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: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:
--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:7) Create a Deployment named redis-deploy in the devns namespace with 2 replicas
Target thedevns namespace using -n or --namespace:
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 withkubectl run plus --expose (requires --port):
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 servicewith explicit flags, or - Generate YAML (
--dry-run=client -o yaml), edit it, then apply.
kubectl expose or --expose on kubectl run is often the fastest approach.
Further reading and references:
- Kubernetes Services: https://kubernetes.io/docs/concepts/services-networking/service/
- kubectl cheat sheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
- Deployments: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/