Introduction
Kubernetes, as a container orchestration system, manages the lifecycle of containers within its pods. This tutorial aims to demystify the complex lifecycle stages and states that pods go through from creation until termination. The tutorial includes hands-on examples to give you a practical understanding of these concepts.
A pod is the smallest and simplest unit in the Kubernetes object model that you can create or deploy. It represents a single instance of a process running in your cluster. Pods encapsulate an application’s container (or, in some cases, multiple containers), storage resources, a unique network IP, and options that govern how the container(s) should run.
Pod Phases
The status of a Pod is defined in terms of its Phase. A Pod’s status
field is a PodStatus
object, which has a phase
field. The possible phase
values are:
Pending
: The pod has been accepted by the Kubernetes system but one or more containers are not yet running.Running
: The pod has been bound to a node, and all of the containers have been created. At least one container is still running, or is in the process of starting or restarting.Succeeded
: All containers in the pod have terminated successfully, and they will not be restarted.Failed
: All containers in the pod have terminated, and at least one container has terminated in failure.
Basic Pod Creation
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
In the above example, we define a simple pod that runs a single container. Apply this configuration with kubectl apply -f pod.yaml
.
Checking Pod Phase
$ kubectl get pod myapp-pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 30s
Pod Conditions
Besides the phase
, Pods have a status
field which is an array of PodCondition
types. Common pod conditions include:
PodScheduled
: The pod has been scheduled to a node.ContainersReady
: All containers in the pod are ready.Initialized
: All initialization containers have started successfully.Ready
: The pod is able to serve requests and should be added to the load balancing pools of all matching services.
Examining Pod Conditions
$ kubectl describe pod myapp-pod
...
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
...
Container Probes
Kubernetes provides probes to diagnose and take action based on the state of the containers within the pods. There are three kinds of probes:
livenessProbe
: Indicates whether the container is running effectively.readinessProbe
: Indicates whether the container is ready to serve requests.startupProbe
: Indicates whether the application within the container has started up completely.
Create a liveness probe:
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
containers:
- name: liveness-container
image: k8s.gcr.io/liveness
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
Liveness Probe Output
$ kubectl describe pod liveness-pod
...
Liveness: tcp-socket :8080 delay=15s timeout=1s period=20s #success=1 #failure=3
...
Container States
Containers within a pod can be in three different states:
Waiting
: Container is not yet running.Running
: Container has started, and is running.Terminated
: Container has been stopped.
The container states offer insights into the pod’s condition, particularly during debugging when Pods are not starting or behaving as expected.
Observing Container States
$ kubectl describe pod myapp-pod
...
Containers:
myapp-container:
State: Running
Started: Wed, 01 Jan 2020 00:00:00 -0800
...
Advanced Usage: Init Containers
Init containers allow you to initialize a pod before your application containers start. Let’s consider an advanced example:
apiVersion: v1
kind: Pod
metadata:
name: myapp-init
spec:
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
containers:
- name: myapp-container
image: myapp:latest
ports:
- containerPort: 80
Conclusion
In this tutorial, we have explored the intricacies of the pod lifecycle and states in Kubernetes through practical examples. Understanding these lifecycle phases, conditions, container probes, and states can significantly help in managing and troubleshooting applications on Kubernetes effectively.