chapter six

6 Managing the pod lifecycle and container health

 

This chapter covers

  • Inspecting the pod’s status
  • How to keep containers healthy using liveness probes
  • Using lifecycle hooks to perform actions at container startup and shutdown
  • The complete lifecycle of the pod and its containers

After reading the previous chapter, you should be able to deploy, inspect, and communicate with pods containing one or more containers. This chapter provides a much deeper understanding of how the pod and its containers operate.

Note

The code files for this chapter are available at https://mng.bz/oZjD.

6.1 Understanding the pod's status

After you create a Pod object and it runs, you can see what’s going on with the pod by reading the Pod object back from the API. As discussed in chapter 4, the Pod object manifest, as well as the manifests of most other kinds of objects, contain a section that provides the status of the object. A pod’s status section contains the following information:

  • The IP addresses of the pod and the worker node that hosts it
  • The time the pod was started
  • The pod’s quality-of-service (QoS) class
  • The phase the pod is in
  • The conditions of the pod
  • The state of its individual containers

The IP addresses and the start time don’t need any further explanation, and the QoS class isn’t relevant now. However, the phase and conditions of the pod, as well as the states of its containers, are important for understanding the pod lifecycle.

6.1.1 Understanding the pod phase

6.1.2 Understanding pod conditions

6.1.3 Understanding the container status

6.2 Keeping containers healthy

6.2.1 Understanding container auto-restart

6.2.2 Checking the container’s health using liveness probes

6.2.3 Creating an HTTP GET liveness probe

6.2.4 Observing the liveness probe in action

6.2.5 Using the exec and the tcpSocket liveness probe types

6.2.6 Using a startup probe when an application is slow to start

6.2.7 Creating effective liveness probe handlers

6.3 Executing actions at container start-up and shutdown

6.3.1 Using post-start hooks to perform actions when the container starts

6.3.2 Using pre-stop hooks to run a process just before the container terminates

6.4 Understanding the pod lifecycle

6.4.1 Understanding the initialization stage

6.4.2 Understanding the run stage

6.4.3 Understanding the termination stage

6.4.4 Visualizing the full lifecycle of the pod’s containers

6.5 Summary