Creating and Managing Pods
1. Creating Pod from YAML
Example: Minimal Pod spec
apiVersion: v1
kind: Pod
metadata:
name: web
labels: { app: web }
spec:
containers:
- name: nginx
image: nginx:1.27-alpine
ports: [{ containerPort: 80 }]
resources:
requests: { cpu: 100m, memory: 128Mi }
limits: { cpu: 500m, memory: 256Mi }
| Command | Action |
|---|---|
kubectl apply -f pod.yaml | Create/update (declarative) |
kubectl create -f pod.yaml | Create only (errors if exists) |
kubectl replace -f pod.yaml | Replace existing |
2. Running Pod Imperatively
| Command | Use |
|---|---|
kubectl run web --image=nginx | Quick pod |
kubectl run tmp -it --rm --image=busybox -- sh | Interactive shell, auto-delete |
kubectl run web --image=nginx --dry-run=client -o yaml | Generate YAML template |
kubectl run job1 --image=alpine --restart=OnFailure | Create a Job |
3. Listing Pods
kubectl get pods # current namespace
kubectl get pods -A # all namespaces
kubectl get pods -o wide # add node + IP
kubectl get pods -l app=web,env=prod # label filter
kubectl get pods --field-selector=status.phase=Running
| Phase | Meaning |
|---|---|
| Pending | Accepted, not yet running (scheduling/pulling) |
| Running | At least one container started |
| Succeeded | All containers terminated successfully |
| Failed | All terminated; ≥1 with non-zero exit |
| Unknown | State could not be obtained |
4. Describing Pod Details
| Section | Use |
|---|---|
| Status / Conditions | PodScheduled, Initialized, Ready, ContainersReady |
| Containers | Image, ports, env, mounts, last state, restart count |
| Volumes | Mounted volumes |
| Events | Recent scheduling/pull/probe events |
kubectl describe pod web -n prod
5. Viewing Pod Logs
| Flag | Effect |
|---|---|
-c CONTAINER | Multi-container pod |
--previous | Logs from prior crashed container |
--tail=100 | Last N lines |
--since=10m | Time window |
--timestamps | RFC3339 timestamps |
-l app=web | Across pods matching label |
6. Following Log Streams
kubectl logs -f web # tail single pod
kubectl logs -f -l app=web --max-log-requests=10
stern '^web-.*' -n prod # plugin: multi-pod follow
| Tool | Strength |
|---|---|
| kubectl logs -f | Built-in, single pod |
| stern | Regex match, colored, multi-pod |
| kail | Filter by labels/namespace, follow new pods |
7. Executing Commands in Pod
| Command | Use |
|---|---|
kubectl exec POD -- ls / | Single command |
kubectl exec -it POD -- sh | Interactive shell |
kubectl exec -it POD -c sidecar -- bash | Specific container |
8. Port Forwarding to Pod
kubectl port-forward pod/web 8080:80
kubectl port-forward svc/web 8080:80
kubectl port-forward deploy/web 8080:80
kubectl port-forward --address 0.0.0.0 pod/web 8080:80
| Format | Effect |
|---|---|
LOCAL:REMOTE | Map local port to pod port |
:REMOTE | Random local port |
9. Deleting Pods
| Command | Behavior |
|---|---|
kubectl delete pod web | Graceful delete (30s default) |
kubectl delete pod web --grace-period=0 --force | Immediate, may orphan |
kubectl delete pods -l app=web | Bulk by label |
kubectl delete pod --field-selector=status.phase=Failed | Cleanup failed pods |
10. Using Pod Selectors
| Syntax | Meaning |
|---|---|
-l app=web | Equality |
-l 'env in (prod,staging)' | Set-based |
-l 'env notin (dev)' | Negation |
-l '!canary' | Label absent |
-l 'tier,!debug' | Combined |
11. Watching Pod Status
kubectl get pods -w # stream changes
kubectl get pods --watch-only
watch -n 1 kubectl get pods # shell watch
| Flag | Effect |
|---|---|
-w | Initial list + updates |
--watch-only | Skip initial list |
12. Getting Pod YAML
kubectl get pod web -o yaml
kubectl get pod web -o yaml | kubectl neat # strip generated fields
kubectl get pod web -o jsonpath='{.spec.containers[*].image}'
| Output | Format |
|---|---|
-o yaml | Full YAML |
-o json | JSON |
-o jsonpath='...' | JSONPath expression |
-o custom-columns=... | Custom table |
13. Copying Files to Pod
kubectl cp ./local.txt POD:/tmp/remote.txt
kubectl cp POD:/var/log/app.log ./app.log
kubectl cp ./dir POD:/data -c CONTAINER
| Direction | Syntax |
|---|---|
| Upload | kubectl cp src POD:dest |
| Download | kubectl cp POD:src dest |
| Requires | tar binary in container |