0
0
Kubernetesdevops~5 mins

Pod lifecycle states in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Pods are the smallest units in Kubernetes that run containers. Understanding their lifecycle states helps you know what is happening to your app at each step, like when it starts, runs, or stops.
When you want to check if your app is running correctly inside Kubernetes.
When your pod is stuck and you need to know why it is not starting.
When you want to monitor the health and status of your app containers.
When you need to debug issues related to pod crashes or restarts.
When you want to understand how Kubernetes manages your app's containers over time.
Commands
This command creates a pod named example-pod running the nginx container. The restart=Never flag means Kubernetes will not restart the pod automatically if it stops.
Terminal
kubectl run example-pod --image=nginx --restart=Never
Expected OutputExpected
pod/example-pod created
--image - Specifies the container image to run inside the pod
--restart - Controls the pod restart policy; Never means no automatic restart
This command shows the current state of the example-pod, such as Pending, Running, or Completed.
Terminal
kubectl get pods example-pod
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-pod 1/1 Running 0 10s
This command gives detailed information about the pod, including events and state changes, helping you understand its lifecycle transitions.
Terminal
kubectl describe pod example-pod
Expected OutputExpected
Name: example-pod Namespace: default Status: Running Containers: nginx: Container ID: docker://abcdef123456 Image: nginx State: Running Started: Thu, 01 Jan 1970 00:00:10 +0000 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned default/example-pod to node-1 Normal Pulled 1m kubelet Container image "nginx" already present on machine Normal Created 1m kubelet Created container nginx Normal Started 1m kubelet Started container nginx
This command deletes the example-pod, which moves it to the Terminating state before removal.
Terminal
kubectl delete pod example-pod
Expected OutputExpected
pod "example-pod" deleted
Key Concept

If you remember nothing else from pod lifecycle states, remember: pods move through states like Pending, Running, and Terminating to show their current condition.

Common Mistakes
Trying to create a pod without specifying a container image
Kubernetes needs a container image to run inside the pod; without it, the pod cannot start.
Always specify a valid container image using the --image flag when creating a pod.
Assuming a pod is running just because it exists
A pod can exist but be in Pending or Failed state, meaning it is not ready to serve your app.
Use kubectl get pods and check the STATUS column to confirm the pod is Running.
Deleting a pod without understanding it will be Terminating first
The pod does not disappear instantly; it goes through Terminating state which may take time if cleanup is needed.
Wait for the pod to fully terminate before assuming it is gone.
Summary
Create a pod with kubectl run specifying the container image and restart policy.
Check the pod's current lifecycle state with kubectl get pods.
Use kubectl describe pod to see detailed state changes and events.
Delete the pod to move it to the Terminating state before removal.