You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-5Lines changed: 87 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,67 @@ Ballot leverages Consul's session and key-value (KV) store APIs to perform leade
21
21
4. Tagging the Leader: The leader is tagged with a specified tag (e.g., primary) in the Consul catalog.
22
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
23
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
+
24
51
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
52
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?
27
85
28
86
1. Install Ballot
29
87
```bash
@@ -55,7 +113,7 @@ $ touch consul/state/ready3
55
113
56
114
### Environment variables
57
115
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.
59
117
60
118
```bash
61
119
$ADDRESS# IP Address of the current service elected
@@ -89,16 +147,40 @@ election:
89
147
90
148
### Development and examples
91
149
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
+
```
93
172
173
+
**Multiple elections:** Ballot can manage elections for multiple services simultaneously. Add multiple entries to `election.enabled` and configure each under `election.services`.
94
174
95
175
### Contributing
96
176
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
0 commit comments