Kubernetes Pod Lifecycle Tutorial-DecodingDevOps

Kubernetes Pod Lifecycle Tutorial-DecodingDevOps

Pods are the smallest artifact in the Kubernetes System. There are always containers running inside the pod. These containers contain the package of the application and it's compatible environment. There are some cases in which pods face failure or other states during its life cycle. in the following steps i will explain about what is kubernetes pod life cycle and different states of a pod.

States of a Pod

A deployed pod goes through different phase or we can say states during its lifespan.

  • Pending: When the Pod's metadata is accepted by the Kuberenetes but still hasn't been deployed to any node, Pod goes into the Pending state. E.g. When Nodes do not have enough resources in the cluster, it causes the pod to go in pending state.
  • Running: When the pod gets scheduled on any node and containers inside the pod started, Pod stays in Running phase.
  • Succeeded: When containers inside the the pod get terminated with exit code 0 then the pod goes into the Succeeded State.
  • Failed: When one of the containers inside the pod gets terminated with exit code other than 0, It causes the pod to go into the Failed state.
  • CrashLoopBackoff: When the containers inside the pod get failed to start then the pod is being recreated again and again.

Container probes

There are some probe mechanisms used in the Kubernetes to continue monitoring the running pod's health.

  • Exec Probe: It is being used to execute a command inside the container. It is being considered a successful probe if command gets terminated with exit code 0.
  • TCPSocket Probe: It is being used to establish a TCP connection to a port and if connection gets successfully established then it is being considered successful.
  • HTTPGet Probe: If pods are being used to deploy web apps then this probe is helpful to monitor pod's health. It is being considered successful if it returns the status code of the http request in range 200 to 400.

These probes hold one three results as follow:

  • Success: The container passed the probe.
  • Failure: The container failed to return a positive response to the probe.
  • Unknown: The probe failed. So nothing will happen as an action.

Using these probe mechanisms, the Kubernetes provides different healthcheck functionalities.

  • Liveness: We use a liveness check to monitor our application. If something happens to our application and liveness check don't get any valid response from the server then kubernetes  will recreate the pod and monitor it again.
  • Readiness: A healthy Readiness Probe's response always tells that container and application is ready to serve. Always kubernetes includes those pods into the load balancer who are considered healthy with the help of readiness. Unhealthy pods are removed from the Load Balancer.

Lifecycle Hooks

There are two types of hook being used by the Kubernetes system to execute some actions before the starting the application or before getting terminated.

PreStart Hook: This allows the system to execute actions before the application gets started.

PreStop Hook: This allows the system to execute some actions just before pods get terminated 

Pod LifeCycle

Pod Creation

The pod has been going from a different phase since its birth. It can be terminated with Succeeded phase or it might be go into the Failed state if container terminated with exit code other than 0.

Pods went through the following situations to serve it's task.

  1. Kubectl or any other tool submit the meta information for the Pod to the API server.
  2. API server stores that information into the storage called etcd.
  3. Using watcher functionality, scheduler can check that there is a new pod available to be deployed and hasn't yet on to any node.
  4. Scheduler assign a node to the new pod and send acknowledge API server.
  5. API server updates this changes in to the etcd.
  6. Kubelete in every node gets information from their watcher tool and deploy a pod's container on them if it gets assigned to them from the scheduler.
  7. Once containers get started on the node, kublete acknowledge API server about the container's state.
  8. This information is being stored into the etcd by the API server. 
  9. The etcd acknowledge again to API server and this acknowledgment is being sent back to the kublete to tell it that changes are accepted.
  • kubernetes pod lifecycle hooks
  • pod lifecycle kubernetes
  • kubernetes pod restart reason
  • pod pending kubernetes
  • kubernetes pod lifecycle prestop
  • pod lifecycle poststart kubernetes
  • kubernetes pod startup time
  • prestop sleep kubernetes
  • kubernetes prestop http example
  • kubernetes restart pod periodically