consul icon indicating copy to clipboard operation
consul copied to clipboard

Servers can't agree on cluster leader after restart when gossiping on WAN

Open jippi opened this issue 12 years ago • 45 comments

Hi,

I'm running consul in a all "WAN" environment, one DC. All my boxes is in the same rack, but do not have a private lan to gossip over.

The first time they join each other, with an empty /opt/consul directory they manage to join and agree on a leader.

If I restart the cluster, they still connect and find each other - but they never seem to agree on a leader

Node            Address              Status  Type    Build  Protocol
consul01        195.1xx.35.xx1:8301  alive   server  0.4.1  2
consul02        195.1xx.35.xx2:8301  alive   server  0.4.1  2
consul03        195.1xx.35.xx3:8301  alive   server  0.4.1  2

they just keep repeating 2014/11/05 13:09:41 [ERR] agent: failed to sync remote state: No cluster leader in the consul monitor output

All nodes are started with /usr/local/bin/consul agent -config-dir /etc/consul

server 1

{
  "advertise_addr": "195.1xx.35.xx1",
  "bind_addr": "195.1xx.35.xx1",
  "bootstrap_expect": 3,
  "client_addr": "0.0.0.0",
  "data_dir": "/opt/consul",
  "datacenter": "online",
  "domain": "consul",
  "log_level": "INFO",
  "ports": {
    "dns": 53
  },
  "recursor": "8.8.8.8",
  "rejoin_after_leave": true,
  "retry_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "server": true,
  "start_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "ui_dir": "/opt/consul/ui"
}

server 2

{
  "advertise_addr": "195.1xx.35.xx2",
  "bind_addr": "195.1xx.35.xx2",
  "bootstrap_expect": 3,
  "client_addr": "0.0.0.0",
  "data_dir": "/opt/consul",
  "datacenter": "online",
  "domain": "consul",
  "log_level": "INFO",
  "ports": {
    "dns": 53
  },
  "recursor": "8.8.8.8",
  "rejoin_after_leave": true,
  "retry_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "server": true,
  "start_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "ui_dir": "/opt/consul/ui"
}

server 3

{
  "advertise_addr": "195.1xx.35.xx3",
  "bind_addr": "195.1xx.35.xx3",
  "bootstrap_expect": 3,
  "client_addr": "0.0.0.0",
  "data_dir": "/opt/consul",
  "datacenter": "online",
  "domain": "consul",
  "log_level": "INFO",
  "ports": {
    "dns": 53
  },
  "recursor": "8.8.8.8",
  "rejoin_after_leave": true,
  "retry_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "server": true,
  "start_join": [
    "195.1xx.35.xx1",
    "195.1xx.35.xx2",
    "195.1xx.35.xx3"
  ],
  "ui_dir": "/opt/consul/ui"
}

jippi avatar Nov 05 '14 13:11 jippi

Can you provide more log output from the servers after a restart? Specifically, those prefixed with "raft:" are of interest. Also you don't usually need retry_join with start_join since they serve an overlapping purpose with different semantics. retry_join will continuously retry until it succeeds, while start_join exits if the join fails.

armon avatar Nov 05 '14 18:11 armon

I am having the same issue.

Note that this only happens with bootstrap_expect. If I have a single bootstrap: true node, the cluster is able to elect a new leader after stopping all the nodes and then starting them all up.

Here's my steps and the logs: https://gist.github.com/adrienbrault/ad8d13802913b095415a

adrienbrault avatar Nov 21 '14 16:11 adrienbrault

