firelyu
firelyu

Reputation: 2222

How to remove node from swarm?

I added three nodes to a swarm cluster with static file mode. I want to remove host1 from the cluster. But I don't find a docker swarm remove command:

Usage: swarm [OPTIONS] COMMAND [arg...]
Commands:
  create, c Create a cluster
  list, l   List nodes in a cluster
  manage, m Manage a docker cluster
  join, j   join a docker cluster
  help, h   Shows a list of commands or help for one command

How can I remove the node from the swarm?

Upvotes: 8

Views: 13314

Answers (5)

André Carvalho
André Carvalho

Reputation: 1117

Using the Docker CLI

I work with Docker Swarm clusters and to remove a node from the cluster there are two options.

It depends on where you want to run the command, within the node you want to remove or on a manager node other than the node to be removed.

The important thing is that the desired node must be drained before being removed to maintain cluster integrity.

First option:

So I think the best thing to do is (as steps in official document):

  1. Go to one of the nodes with manager status using a terminal ssh;
  2. Optionally get your cluster nodes;
  3. Change the availability to drained of the node you want to remove;
  4. And remove it;
# step 1
ssh user@node1cluster3
# step 2, see the nodes in your cluster like print screen below
docker node ls
# step 3, drain one of them
docker node update --availability drain node4cluster3
# step 4, remove the drained node
docker node rm node4cluster3

output of docker node ls

Second option:

The second option needs two terminal logins, one on a manager node and one on the node you want to remove.

Perform the 3 initial steps described in the first option to drain the desired node.

Afterwards, log in to the node you want to remove and run the docker swarm leave command.

# remove from swarm using leave
docker swarm leave

# OR, if the desired node is a manager, you can use force (be careful*)
docker swarm leave --force

*For information about maintaining a quorum and disaster recovery, refer to the Swarm administration guide.

My environment information

  • I use Ubuntu 20.04 for nodes inside VMs;
  • With Docker version 20.10.9;
  • Swarm: active;

Upvotes: 0

DerekC
DerekC

Reputation: 842

Try this:

docker node list # to get a list of nodes in the swarm
docker node rm <node-id>

Upvotes: 1

BMitch
BMitch

Reputation: 263696

The reference to "static file mode" implies the container based standalone swarm that predated the current Swarm Mode that most know as Swarm. These are two completely different "Swarm" products from Docker and are managed with completely different methods.

The other answers here focused on Swarm Mode. With Swarm Mode docker swarm leave on the target node will cause the node to leave the swarm. And when the engine is no longer talking to the manager, docker node rm on an active manager for the specific node will cleanup any lingering references inside the cluster.

With the container based classic swarm, you would recreate the manager container with an updated static list. If you find yourself doing this a lot, the external DB for discovery would make more sense (e.g. consul, etcd, or zookeeper). Given the classic swarm is deprecated and no longer being maintained, I'd suggest using either Swarm Mode or Kubernetes for any new projects.

Upvotes: 0

arcseldon
arcseldon

Reputation: 37105

Using Docker Version: 1.12.0, docker help offers:

➜  docker help swarm

Usage:  docker swarm COMMAND

Manage Docker Swarm

Options:
      --help   Print usage

Commands:
  init        Initialize a swarm
  join        Join a swarm as a node and/or manager
  join-token  Manage join tokens
  update      Update the swarm
  leave       Leave a swarm

Run 'docker swarm COMMAND --help' for more information on a command.

So, next try:

➜  docker swarm leave --help

Usage:  docker swarm leave [OPTIONS]

Leave a swarm

Options:
      --force   Force leave ignoring warnings.
      --help    Print usage

Upvotes: 3

Toni Piza
Toni Piza

Reputation: 515

Using the swarm mode introduced in the docker engine version 1.12, you can directly do docker swarm leave.

Upvotes: 1

Related Questions