Relevant Documentation
Container Health
Kubernetes (K8s) provides several features to build robust solutions, including the ability to automatically restart unhealthy containers. To leverage these features effectively, it is essential for K8s to accurately determine the status of your applications. This requires actively monitoring container health.
Liveness Probes
Liveness probes enable automatic detection of whether a container application is in a healthy state or not. By default, K8s considers a container to be "down" only if the container process stops. However, liveness probes allow customization of this detection mechanism, making it more sophisticated.
Startup Probes
Startup probes are similar to liveness probes, but with a different timing. While liveness probes run continuously on a schedule, startup probes only run at container startup and stop once they succeed. These probes are useful for determining when an application has successfully started, particularly for legacy applications that may have long startup times.
Readiness Probes
Readiness probes are used to determine when a container is ready to accept requests. When a service is backed by multiple container endpoints, user traffic is only directed to a particular pod when all its containers pass the readiness checks defined by their readiness probes. Readiness probes help prevent user traffic from being sent to pods that are still in the process of starting up.
Utilizing liveness probes, startup probes, and readiness probes collectively ensures that containerized applications in Kubernetes maintain optimal health and availability.
Lesson Reference
Create a pod with a command-based liveness probe.
- Create a YAML file named
liveness-pod.yml
and open it for editing:
vi liveness-pod.yml
- Add the following content to the file:
apiVersion: v1 kind: Pod metadata: name: liveness-pod spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'while true; do sleep 3600; done'] livenessProbe: exec: command: ["echo", "Hello, world!"] initialDelaySeconds: 5 periodSeconds: 5
- Save and close the file.
- Create the pod using the YAML file:
kubectl create -f liveness-pod.yml
- Check the status of the pod:
kubectl get pod liveness-pod
Create a pod with an HTTP-based liveness probe.
- Create a YAML file named
liveness-pod-http.yml
and open it for editing:
vi liveness-pod-http.yml
- Add the following content to the file:
apiVersion: v1 kind: Pod metadata: name: liveness-pod-http spec: containers: - name: nginx image: nginx:1.19.1 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 5
- Save and close the file.
- Create the pod using the YAML file:
kubectl create -f liveness-pod-http.yml
- Check the status of the pod:
kubectl get pod liveness-pod-http
Create a pod with a startup probe.
- Create a YAML file named
startup-pod.yml
and open it for editing:
vi startup-pod.yml
- Add the following content to the file:
apiVersion: v1 kind: Pod metadata: name: startup-pod spec: containers: - name: nginx image: nginx:1.19.1 startupProbe: httpGet: path: / port: 80 failureThreshold: 30 periodSeconds: 10
- Save and close the file.
- Create the pod using the YAML file:
kubectl create -f startup-pod.yml
- Check the status of the pod:
kubectl get pod startup-pod
Create a pod with a readiness probe.
- Create a YAML file named
readiness-pod.yml
and open it for editing:
vi readiness-pod.yml
- Add the following content to the file:
apiVersion: v1 kind: Pod metadata: name: readiness-pod spec: containers: - name: nginx image: nginx:1.19.1 readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 5
- Save and close the file.
- Create the pod using the YAML file:
kubectl create -f readiness-pod.yml
- Check the status of the pod:
kubectl get pod readiness-pod