DS87
DS87

Reputation: 602

Change Leader in Docker Swarm

I have a docker swarm environment with three managers. They are named swarm1, swarm2 and swarm3. Due to different circumstances (e.g. network and resources), swarm1 was set as the leader and should stay as the leader. However, after a resource upgrade, swarm1 was rebooted. This led to that swarm2 is set as the leader and swarm1 has now the status reachable. How is it possible to set again swarm1 to the leader?

Upvotes: 6

Views: 20911

Answers (2)

MaduKan
MaduKan

Reputation: 670

I'll attempt to write the whole scenario, so everything is in one place about changing the leader and reverting.

Scenario: Three nodes are swarm1, swarm2 and swarm3. First leader (that needs resource upgrade is swarm1)

Step 1 Make swarm2 as leader

docker node promote swarm2

docker node ls

Make sure that the swarm2 Manager status is Reachable (Not Down)

Step 2 Demote swarm1 now to a worker

docker node demote swarm1

docker node ls

Ensure Manager status for swarm1 is now blank.

You may remove the node from swarm.

From swarm1 node, issue:

docker swarm leave

Do the necessary upgrades to the swarm1 node and join it back to the swarm.

Step 3

Rejoin to the swarm and change lead.

From swarm2 issue

docker swarm join-token manager

From swarm1 node issue the join command

eg:

docker swarm join --token <token> <ip:port>

Step 4

Now re-elect the leader.

From swarm1 issue:

docker node demote swarm2

docker node ls

Now you should see your old configuration with swarm1 as leader.

Upvotes: 5

PowerShellGuy
PowerShellGuy

Reputation: 801

With swarm managers, it's bad practice to have a "special" node that needs to be the leader at all times. All of your manager nodes should be as identical as possible. But, to answer your question, there is no way to manually set the swarm leader. However, what you can do is docker node demote the leader (swarm2), and the other manager (swarm3). Once the managers are demoted to workers, swarm1 by default becomes the leader. Then all you have to do is docker node promote swarm2 and swarm3.

Upvotes: 4

Related Questions