# K8s Liveliness

Liveliness in Kubernetes is all about ensuring that your containers are not just running but actually functional. Think of it as a regular "are you okay?" check that Kubernetes sends to each container. If a container stops responding or gets stuck, Kubernetes will notice and restart it to keep things running smoothly.

## **How Does Liveliness Work?**

Kubernetes uses something called liveness probes to check the health of your [containers](/content/finops-glossary/kubernetes-containers/index.html). These probes act like a pulse check, ensuring the container is alive and kicking.

Here’s how they do it:

1. **HTTP Probes:**

Kubernetes pings a specific URL inside your container. If it gets a valid response, everything’s good. If not, Kubernetes steps in to fix it.
2. **Command Probes:**

A specific command runs inside the container. If it exits with a success code, the container is healthy. If it doesn’t, Kubernetes knows there’s trouble.
3. **TCP Socket Probes:**

Kubernetes attempts to connect to the container on a specified port. If the connection is successful, the container passes the check.

When a probe fails, Kubernetes doesn’t just shrug—it restarts the container to give it another chance.

## **Why Does Liveliness Matter?**

Picture this: Your app hits a glitch, gets stuck in a loop, or outright crashes. Without liveliness checks, that container would just sit there, doing nothing while your users are left frustrated.

Liveliness ensures Kubernetes can detect when a container isn’t working properly and fix it, often before you even notice there’s a problem.

## **Setting Up a Liveness Probe**

Here’s what a liveness probe looks like in a Kubernetes configuration:

```none
livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
```  
In this setup, Kubernetes checks the `/health` endpoint every 10 seconds, starting 5 seconds after the container launches. If the endpoint doesn’t respond, Kubernetes knows it’s time for a restart.

## **Liveliness vs. Readiness: What’s the Difference?**

Here’s where things get interesting: liveliness checks if a container is alive, while readiness checks if it’s ready to do its job.

- A container might pass a liveness probe but fail a readiness probe if, for example, it’s still starting up or waiting for a dependency.
- If a liveness probe fails, Kubernetes restarts the container. If a readiness probe fails, Kubernetes just stops sending traffic to it.

Think of it this way: Liveliness is about survival, and readiness is about being prepared.

## **Common Issues with Liveliness Probes**

1. **False Positives:**

A poorly configured probe can restart containers unnecessarily, creating more problems than it solves.
2. **Performance Impact:**

Probes add a bit of load to your app. Running them too frequently can cause minor performance dips.
3. **Application Complexity:**

Writing accurate probes requires a deep understanding of your app’s behavior, which can be tricky for complex systems.

## **Resources for Further Reading**

- [Liveness Probes in Kubernetes Docs](https://kubernetes.io/docs/concepts/configuration/liveness-readiness-startup-probes/)
- [Understanding Pod Lifecycle and Probes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/)
