Search code examples
docker-swarmswarm

How to reschedule containers with swarm when the server dies for a moment


I run two servers using docker-compose and swarm

When stopping the A server, the container in A server is moved to B server.

but, when starting up the A server, the container that was in the B server will not moved to A server.

I want to know how to properly arrange the location of the dead server's container when the server dies for a moment


Solution

  • First, for your Swarm to be able to re-create a task when a node goes down, you still need to have a majority of manager nodes still available... so if it was only a two node Swarm, this wouldn't work because you'd need three managers for one to fail and another to take leader role and re-schedule the failed replicas. (just a FYI)

    I think what you're asking for is "re-balancing". When a node comes back online (or a new one is added), Swarm does nothing with services that are set to the default replicated mode. Swarm doesn't "move" containers, it destroys and re-creates containers, so it considers the service still healthy on Node B and won't move it back to Node A. It wouldn't want to disrupt your active/healthy services on Node B just because Node A came back online.

    If Node B does fail, then Swarm would again re-schedule the task on the next best node.

    If Node B has a lot of containers, and work is unbalanced (i.e. Node A is empty and Node B has 3 tasks running), then you can force a service update which will destroy and re-create all replicas of that service and will try to spread them out by default, which may result on one of the tasks ending up back on Node A.

    docker service update --force <serivcename>