# Health checks

Source: https://openpanel.sh/docs/projects/health-checks

How a health check decides when a replica is ready, during a deploy and after it.

A health check tells OpenPanel when a replica of a service is ready for traffic. The OpenPanel agent on each server runs it against the replica's container, from the server itself, so the port it checks does not need to be public and the image needs no tool such as `curl`.

## What it decides [#what-it-decides]

- **During a deploy**, the new replicas start beside the old ones. The old ones stop only once every new replica is ready, so a version that never becomes healthy never takes over. A service with a volume is the exception: its old replica stops first. See [The single-instance rule](/docs/volumes/single-instance).
- **The deployment** counts as running when every replica's container runs and is healthy.
- **OpenPanel's edge** sends requests and connections only to replicas that are ready, on every server.

## The two kinds [#the-two-kinds]

| Kind | Healthy when |
|---|---|
| **HTTP** | A `GET` to the path on that port answers with a status from 200 to 399. A redirect counts as an answer and is not followed. The request carries `User-Agent: openpanel-agent-health`. |
| **TCP** | A connection to that port opens. |

The port must be one of the service's ports: an HTTP check needs an `http` port, a TCP check an `http` or `tcp` port.

## Timing [#timing]

| Setting | Default | Range |
|---|---|---|
| `intervalSeconds`, time between checks | 10 | 2 to 300 |
| `timeoutSeconds`, how long one check may take | 5 | 1 to 60 |
| `retries`, failed checks in a row before the replica is unhealthy | 3 | 1 to 10 |

A replica is **starting** until its first check passes, **healthy** after a check passes, and **unhealthy** after `retries` failed checks in a row. One passing check makes it healthy again. An unhealthy replica is not restarted; it gets traffic again once it passes a check.

## Set one [#set-one]

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Open the project and click the service on the canvas. In the **Health check** card of its **Settings** tab, choose **HTTP** or **TCP**, its **Port**, and for HTTP the **Path**. **Interval**, **Timeout** and **Retries** are optional. Click **Save**, then deploy.
</Tab>

<Tab value="CLI">
```sh
openpanel services update web --project shop --health http:3000:/health
openpanel services update db --project shop --health tcp:5432
```

The CLI sets the kind, port and path, with the default timing.
</Tab>

<Tab value="API">
```sh
curl -X PATCH https://openpanel.sh/api/v1/services/$SERVICE_ID \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "healthCheck": {
      "kind": "http", "port": 3000, "path": "/health",
      "intervalSeconds": 5, "timeoutSeconds": 2, "retries": 3
    }
  }'
```

`"healthCheck": null` removes it. The CLI cannot remove one yet.
</Tab>
</Tabs>

Like every setting, a health check takes effect at the next deploy.

## Without a health check [#without-a-health-check]

A replica counts as ready as soon as its container runs. If its image declares its own Docker `HEALTHCHECK`, the replica is ready once Docker reports it healthy.

A container that runs is not always one that answers: without a check, traffic can reach a new replica before the app inside it listens. Set a check on every service that serves traffic.

## When a deploy does not become healthy [#when-a-deploy-does-not-become-healthy]

While a new replica fails its check, the old replicas go on serving. OpenPanel marks the deployment **failed** when:

- its image cannot be pulled, or its container cannot be made;
- the server refuses the change;
- its container exits three times;
- or it is still not ready 15 minutes after the rollout began. The deployment's error then says why, for example "Not ready after 15 minutes: it failed its health check."

The service then goes back to the deployment that ran before. See [Deploy](/docs/deploy).

<Cards>
<Card href="/docs/projects/settings" title="Service settings" description="Every setting a service has." />

<Card href="/docs/load-balancing/replicas" title="Replicas" description="Where replicas run and how a rollout replaces them." />
</Cards>