Skip to content

Commit 8ddf12a

Browse files
authored
Merge pull request #26 from ncode/juliano/readme
docs: improve README with installation, usage, and architecture diagram
2 parents b5c38e2 + b15701c commit 8ddf12a

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

README.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,67 @@ Ballot leverages Consul's session and key-value (KV) store APIs to perform leade
2121
4. Tagging the Leader: The leader is tagged with a specified tag (e.g., primary) in the Consul catalog.
2222
5. Health Checks: The leader's health is continuously monitored. If the leader becomes unhealthy, the lock is released, and a new election occurs.
2323
6. Hooks Execution: Custom scripts or commands can be executed when a service is promoted to leader or demoted.
24+
25+
```mermaid
26+
sequenceDiagram
27+
participant B1 as Ballot Instance 1
28+
participant B2 as Ballot Instance 2
29+
participant C as Consul
30+
31+
B1->>C: Create Session
32+
B2->>C: Create Session
33+
B1->>C: Acquire Lock (KV)
34+
Note right of C: B1 wins lock
35+
B2->>C: Acquire Lock (KV)
36+
Note right of C: B2 fails, waits
37+
B1->>C: Tag service as "primary"
38+
B1->>B1: Execute execOnPromote
39+
loop Health Check
40+
C->>B1: Monitor health
41+
end
42+
Note over B1: B1 becomes unhealthy
43+
C-->>B1: Session invalidated
44+
B1->>B1: Execute execOnDemote
45+
B2->>C: Acquire Lock (KV)
46+
Note right of C: B2 wins lock
47+
B2->>C: Tag service as "primary"
48+
B2->>B2: Execute execOnPromote
49+
```
50+
2451
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).
2552

26-
### How do try it?
53+
### Requirements
54+
55+
- Go 1.21 or higher
56+
- Consul 1.10 or higher
57+
58+
### Installation
59+
60+
**Using Go:**
61+
```bash
62+
go install github.com/ncode/ballot@latest
63+
```
64+
65+
**From source:**
66+
```bash
67+
git clone https://github.com/ncode/ballot
68+
cd ballot
69+
go build -o ballot .
70+
```
71+
72+
### Usage
73+
74+
```bash
75+
# Run with a config file
76+
ballot run --config /path/to/config.yaml
77+
78+
# Default config location: $HOME/.ballot.yaml
79+
ballot run
80+
```
81+
82+
Ballot also supports environment variables via Viper. Any config option can be set using the `BALLOT_` prefix (e.g., `BALLOT_CONSUL_TOKEN`).
83+
84+
### How to try it?
2785

2886
1. Install Ballot
2987
```bash
@@ -55,7 +113,7 @@ $ touch consul/state/ready3
55113

56114
### Environment variables
57115

58-
During the call of execOnPromote and execOnDemote a few environment variables are injected incase you need to use the and port of the service for an intended automation.
116+
During the call of execOnPromote and execOnDemote a few environment variables are injected in case you need to use the address and port of the service for automation.
59117

60118
```bash
61119
$ADDRESS # IP Address of the current service elected
@@ -89,16 +147,40 @@ election:
89147
90148
### Development and examples
91149
92-
Examples of configuration files and service definitions can be found inside config/development
150+
Examples of configuration files and service definitions can be found inside `configs/development`.
151+
152+
### Production deployment
153+
154+
Ballot handles SIGINT and SIGTERM for graceful shutdown. Logs are output in JSON format to stdout.
155+
156+
**Systemd example:**
157+
```ini
158+
[Unit]
159+
Description=Ballot Leader Election
160+
After=network.target consul.service
161+
Wants=consul.service
162+
163+
[Service]
164+
Type=simple
165+
ExecStart=/usr/local/bin/ballot run --config /etc/ballot/config.yaml
166+
Restart=on-failure
167+
RestartSec=5
168+
169+
[Install]
170+
WantedBy=multi-user.target
171+
```
93172

173+
**Multiple elections:** Ballot can manage elections for multiple services simultaneously. Add multiple entries to `election.enabled` and configure each under `election.services`.
94174

95175
### Contributing
96176

97-
Contributions are welcome! Please open an issue or submit a pull request for any improvements, bug fixes, or new features.
177+
Contributions are welcome! Please:
178+
- Open an issue to discuss significant changes before submitting a PR
179+
- Include tests for bug fixes and new features
180+
- Run `go test ./...` and `go vet ./...` before submitting
98181

99182
### TODO
100183

101-
- Write more tests
102184
- Allow to pre-define the preferred leader
103185

104186
### License

0 commit comments

Comments
 (0)