|
| 1 | +--- |
| 2 | +title: Health Probes |
| 3 | +weight: 85 |
| 4 | +--- |
| 5 | + |
| 6 | +Operators running in Kubernetes should expose health probe endpoints so that the kubelet can detect startup |
| 7 | +failures and runtime degradation. JOSDK provides the building blocks through its |
| 8 | +[`RuntimeInfo`](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/RuntimeInfo.java) |
| 9 | +API. |
| 10 | + |
| 11 | +## RuntimeInfo |
| 12 | + |
| 13 | +`RuntimeInfo` is available via `operator.getRuntimeInfo()` and exposes: |
| 14 | + |
| 15 | +| Method | Purpose | |
| 16 | +|---|---| |
| 17 | +| `isStarted()` | `true` once the operator and all its controllers have fully started | |
| 18 | +| `allEventSourcesAreHealthy()` | `true` when every registered event source (informers, polling sources, etc.) reports a healthy status | |
| 19 | +| `unhealthyEventSources()` | returns a map of controller name → unhealthy event sources, useful for diagnostics | |
| 20 | +| `unhealthyInformerWrappingEventSourceHealthIndicator()` | returns a map of controller name → unhealthy informer-wrapping event sources, each exposing per-informer details via `InformerHealthIndicator` (`hasSynced()`, `isWatching()`, `isRunning()`, `getTargetNamespace()`) | |
| 21 | + |
| 22 | +In most cases a single readiness probe backed by `allEventSourcesAreHealthy()` is sufficient: before the |
| 23 | +operator has fully started the informers will not have synced yet, so the check naturally covers the startup |
| 24 | +case as well. Once running, it detects runtime degradation such as a lost watch connection. |
| 25 | + |
| 26 | +### Fine-Grained Informer Diagnostics |
| 27 | + |
| 28 | +For advanced use cases — such as exposing per-informer health in a diagnostic endpoint or logging which |
| 29 | +specific namespace lost its watch — `unhealthyInformerWrappingEventSourceHealthIndicator()` gives access to |
| 30 | +individual `InformerHealthIndicator` instances. Each indicator exposes `hasSynced()`, `isWatching()`, |
| 31 | +`isRunning()`, and `getTargetNamespace()`. This is typically not needed for a standard health probe but can |
| 32 | +be valuable for operational dashboards or troubleshooting. |
| 33 | + |
| 34 | +## Setting Up a Probe Endpoint |
| 35 | + |
| 36 | +The example below uses [Jetty](https://eclipse.dev/jetty/) to expose a `/healthz` endpoint. Any HTTP |
| 37 | +server library works — the key is calling the `RuntimeInfo` methods to determine the response code. |
| 38 | + |
| 39 | +```java |
| 40 | +import org.eclipse.jetty.server.Server; |
| 41 | +import org.eclipse.jetty.server.handler.ContextHandler; |
| 42 | + |
| 43 | +Operator operator = new Operator(); |
| 44 | +operator.register(new MyReconciler()); |
| 45 | + |
| 46 | +// start the health server before the operator so probes can be queried during startup |
| 47 | +var health = new ContextHandler(new HealthHandler(operator), "/healthz"); |
| 48 | +Server server = new Server(8080); |
| 49 | +server.setHandler(health); |
| 50 | +server.start(); |
| 51 | + |
| 52 | +operator.start(); |
| 53 | +``` |
| 54 | + |
| 55 | +Where `HealthHandler` extends `org.eclipse.jetty.server.Handler.Abstract` and checks |
| 56 | +`operator.getRuntimeInfo().allEventSourcesAreHealthy()`. |
| 57 | + |
| 58 | +See the |
| 59 | +[`operations` sample operator](https://github.com/java-operator-sdk/java-operator-sdk/tree/main/sample-operators/operations) |
| 60 | +for a complete working example. |
| 61 | + |
| 62 | +## Kubernetes Deployment Configuration |
| 63 | + |
| 64 | +Once your operator exposes the probe endpoint, configure probes in your Deployment manifest. Both the |
| 65 | +startup and readiness probes can point to the same `/healthz` endpoint — the startup probe simply uses a |
| 66 | +higher `failureThreshold` to give the operator time to initialize: |
| 67 | + |
| 68 | +```yaml |
| 69 | +containers: |
| 70 | +- name: operator |
| 71 | + ports: |
| 72 | + - name: probes |
| 73 | + containerPort: 8080 |
| 74 | + startupProbe: |
| 75 | + httpGet: |
| 76 | + path: /healthz |
| 77 | + port: probes |
| 78 | + initialDelaySeconds: 1 |
| 79 | + periodSeconds: 3 |
| 80 | + failureThreshold: 20 |
| 81 | + readinessProbe: |
| 82 | + httpGet: |
| 83 | + path: /healthz |
| 84 | + port: probes |
| 85 | + initialDelaySeconds: 5 |
| 86 | + periodSeconds: 5 |
| 87 | + failureThreshold: 3 |
| 88 | +``` |
| 89 | +
|
| 90 | +The startup probe gives the operator time to start (up to ~60 s with the settings above). Once the startup |
| 91 | +probe succeeds, the readiness probe takes over and will mark the pod as not-ready if any event source |
| 92 | +becomes unhealthy. |
| 93 | +
|
| 94 | +## Helm Chart Support |
| 95 | +
|
| 96 | +The [generic Helm chart](/docs/documentation/operations/helm-chart) supports health probes out of the box. |
| 97 | +Enable them in your `values.yaml`: |
| 98 | + |
| 99 | +```yaml |
| 100 | +probes: |
| 101 | + port: 8080 |
| 102 | + startup: |
| 103 | + enabled: true |
| 104 | + path: /healthz |
| 105 | + readiness: |
| 106 | + enabled: true |
| 107 | + path: /healthz |
| 108 | +``` |
| 109 | + |
| 110 | +All probe timing parameters (`initialDelaySeconds`, `periodSeconds`, `failureThreshold`) have sensible |
| 111 | +defaults and can be overridden. |
0 commit comments