It looks like both of you are forcing the cluster into an outage state. The bootstrap_expect is only used for an initial bootstrap process. Once the cluster has established quorum, it is not expected to ever loose it again. A loss of quorum requires manual intervention (See: https://www.consul.io/docs/guides/outage.html).

This is what is happening:

  • Initial cluster start, with bootstrap, leader elected (3 raft peers, quorum size 2)
  • Server 1 leaves, (2 raft peers, quorum size 2)
  • Server 2 leaves (1 raft peer, quorum size 1) <= Outage! Without -bootstrap a single node cluster is not allowed for safety reasons (avoid a split-brain in the cluster)
  • Server 3 leaves (No servers)

When you are starting all the servers again you now have 3 servers again. But this time there is no leader, and no quorum. At this point, it is unsafe for any of the servers to gain leadership (split-brain risks), they will sit there until an operator intervenes.

The best way is to avoid causing an outage. If you cause quorum to be lost, manual intervention is required. Any other approach on the part of Consul would introduce safety issues.

armon avatar Nov 21 '14 18:11 armon

Very interesting @armon! Thanks for the explanation.

hanshasselberg avatar Nov 21 '14 21:11 hanshasselberg

@armon What about being able to specify the expected quorum size ? It is up to the user to use a correct value, like it is for bootstrap_expect

adrienbrault avatar Nov 23 '14 11:11 adrienbrault

@adrienbrault Not currently. There are issues around changing the quorum size then once it is specified. The current approach I think makes the very reasonable trade off of being a zero-touch bootstrap, and zero-touch scale up and down as long as quorum isn't lost. With a sensible amount of redundancy, it should be incredibly unlikely that an operator needs to intervene.

armon avatar Nov 24 '14 19:11 armon

I can understand why the cluster can't currently recover from losing its quorum, but I too would like to see a way to allow automatic recovery by compromising elsewhere, eg having a fixed quorum size (or a fixed maximum number of Consul servers?) and losing the ability to zero-touch scale up and down. Here's our use case:

We have a bunch of servers running in AWS. I'd like to have Consul servers running on three of them, and Consul clients on the rest. So far so good. The catch is that we shut down all but one of the servers each night (to save money while they are idle). It seems that even if I put a Consul server on the lone surviving machine, I'd have to jump through some extra hoops to have the cluster recover each morning? I don't see us wanting to dynamically add additional Consul servers anytime soon but automatic recovery even after a complete shutdown (intentional or otherwise) would be very beneficial to us. Thoughts?

chrismiller avatar Dec 03 '14 21:12 chrismiller

Reported the exact same issue when I interrupted the servers in #476. I expected bootstrap_expect to handle this for me.

francois avatar Dec 06 '14 17:12 francois

Basically we can safely provide one of two things:

  • Scale up/down without operator intervention (change quorum size)
  • Fixed quorum size, bootstrap without operator intervention

I don't see a way for us to safely provide both. Currently bootstrap_expect is giving a bit of both in the very special case of an "empty" cluster (no previous leader) because there is no safety concerns there.

So we can have a flag like -expect=3 and we can automatically bootstrap / recover when 3 servers are available. In that world, scaling down to 1 down will cause an outage.

armon avatar Dec 08 '14 18:12 armon

Hi,

I'm doing some testing of consul and just ran into this myself, since restarting the cluster at once is trivial with config management tools. I would expect this type of user error to be frequent, especially with folks used to tools that regain quorum when possible (mongo, heartbeat, etc..). I think "never loose quorum" is not tenable in the long run.

It seems like the 80% use case is the "Nothing bad happened, I just restarted my cluster and would like it to return to operation." In that case it appears that consul already has most of the information required to do so without having to sacrifice the zero-touch scale or bootstrap? It knows the previous server list from raft/peers.json and could remember who the last leader was as a replicated fact. Quorum re-convergence then could simply be "all the previous peer members are gossiping again, and I was the previous leader, let's try an election" ?

-n

nathanhruby avatar May 11 '15 20:05 nathanhruby

Can we at least have steps documented somewhere for restoring the cluster in case we lose the quorum?

mohitarora avatar May 12 '15 02:05 mohitarora

@mohitarora Is that not covered here? http://www.consul.io/docs/guides/outage.html

ryanbreen avatar May 12 '15 02:05 ryanbreen

@ryanbreen That didn't help. Here is what i did

  • Started first node in bootstrap mode ( this node will self-elect as leader, creating a basis for forming the cluster.)
  • Started second and third node in non bootstrap mode which i call a normal server
  • I wanted each server on equal footing so I did shutdown the bootstrapped consul instance and then re-enter the cluster as a normal server.

Everything looks good at this point.

I forced the cluster to lose quorum by restarting 2 of the 3 nodes at same time. Nodes came back online but leader was not selected on its own.

I want to know what should be my next step here.

Should i again start node 1 in bootstrap mode and re-execute the steps mentioned above?

mohitarora avatar May 12 '15 02:05 mohitarora

I would suggest bootstrapping with --bootstrap-expect=3 instead of a -bootstrap, per this guide: http://www.consul.io/docs/guides/bootstrapping.html

ryanbreen avatar May 12 '15 02:05 ryanbreen

bootstrap-expect doesn't seem work after a node has entered and left a cluster and has vestigial raft data left in the datadir.

nathanhruby avatar May 12 '15 03:05 nathanhruby

Thanks @ryanbreen .

-bootstrap-expect is better than -bootstrap, I will start using that but both these are used when cluster is initialized.

I still need the steps for recovery once quorum is lost. In my case no leader was selected once all nodes came back to life after quorum loss.

mohitarora avatar May 12 '15 04:05 mohitarora

I also had a similar question to @mohitarora. If we do lose quorum, and there is no leader, what do we do?

saulshanabrook avatar May 12 '15 23:05 saulshanabrook

@saulshanabrook Then you're in an outage scenario (since you've lost quorum) and need to decide which server is authoritative, then follow the outage recovery guide.

One thing I've also found: If your leaders actually leave the cluster when shutdown cleanly (rather than kill -9 style killed), which is the default behavior, then they can't rejoin after a restart due to having notified the cluster they are leaving. @mohitarora try setting leave-on-terminate to false in your server config and then repeating your test of starting 3 servers then stopping 2 and bringing them back up.

highlyunavailable avatar May 12 '15 23:05 highlyunavailable

@highlyunavailable What if all three go down at once? How do I restart then?

saulshanabrook avatar May 13 '15 00:05 saulshanabrook

If they went down hard or if you had leave-on-terminate set to false you should be able to just start them back up in any order assuming their IPs didn't change. If they did you need to do outage recovery.

highlyunavailable avatar May 13 '15 00:05 highlyunavailable