n-marton / ballot

Consul based leader election with tagging support and hooks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ballot

Go Go Report Card License

Consul based leader election with tagging support and hooks

What is it?

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.

How do I test it?

  1. Install Ballot
$ git clone https://github.com/ncode/ballot
$ go build
  1. Run consul in dev mode and register two services
$ consul agent -dev -enable-script-checks=true &
$ curl --request PUT --data @examples/consul/my-service1.json http://127.0.0.1:8500/v1/agent/service/register\?replace-existing-checks\=true
$ curl --request PUT --data @examples/consul/my-service2.json http://127.0.0.1:8500/v1/agent/service/register\?replace-existing-checks\=true
  1. Run one instance of Ballot for each service
$ ./ballot run --config $PWD/examples/config/ballot1.yaml &
$ ./ballot run --config $PWD/examples/config/ballot2.yaml &
  1. Open consul ui http://127.0.0.1:8500/ui/dc1/services/my_service/instances
  2. Play with the health checks and see the election happening and moving
$ cp /bin/ls /tmp/lalala1
$ cp /bin/ls /tmp/lalala2
$ sleep 30
$ rm /tmp/lalala1
$ sleep 10
$ cp /bin/ls /tmp/lalala1
$ rm /tmp/lalala2
$ sleep 10
$ cp /bin/ls /tmp/lalala2

Environment variables

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.

$ADDRESS   # IP Address of the current service elected
$PORT      # Port of the service
$SESSIONID # Current SessionID of the elected master

TODO:

  • Write tests
  • Allow to pre-define the preferred leader

About

Consul based leader election with tagging support and hooks

License:Apache License 2.0


Languages

Language:Go 100.0%