|
| 1 | +--- |
| 2 | +title: Leader Election |
| 3 | +weight: 89 |
| 4 | +--- |
| 5 | + |
| 6 | +When running multiple replicas of an operator for high availability, leader election ensures that |
| 7 | +only one instance actively reconciles resources at a time. JOSDK uses Kubernetes |
| 8 | +[Lease](https://kubernetes.io/docs/concepts/architecture/leases/) objects for leader election. |
| 9 | + |
| 10 | +## Enabling Leader Election |
| 11 | + |
| 12 | +### Programmatic Configuration |
| 13 | + |
| 14 | +```java |
| 15 | +var operator = new Operator(o -> o.withLeaderElectionConfiguration( |
| 16 | + new LeaderElectionConfiguration("my-operator-lease", "operator-namespace"))); |
| 17 | +``` |
| 18 | + |
| 19 | +Or using the builder for full control: |
| 20 | + |
| 21 | +```java |
| 22 | +import static io.javaoperatorsdk.operator.api.config.LeaderElectionConfigurationBuilder.aLeaderElectionConfiguration; |
| 23 | + |
| 24 | +var config = aLeaderElectionConfiguration("my-operator-lease") |
| 25 | + .withLeaseNamespace("operator-namespace") |
| 26 | + .withIdentity(System.getenv("POD_NAME")) |
| 27 | + .withLeaseDuration(Duration.ofSeconds(15)) |
| 28 | + .withRenewDeadline(Duration.ofSeconds(10)) |
| 29 | + .withRetryPeriod(Duration.ofSeconds(2)) |
| 30 | + .build(); |
| 31 | + |
| 32 | +var operator = new Operator(o -> o.withLeaderElectionConfiguration(config)); |
| 33 | +``` |
| 34 | + |
| 35 | +### External Configuration |
| 36 | + |
| 37 | +Leader election can also be configured via properties (e.g. environment variables or a config file). |
| 38 | + |
| 39 | +See details under [configurations](configuration.md) page. |
| 40 | + |
| 41 | +## How It Works |
| 42 | + |
| 43 | +1. When leader election is enabled, the operator starts but **does not process events** until it acquires |
| 44 | + the lease. |
| 45 | +2. Once leadership is acquired, event processing begins normally. |
| 46 | +3. If leadership is lost (e.g. the leader pod becomes unresponsive), another instance acquires the lease |
| 47 | + and takes over reconciliation. The instance that lost the lead is terminated (`System.exit()`) |
| 48 | + |
| 49 | +### Identity and Namespace Inference |
| 50 | + |
| 51 | +If not explicitly set: |
| 52 | +- **Identity** is resolved from the `HOSTNAME` environment variable, then the pod name, falling back to a |
| 53 | + random UUID. |
| 54 | +- **Lease namespace** defaults to the namespace the operator pod is running in. |
| 55 | + |
| 56 | +## RBAC Requirements |
| 57 | + |
| 58 | +The operator's service account needs permissions to manage Lease objects: |
| 59 | + |
| 60 | +```yaml |
| 61 | +- apiGroups: ["coordination.k8s.io"] |
| 62 | + resources: ["leases"] |
| 63 | + verbs: ["create", "update", "get"] |
| 64 | +``` |
| 65 | +
|
| 66 | +JOSDK checks for these permissions at startup and throws a clear error if they are missing. |
| 67 | +
|
| 68 | +## Sample E2E Test |
| 69 | +
|
| 70 | +A complete working example is available in the |
| 71 | +[`leader-election` sample operator](https://github.com/java-operator-sdk/java-operator-sdk/tree/main/sample-operators/leader-election), |
| 72 | +including multi-replica deployment manifests and an E2E test that verifies failover behavior. |
0 commit comments