# Probes

## [About](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/)

A **Probe** is a diagnostic mechanism that Kubernetes uses to **check the health of a container**.\
They run periodically and report the status back to the kubelet. Based on the probe result, Kubernetes can decide to:

* Restart a container
* Stop routing traffic to a container
* Mark the Pod as ready or not ready

## Types of Probes

### Liveness probe

* Checks if the container is **still running properly**.
* If it fails, Kubernetes **restarts the container**.
* Example: an app is running but deadlocked → liveness probe fails → kubelet restarts it.

{% hint style="info" %}
Ensures apps that get stuck or crash get restarted automatically.
{% endhint %}

#### Example

{% code title="deployment.yaml" %}

```yaml
...
spec:
  template:
    ...
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.25
          ports:
            - containerPort: 80
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8000
            initialDelaySeconds: 5
            periodSeconds: 10
```

{% endcode %}

### Readiness probe

* Checks if the container is **ready to accept traffic**.
* If it fails, the Pod is marked **unready** → Service will **stop sending traffic** to it.
* Example: your app takes 30 seconds to load caches. Until ready probe passes, no traffic is sent.

{% hint style="info" %}
Ensures traffic is only routed to healthy Pods.
{% endhint %}

{% code title="deployment.yaml" %}

```yaml
...
spec:
  template:
    ...
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.25
          ports:
            - containerPort: 80
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8000
            initialDelaySeconds: 5
            periodSeconds: 10
```

{% endcode %}

### Startup probe

* Checks if the container **has started successfully**.
* Especially useful for apps with **slow startup times**.
* Disables liveness and readiness checks until it succeeds.
* Prevents Kubernetes from killing apps that just need more time to boot.

{% hint style="info" %}
Ensures slow apps don’t get killed prematurely during initialization.
{% endhint %}

{% code title="deployment.yaml" %}

```yaml
...
spec:
  template:
    ...
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.25
          ports:
            - containerPort: 80
          startupProbe:
            httpGet:
              path: /healthz
              port: 8000
            initialDelaySeconds: 5
            periodSeconds: 10
```

{% endcode %}

## Probe Actions

Probes can use one of these mechanisms to check health:

### httpGet

Hits an HTTP endpoint in your container:

```yaml
httpGet:
  path: /healthz
  port: 8080
```

### tcpSocket

Tries to open a TCP connection:

```yaml
tcpSocket:
  port: 3306
```

### exec

Runs a command inside the container:

```yaml
exec:
  command: ["cat", "/tmp/healthy"]
```

## Probe Parameters

* `initialDelaySeconds`: how long to wait before starting checks.
* `periodSeconds`: how often to run the probe.
* `timeoutSeconds`: how long to wait before probe times out.
* `successThreshold`: how many successes before marking healthy.
* `failureThreshold`: how many failures before marking unhealthy.

## Example

{% code title="deployment.yaml" %}

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: myapp:1.0
          ports:
            - containerPort: 8080
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /ready
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 5
          startupProbe:
            httpGet:
              path: /startup
              port: 8080
            failureThreshold: 30
            periodSeconds: 10
```

{% endcode %}
