Documentation ¶
Overview ¶
consul-skipper is a library for a cluster leader election using Consul KV. This needs to be attached to the Consul Agent and then it runs leader election from there.
import "github.com/darkcrux/consul-skipper" candidate := &skipper.Candidate{ ConsulAddress: "10.0.0.10:8500", ConsulDatacenter: "dc1", LeadershipKey: "app/leader", } candidate.RunForElection()
Running for election runs asynchronously and will keep running as long as the main application is running. To check if the current attached agent is the current leader, use:
skipper.IsLeader()
It is also possible to force a leader to step down forcing a re-election.
skipper.Resign()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Candidate ¶
type Candidate struct { ConsulAddress string // The address of the consul agent. This defaults to 127.0.0.1:8500. ConsulDatacenter string // The datacenter to connect to. This defaults to the config used by the agent. LeadershipKey string // The leadership key. This needs to be a proper Consul KV key. eg. app/leader // contains filtered or unexported fields }
func (*Candidate) Leader ¶
Leader returns the node of the current cluster leader. This returns an empty string if there is no leader.
func (*Candidate) Resign ¶
func (c *Candidate) Resign()
Resign forces the current agent to step down as the leader forcing a re-election. Nothing happens if the agent is not the current leader.
func (*Candidate) RunForElection ¶
func (c *Candidate) RunForElection()
RunForElection makes the candidate run for leadership against the other nodes in the cluster. This tries and acquire the lock of the given LeadershipKey and setting it's value with the current node. If the lock acquisition passes, then the node where the agent is running is now the new leader. A re-election will occur when there are changes in the LeadershipKey.