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 }
CommandAction
kubectl apply -f pod.yamlCreate/update (declarative)
kubectl create -f pod.yamlCreate only (errors if exists)
kubectl replace -f pod.yamlReplace existing

2. Running Pod Imperatively

CommandUse
kubectl run web --image=nginxQuick pod
kubectl run tmp -it --rm --image=busybox -- shInteractive shell, auto-delete
kubectl run web --image=nginx --dry-run=client -o yamlGenerate YAML template
kubectl run job1 --image=alpine --restart=OnFailureCreate 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
PhaseMeaning
PendingAccepted, not yet running (scheduling/pulling)
RunningAt least one container started
SucceededAll containers terminated successfully
FailedAll terminated; ≥1 with non-zero exit
UnknownState could not be obtained

4. Describing Pod Details

SectionUse
Status / ConditionsPodScheduled, Initialized, Ready, ContainersReady
ContainersImage, ports, env, mounts, last state, restart count
VolumesMounted volumes
EventsRecent scheduling/pull/probe events
kubectl describe pod web -n prod

5. Viewing Pod Logs

FlagEffect
-c CONTAINERMulti-container pod
--previousLogs from prior crashed container
--tail=100Last N lines
--since=10mTime window
--timestampsRFC3339 timestamps
-l app=webAcross 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
ToolStrength
kubectl logs -fBuilt-in, single pod
sternRegex match, colored, multi-pod
kailFilter by labels/namespace, follow new pods

7. Executing Commands in Pod

CommandUse
kubectl exec POD -- ls /Single command
kubectl exec -it POD -- shInteractive shell
kubectl exec -it POD -c sidecar -- bashSpecific 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
FormatEffect
LOCAL:REMOTEMap local port to pod port
:REMOTERandom local port

9. Deleting Pods

CommandBehavior
kubectl delete pod webGraceful delete (30s default)
kubectl delete pod web --grace-period=0 --forceImmediate, may orphan
kubectl delete pods -l app=webBulk by label
kubectl delete pod --field-selector=status.phase=FailedCleanup failed pods

10. Using Pod Selectors

SyntaxMeaning
-l app=webEquality
-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
FlagEffect
-wInitial list + updates
--watch-onlySkip 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}'
OutputFormat
-o yamlFull YAML
-o jsonJSON
-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
DirectionSyntax
Uploadkubectl cp src POD:dest
Downloadkubectl cp POD:src dest
Requirestar binary in container