https://github.com/quiqupltd/libelection
Library to perform leader election in a cluster of containerized Elixir nodes
https://github.com/quiqupltd/libelection
elixir kubernetes
Last synced: 4 months ago
JSON representation
Library to perform leader election in a cluster of containerized Elixir nodes
- Host: GitHub
- URL: https://github.com/quiqupltd/libelection
- Owner: quiqupltd
- License: gpl-3.0
- Created: 2018-02-12T16:57:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-08-23T05:07:39.000Z (almost 3 years ago)
- Last Synced: 2025-11-22T00:27:02.348Z (7 months ago)
- Topics: elixir, kubernetes
- Language: Elixir
- Size: 60.5 KB
- Stars: 12
- Watchers: 7
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

# Libelection
[](https://travis-ci.org/quiqupltd/libelection)
[](https://hex.pm/packages/libelection)
[](https://coveralls.io/repos/github/quiqupltd/libelection)
[](https://inch-ci.org/github/quiqupltd/libelection)
Library to perform leader election in a cluster of containerized Elixir nodes.
## Installation
```elixir
def deps do
[{:libelection, "~> 1.1.0"}]
end
```
## Usage
```elixir
if Election.leader? do
# Code path executed only by the leader node
else
# Code path executed by followers
end
```
## How it works
Polls the API of the configured container orchestration platform to determine the oldest node of the cluster.
To configure the polling interval use:
```elixir
config :libelection, :polling_interval, 2_000 # 2 seconds
```
To configure the function which lists the node of the cluster use:
```elixir
config :libelection, :list_nodes, {module, function, args}
# it can also be a function reference
config :libelection, :list_nodes, &SomeModule.some_function/1
```
Configure the logger
```elixir
config :libelection, :logger, %{level: :debug} # Default
```
*Note:* The configuration also supports [confex](https://hex.pm/packages/confex) style configurations.
## Election Strategies
### Rancher
The [`create_index`](http://rancher.com/docs/rancher/v1.2/en/rancher-services/metadata-service/#container) identifier is used to pick the leader.
```elixir
config :libelection,
strategy: Election.Strategy.Rancher,
rancher_node_basename: "some-app"
```
### Kubernetes
The [`resourceVersion`](https://kubernetes.io/docs/reference/generated/federation/v1/definitions/) identifier
is used to pick the leader.
```elixir
config :libelection,
strategy: Election.Strategy.Kubernetes,
kubernetes_selector: "app=some-app",
kubernetes_node_basename: "some-app"
```
## Documentation
* [exdoc](https://hexdocs.pm/libelection/)
* wiki (coming soon)
## License
[GPLv3](https://github.com/quiqupltd/libelection/blob/master/LICENSE.md)