Skip to content

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:

webserver_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: webserver
spec:
  containers:
    - name: web1
      image: nginx:latest
      ports:
        - containerPort: 80

This 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.yaml

You should see something similar to:

pod/webserver created

Check That The Pod Started

kubectl get pods

Example output:

NAME        READY   STATUS    RESTARTS   AGE
webserver   1/1     Running   0          10s

If 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 pods

It gives you a quick overview of the Pods in your current namespace. For example:

NAME        READY   STATUS    RESTARTS   AGE
webserver   1/1     Running   0          2m

Here’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 webserver

You can also get more details in a wider format:

kubectl get pod webserver -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE
webserver   1/1     Running   0          3m    10.244.0.5   worker-1

3. 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 webserver

This 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 web1

If 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 webserver

For 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 webserver

Press 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 web1

The -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/bash

If the image doesn’t contain Bash, try sh instead:

kubectl exec -it webserver -- /bin/sh

You 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 -v

Or:

kubectl exec webserver -- ls /usr/share/nginx/html

This 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 yaml

This 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
sidecar

This is useful when you’re unsure which container name to pass to commands such as:

kubectl logs webserver -c web1

or:

kubectl exec -it webserver -c web1 -- /bin/sh

8. 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 sidecar

Here:

  • init-config is an init container.
  • web1 and sidecar are 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 webserver

and:

kubectl describe pod webserver

You can also look directly at recent events:

kubectl get events

For 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 webserver

You should see:

pod "webserver" deleted

Verify that it’s gone:

kubectl get pods

If 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 webserver

If 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 webserver

One 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.

Last updated on