Understanding these three core Kubernetes objects — Pods, Deployments, and Services — is the foundation for everything else you'll do in Kubernetes. This guide explains each with practical examples.
Pods: The Smallest Unit
A Pod wraps one or more containers that share networking and storage — in practice, most Pods run exactly one container. You rarely create Pods directly; instead, a Deployment manages them for you.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
Why You Don't Manage Pods Directly
A bare Pod, if it crashes, stays dead — nothing restarts it automatically. This is why Deployments exist: they continuously ensure the desired number of Pod replicas are running.
Deployments: Managing Pod Replicas
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f deployment.yaml
replicas: 3 tells Kubernetes to always keep 3 identical Pods running — if one crashes, a replacement is created automatically.
Services: Stable Networking for Pods
Pods are ephemeral — they get new IP addresses whenever recreated. A Service provides a stable, unchanging endpoint that automatically routes to whichever Pods currently match its selector.
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
type: ClusterIP
Service Types
| Type | Use Case |
|---|---|
| ClusterIP (default) | Internal-only access, other Pods within the cluster |
| NodePort | Exposes the service on a static port on every node, accessible externally |
| LoadBalancer | Requests an external load balancer (typically requires cloud provider integration) |
Putting It Together: A Complete Simple App
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl get pods
kubectl get services
Scaling a Deployment
kubectl scale deployment my-app --replicas=5
Viewing Pod Logs
kubectl logs POD_NAME
Getting a Shell Inside a Running Pod
kubectl exec -it POD_NAME -- /bin/bash
Common Errors
Service isn't routing traffic to Pods — verify the Service's selector exactly matches the labels defined in the Deployment's Pod template; a mismatch is the most common cause.
Pods stuck in "Pending" — usually insufficient cluster resources (CPU/RAM) to schedule the Pod; check with kubectl describe pod POD_NAME for the specific reason.
FAQ
Do I need a separate YAML file for every object?
No — multiple objects can be combined in one file separated by ---, though separate files are often clearer for larger projects.
Continue Reading
- How to Deploy a Stateless Web App to Kubernetes (Step-by-Step)
- How to Expose Applications with a Kubernetes Ingress Controller (Nginx Ingress)
- Kubernetes ConfigMaps and Secrets: Managing Configuration Safely
Browse more articles in Kubernetes & Container Orchestration.