Skip to content

Commit 333b166

Browse files
committed
update gitignore and README.md
1 parent 15ced93 commit 333b166

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
# Ide
1515
.idea*
1616

17+
# .DS_Store
18+
.DS_Store
19+
1720
# Dependency directories (remove the comment below to include it)
1821
# vendor/

README.md

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,52 @@
55
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
66
[![codecov](https://codecov.io/gh/ncode/ballot/graph/badge.svg?token=GVADXAIACR)](https://codecov.io/gh/ncode/ballot)
77

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.
99

1010
### What is it?
1111

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.
1313

1414
### How does it work?
1515

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:
1817

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?
2027

2128
1. Install Ballot
2229
```bash
2330
$ 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
3132
```
32-
3. Run one instance of Ballot for each service
33+
2. Run the development setup
3334
```bash
34-
$ ./ballot run --config $PWD/examples/config/ballot1.yaml &
35-
$ ./ballot run --config $PWD/examples/config/ballot2.yaml &
35+
$ make &
3636
```
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
3939
```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
4446
$ sleep 10
45-
$ cp /bin/ls /tmp/lalala1
46-
$ rm /tmp/lalala2
47+
$ touch consul/state/ready1
48+
$ rm consul/state/ready2
4749
$ 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
4954
```
5055

5156
### Environment variables
@@ -82,9 +87,20 @@ election:
8287
lockDelay: 5s # Lock delay for the session
8388
```
8489
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
86100
87101
- Write more tests
88-
- Add more examples
89102
- 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

Comments
 (0)