Pod Management Basic Commands
In this lesson we will be working with Pods using kubectl. We’ll create a simple Nginx Pod, check that it’s running, inspect what’s happening inside it, view its logs, execute commands in the container, and finally delete it. Along the way, we’ll also look at a few useful commands that make troubleshooting Pods much easier.
1. Start a Pod From a Config File
Instead of creating a Pod entirely from the command line, you can describe it in a YAML file.
For example, create a file named webserver_pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: webserver
spec:
containers:
- name: web1
image: nginx:latest
ports:
- containerPort: 80This configuration creates a Pod named webserver with one container called web1, running the nginx:latest image.
Now apply the configuration:
kubectl apply -f webserver_pod.yamlYou should see something similar to:
pod/webserver createdCheck That The Pod Started
kubectl get podsExample output:
NAME READY STATUS RESTARTS AGE
webserver 1/1 Running 0 10sIf the Pod isn’t Running, don’t worry yet. You can investigate it using the commands below.
2. List Pods
The most common command you’ll use is:
kubectl get podsIt gives you a quick overview of the Pods in your current namespace. For example:
NAME READY STATUS RESTARTS AGE
webserver 1/1 Running 0 2mHere’s what the columns mean:
- NAME — the name of the Pod.
- READY — how many containers are ready compared with the total number of containers.
- STATUS — the current Pod status.
- RESTARTS — how many times containers in the Pod have restarted.
- AGE — how long the Pod has existed.
Get a Specific Pod
If you only want information about webserver:
kubectl get pod webserverYou can also get more details in a wider format:
kubectl get pod webserver -o wideNAME READY STATUS RESTARTS AGE IP NODE
webserver 1/1 Running 0 3m 10.244.0.5 worker-13. Describe a Pod
When something isn’t working, kubectl get pods usually isn’t enough. That’s when kubectl describe becomes useful:
kubectl describe pod webserverThis shows a lot more information, including:
- Pod status
- Containers
- Images
- Environment variables
- Volumes
- Node information
- Conditions
- Events
The Events section near the bottom is especially useful when a Pod won’t start.
For example, you might see an event such as:
Events:
Type Reason Message
---- ------ -------
Normal Scheduled Successfully assigned default/webserver to worker-1
Normal Pulling Pulling image "nginx:latest"
Normal Pulled Successfully pulled image
Normal Created Created container web1
Normal Started Started container web1If there’s a problem, you’ll often see a warning explaining what went wrong.
4. View Pod Logs
Once the Pod is running, you can see what the container is writing to its standard output and error streams:
kubectl logs webserverFor Nginx, you may see access or startup-related output depending on what the container has logged.
Follow Logs In Real Time
If you want to watch new log entries as they appear:
kubectl logs -f webserverPress Ctrl+C when you’re done.
Logs From a Specific Container
This becomes important when a Pod has multiple containers.
Use:
kubectl logs webserver -c web1The -c option tells Kubernetes exactly which container’s logs you want.
5. Execute a Command Inside The Pod
Sometimes logs aren’t enough. You may want to get inside the container and inspect the filesystem, processes, configuration, or network.
You can start an interactive shell with:
kubectl exec -it webserver -- /bin/bashIf the image doesn’t contain Bash, try sh instead:
kubectl exec -it webserver -- /bin/shYou will get the interactive session as such:
root@webserver:/#Now you’re inside the container.
You can run commands such as ls, pwd or anything you like to and when you are finished exit to exit.
Run a Single Command Without Opening a Shell
You don’t always need an interactive session, you can run ad-hoc commands:
kubectl exec webserver -- nginx -vOr:
kubectl exec webserver -- ls /usr/share/nginx/htmlThis is particularly useful for quick checks and scripts.
6. Check The Pod’s Configuration
You can also ask Kubernetes to show the Pod’s configuration as YAML:
kubectl get pod webserver -o yamlThis is useful when you want to see what Kubernetes actually has stored for the Pod.
7. List Containers Inside a Pod
A Pod can contain more than one container. To list the names of the regular containers:
kubectl get pod webserver \
-o jsonpath='{.spec.containers[*].name}'If you want each container on its own line:
kubectl get pod webserver \
-o jsonpath='{.spec.containers[*].name}' | tr ' ' '\n'For a Pod containing multiple containers, you might get:
web1
sidecarThis is useful when you’re unsure which container name to pass to commands such as:
kubectl logs webserver -c web1or:
kubectl exec -it webserver -c web1 -- /bin/sh8. List Regular And Init Containers
Kubernetes also supports init containers.
Init containers run before the regular application containers start. They’re commonly used for initialization tasks such as preparing files or waiting for another service.
To display both init containers and regular containers:
kubectl get pod webserver \
-o jsonpath='{.spec.initContainers[*].name} {.spec.containers[*].name}'For example, a Pod might return:
init-config web1 sidecarHere:
init-configis an init container.web1andsidecarare regular containers.
If you want to make the output easier to read:
kubectl get pod webserver \
-o jsonpath='{.spec.initContainers[*].name} {.spec.containers[*].name}' \
| tr ' ' '\n'9. Check Pod Status and Events
When troubleshooting, these two commands are particularly useful:
kubectl get pod webserverand:
kubectl describe pod webserverYou can also look directly at recent events:
kubectl get eventsFor a cleaner chronological view:
kubectl get events --sort-by='.lastTimestamp'If your Pod is stuck in states such as Pending, ContainerCreating, CrashLoopBackOff, or ImagePullBackOff, checking the events is often one of the first things you should do.
10. Delete The Pod
When you’re finished experimenting, you can remove the Pod with:
kubectl delete pod webserverYou should see:
pod "webserver" deletedVerify that it’s gone:
kubectl get podsIf webserver was the only Pod, you may see:
No resources found in default namespace.11. A Useful Workflow To Remember
You don’t need to memorize every kubectl command immediately. For a basic Pod, a typical workflow looks like this:
# Create the Pod
kubectl apply -f webserver_pod.yaml
# Check its status
kubectl get pods
# Get more information
kubectl describe pod webserver
# View logs
kubectl logs webserver
# Open a shell
kubectl exec -it webserver -- /bin/sh
# Inspect the Pod configuration
kubectl get pod webserver -o yaml
# Delete the Pod
kubectl delete pod webserverIf something goes wrong, add these to your troubleshooting toolkit:
# get Pod info
kubectl get pod webserver -o wide
# describe Pod
kubectl describe pod webserver
# get events chronologically
kubectl get events --sort-by='.lastTimestamp'
# get Pod logs
kubectl logs webserverOne important thing to remember
A Pod is not usually the object you manage directly in a production application. In real Kubernetes workloads, you’ll more commonly create a Deployment, StatefulSet, DaemonSet, or another controller that manages Pods for you.
For learning Kubernetes fundamentals, however, creating and inspecting a Pod directly is a great way to understand how kubectl and Pods work.