Kubernetes Pods Practical - Essential Pod Commands & Examples Guide

Practical hands-on tutorial for Kubernetes pod management with essential kubectl commands. Learn kubectl run, get pods, describe pod, exec commands, interactive shell access, force deletion, and YAML-based pod management with copy-paste examples and downloadable configuration files.

Create Pod (Imperative)

kubectl run mynginx --image=nginx

Creates a new pod running Nginx using the imperative approach.

Explanation:

  • kubectl run → Creates and runs a pod
  • mynginx → Name for the pod
  • --image=nginx → Specifies the container image to use

List Pods

kubectl get pods

Displays a list of all running pods in the current namespace.

Usage:

Use this command to check the status of your running pods, see their names, status, and age.

Detailed Pod Information

kubectl get pods -o wide

Shows additional information about pods including IP addresses and nodes.

Explanation:

  • -o wide → Shows extended information
  • Displays node name, IP addresses, and more

Describe Pod

kubectl describe pod mynginx

Shows detailed information about a specific pod including events, conditions, and configuration.

Usage:

Use this command when troubleshooting pod issues to see events and detailed status information.

Delete Pod

kubectl delete pod mynginx

Deletes a specific pod from the cluster.

Note:

If the pod is managed by a Deployment or ReplicaSet, it will be recreated automatically.

Create a pod running BusyBox with Shell

kubectl run mybox --image=busybox -it -- /bin/sh

Creates a pod and attaches an interactive shell session to it.

Shell Commands to run:

ls

List directory contents

echo -n 'A Secret' | base64

Encode string to base64 (for Kubernetes Secrets)

exit

Exit the shell session

Explanation:

  • -it → Interactive terminal
  • -- /bin/sh → Command to run inside the container
  • Use exit to leave the shell
  • The base64 command demonstrates encoding for Kubernetes Secrets

Force Delete Pod

kubectl delete pod mybox --wait=false
kubectl delete pod mybox --grace-period=0 --force

Forcefully deletes a pod without waiting for graceful termination.

Warning:

This command immediately terminates the pod without allowing it to clean up. Use with caution.

Create from YAML (Declarative)

kubectl create -f myapp.yaml

Creates Kubernetes resources defined in a YAML configuration file.

Get the YAML file

Download the myapp.yaml configuration file

Download myapp.yaml

Explanation:

  • create -f → Create resources from file
  • myapp.yaml → Configuration file defining the resources
  • This is the declarative approach to Kubernetes management

Detailed Pod Information

kubectl get pods -o wide

Shows additional information about pods including IP addresses and nodes.

Explanation:

  • -o wide → Shows extended information
  • Displays node name, IP addresses, and more

Describe Pod

kubectl describe pod myapp-pod

Shows detailed information about a specific pod including events, conditions, and configuration.

Usage:

Use this command when troubleshooting pod issues to see events and detailed status information.

Execute Command in Pod

kubectl exec -it myapp-pod -- bash

Executes a command inside a running pod. This example opens a bash shell.

Shell Commands to run:

echo $DBCON

Print the value of the DBCON environment variable

exit

Exit the shell session

Explanation:

  • exec → Execute command in container
  • -it → Interactive terminal
  • myapp-pod → Name of the pod
  • -- bash → Command to execute

Delete from YAML

kubectl delete -f myapp.yaml

Deletes all resources defined in a YAML configuration file.

Usage:

This is the cleanest way to remove resources that were created from a configuration file.