Skip to content

Commit 7e578b4

Browse files
authored
doc/user-guide: show how to enable both leader election options (#1052)
1 parent 972c2a2 commit 7e578b4

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

doc/user-guide.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,72 @@ func main() {
400400

401401
After adding new import paths to your operator project, run `dep ensure` in the root of your project directory to fulfill these dependencies.
402402

403+
## Leader election
404+
405+
During the lifecycle of an operator it's possible that there may be more than 1 instance running at any given time e.g when rolling out an upgrade for the operator.
406+
In such a scenario it is necessary to avoid contention between multiple operator instances via leader election so that only one leader instance handles the reconciliation while the other instances are inactive but ready to take over when the leader steps down.
407+
408+
There are two different leader election implementations to choose from, each with its own tradeoff.
409+
410+
- [Leader-for-life][leader_for_life]: The leader pod only gives up leadership (via garbage collection) when it is deleted. This implementation precludes the possibility of 2 instances mistakenly running as leaders (split brain). However, this method can be subject to a delay in electing a new leader. For instance when the leader pod is on an unresponsive or partitioned node, the [`pod-eviction-timeout`][pod_eviction_timeout] dictates how it takes for the leader pod to be deleted from the node and step down (default 5m).
411+
- [Leader-with-lease][leader_with_lease]: The leader pod periodically renews the leader lease and gives up leadership when it can't renew the lease. This implementation allows for a faster transition to a new leader when the existing leader is isolated, but there is a possibility of split brain in [certain situations][lease_split_brain].
412+
413+
By default the SDK enables the leader-for-life implementation. However you should consult the docs above for both approaches to consider the tradeoffs that make sense for your use case.
414+
415+
The following examples illustrate how to use the two options:
416+
417+
### Leader for life
418+
419+
A call to `leader.Become()` will block the operator as it retries until it can become the leader by creating the configmap named `memcached-operator-lock`.
420+
421+
```Go
422+
import (
423+
...
424+
"github.com/operator-framework/operator-sdk/pkg/leader"
425+
)
426+
427+
func main() {
428+
...
429+
err = leader.Become(context.TODO(), "memcached-operator-lock")
430+
if err != nil {
431+
log.Error(err, "Failed to retry for leader lock")
432+
os.Exit(1)
433+
}
434+
...
435+
}
436+
```
437+
If the operator is not running inside a cluster `leader.Become()` will simply return without error to skip the leader election since it can't detect the operator's namespace.
438+
439+
### Leader with lease
440+
441+
The leader-with-lease approach can be enabled via the [Manager Options][manager_options] for leader election.
442+
443+
```Go
444+
import (
445+
...
446+
"sigs.k8s.io/controller-runtime/pkg/manager"
447+
)
448+
449+
func main() {
450+
...
451+
opts := manager.Options{
452+
...
453+
LeaderElection: true,
454+
LeaderElectionID: "memcached-operator-lock"
455+
}
456+
mgr, err := manager.New(cfg, opts)
457+
...
458+
}
459+
```
460+
461+
When the operator is not running in a cluster, the Manager will return an error on starting since it can't detect the operator's namespace in order to create the configmap for leader election. You can override this namespace by setting the Manager's `LeaderElectionNamespace` option.
462+
463+
464+
[pod_eviction_timeout]: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-controller-manager/#options
465+
[manager_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/manager#Options
466+
[lease_split_brain]: https://github.com/kubernetes/client-go/blob/30b06a83d67458700a5378239df6b96948cb9160/tools/leaderelection/leaderelection.go#L21-L24
467+
[leader_for_life]: https://godoc.org/github.com/operator-framework/operator-sdk/pkg/leader
468+
[leader_with_lease]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/leaderelection
403469
[memcached_handler]: ../example/memcached-operator/handler.go.tmpl
404470
[memcached_controller]: ../example/memcached-operator/memcached_controller.go.tmpl
405471
[layout_doc]:./project_layout.md

0 commit comments

Comments
 (0)