|
5 | 5 | [](https://opensource.org/licenses/Apache-2.0) |
6 | 6 | [](https://codecov.io/gh/ncode/ballot) |
7 | 7 |
|
8 | | -Consul based leader election with tagging support and hooks |
| 8 | +Consul-based leader election tool with tagging support and hooks for automated tasks on leadership changes. |
9 | 9 |
|
10 | 10 | ### What is it? |
11 | 11 |
|
12 | | -Consul lacks a built-in feature for leader election among registered services. This tool is designed to fill that gap. It functions by designating a leader among multiple services, marking the chosen leader with a specified tag. Additionally, it allows for the execution of a script whenever a leader election occurs. |
| 12 | +Ballot fills a gap in Consul's functionality by providing a built-in feature for leader election among registered services. It designates a leader among multiple services, marks the chosen leader with a specified tag, and allows for the execution of a script whenever a leader election occurs. |
13 | 13 |
|
14 | 14 | ### How does it work? |
15 | 15 |
|
16 | | -Ballot uses Consul's session API to create a session for each service. The session is then used to create a lock on a key. The service that successfully creates the lock is elected as the leader. The leader is then tagged with a specified tag. The leader election is monitored and the leader is updated if the current leader is no longer healthy. |
17 | | -More info about the sessions here [https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections](https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections). |
| 16 | +Ballot leverages Consul's session and key-value (KV) store APIs to perform leader election: |
18 | 17 |
|
19 | | -### How do I use it? |
| 18 | +1. Session Creation: Each service instance creates a Consul session. |
| 19 | +2. Lock Acquisition: Instances attempt to acquire a lock on a predefined Consul KV key using their session. |
| 20 | +3. Leader Election: The instance that successfully acquires the lock is elected as the leader. |
| 21 | +4. Tagging the Leader: The leader is tagged with a specified tag (e.g., primary) in the Consul catalog. |
| 22 | +5. Health Checks: The leader's health is continuously monitored. If the leader becomes unhealthy, the lock is released, and a new election occurs. |
| 23 | +6. Hooks Execution: Custom scripts or commands can be executed when a service is promoted to leader or demoted. |
| 24 | +More information about Consul sessions and leader elections can be found in [https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections](https://developer.hashicorp.com/consul/tutorials/developer-configuration/application-leader-elections). |
| 25 | + |
| 26 | +### How do try it? |
20 | 27 |
|
21 | 28 | 1. Install Ballot |
22 | 29 | ```bash |
23 | 30 | $ git clone https://github.com/ncode/ballot |
24 | | -$ go build |
25 | | -``` |
26 | | -2. Run consul in dev mode and register two services |
27 | | -```bash |
28 | | -$ consul agent -dev -enable-script-checks=true & |
29 | | -$ curl --request PUT --data @examples/consul/my-service1.json http://127.0.0.1:8500/v1/agent/service/register\?replace-existing-checks\=true |
30 | | -$ curl --request PUT --data @examples/consul/my-service2.json http://127.0.0.1:8500/v1/agent/service/register\?replace-existing-checks\=true |
| 31 | +$ cd configs/development |
31 | 32 | ``` |
32 | | -3. Run one instance of Ballot for each service |
| 33 | +2. Run the development setup |
33 | 34 | ```bash |
34 | | -$ ./ballot run --config $PWD/examples/config/ballot1.yaml & |
35 | | -$ ./ballot run --config $PWD/examples/config/ballot2.yaml & |
| 35 | +$ make & |
36 | 36 | ``` |
37 | | -4. Open consul ui http://127.0.0.1:8500/ui/dc1/services/my_service/instances |
38 | | -5. Play with the health checks and see the election happening and moving |
| 37 | +3. Open consul ui http://127.0.0.1:8500/ui/dc1/services/my-service/instances |
| 38 | +4. Play with the health checks and see the election happening and moving |
39 | 39 | ```bash |
40 | | -$ cp /bin/ls /tmp/lalala1 |
41 | | -$ cp /bin/ls /tmp/lalala2 |
42 | | -$ sleep 30 |
43 | | -$ rm /tmp/lalala1 |
| 40 | +$ find consul/state |
| 41 | +consul/state |
| 42 | +consul/state/ready1 |
| 43 | +consul/state/ready3 |
| 44 | +consul/state/ready2 |
| 45 | +$ rm consul/state/ready1 |
44 | 46 | $ sleep 10 |
45 | | -$ cp /bin/ls /tmp/lalala1 |
46 | | -$ rm /tmp/lalala2 |
| 47 | +$ touch consul/state/ready1 |
| 48 | +$ rm consul/state/ready2 |
47 | 49 | $ sleep 10 |
48 | | -$ cp /bin/ls /tmp/lalala2 |
| 50 | +$ touch consul/state/ready2 |
| 51 | +$ rm consul/state/ready3 |
| 52 | +$ sleep 10 |
| 53 | +$ touch consul/state/ready3 |
49 | 54 | ``` |
50 | 55 |
|
51 | 56 | ### Environment variables |
@@ -82,9 +87,20 @@ election: |
82 | 87 | lockDelay: 5s # Lock delay for the session |
83 | 88 | ``` |
84 | 89 |
|
85 | | -### TODO: |
| 90 | +### Development and examples |
| 91 | +
|
| 92 | +Examples of configuration files and service definitions can be found inside config/development |
| 93 | +
|
| 94 | +
|
| 95 | +### Contributing |
| 96 | +
|
| 97 | +Contributions are welcome! Please open an issue or submit a pull request for any improvements, bug fixes, or new features. |
| 98 | +
|
| 99 | +### TODO |
86 | 100 |
|
87 | 101 | - Write more tests |
88 | | -- Add more examples |
89 | 102 | - Allow to pre-define the preferred leader |
90 | | -- Update the docks with the lock delays and timeouts |
| 103 | +
|
| 104 | +### License |
| 105 | +
|
| 106 | +This project is licensed under the Apache License 2.0. See the LICENSE file for details. |
0 commit comments