Pod Commands Sheet!
1. Pod Creation (Imperative Commands)
Section titled “1. Pod Creation (Imperative Commands)”The run command creates a bare Pod. (Note: In production, you usually use Declarative YAML files, but these imperative commands are essential for fast testing and generating templates).
-
Create a basic Pod:
kubectl run my-pod --image=nginx -
Generate YAML without creating the Pod (The most important admin trick):
kubectl run my-pod --image=nginx --dry-run=client -o yaml > pod.yaml -
Create with a specific port exposed:
kubectl run my-pod --image=nginx --port=80 -
Create and attach labels:
kubectl run my-pod --image=nginx --labels="tier=backend,env=dev" -
Create with environment variables:
kubectl run my-pod --image=nginx --env="DB_HOST=10.0.0.5" -
Create and immediately attach a shell:
kubectl run my-pod --image=busybox -it --rm -- /bin/sh(Logic:
--rmdestroys the Pod the second you exit the shell. Perfect for quick network testing).
2. Listing & Discovery
Section titled “2. Listing & Discovery”By default, kubectl only looks in the default namespace.
-
List Pods in the current namespace:
kubectl get pods -
List Pods with extra details (Shows Node assignment and Pod IPs):
kubectl get pods -o wide -
List Pods in a specific namespace:
kubectl get pods -n kube-system -
List Pods across ALL namespaces:
kubectl get pods -A -
Watch Pods in real-time (Live stream of state changes):
kubectl get pods -w -
Filter Pods by their labels:
kubectl get pods -l tier=backend
3. Modifying a Running Pod
Section titled “3. Modifying a Running Pod”Pods are largely immutable. You cannot change their network namespace or hardware limits on the fly. You can change their image, which forces the kubelet to restart the container within the existing Pod boundary.
-
Change the image of a specific container inside a Pod:
kubectl set image pod/my-pod <container-name>=nginx:1.19.1 -
Live-edit the Pod’s configuration in your default terminal editor:
kubectl edit pod my-pod(Logic: When you save and close, the API server instantly attempts to apply the changes to
etcd).
4. Debugging & Interaction
Section titled “4. Debugging & Interaction”When a Pod fails, you use these three commands in this exact order to find the root cause.
-
Check the Pod’s lifecycle events and scheduling errors:
kubectl describe pod my-pod -
Check the application’s standard output/error logs:
kubectl logs my-pod -
Check logs for a specific container (if the Pod has multiple):
kubectl logs my-pod -c sidecar-container -
Open an interactive shell directly inside the running container:
kubectl exec -it my-pod -- /bin/sh(Use
/bin/bashif the image supports it).
5. Destruction
Section titled “5. Destruction”-
Delete a specific Pod:
kubectl delete pod my-pod -
Delete all Pods in the current namespace:
kubectl delete pods --all -
Force delete a stuck Pod (Bypasses the 30-second graceful termination):
kubectl delete pod my-pod --force --grace-period=0