2

I have setup with 2 servers, production one called prod and staging one called staging. Both servers are in same Docker swarm. I want to write bash script to move one docker swarm service from production to staging server. I have created this 2 bash scripts i'm running at node manager.

#!/usr/bin/env bash

# move_service_to_prod.sh

/usr/bin/docker service update \
  --constraint-rm "node.hostname==staging" \
  --constraint-rm "node.hostname==prod" \
  --constraint-add "node.hostname==prod" \
some_service

#!/usr/bin/env bash

# move_service_to_staging.sh

/usr/bin/docker service update \
  --constraint-rm "node.hostname==staging" \
  --constraint-rm "node.hostname==prod" \
  --constraint-add "node.hostname==staging" \
some_service

As you can see, script removes all constraints, and than add new one. When i start 1st script, it sometimes creates 2 prod constraints for service. Like this output of `docker inspect some_service'

  "Placement": {
    "Constraints": [
      "node.hostname==staging",
      "node.hostname==staging"
    ],

Sometimes it not works, - service still running on staging machine. If there will be few more environments (like staging1, staging2, etc), code will be much bigger.

Is there any reliable bash oneliner to remove all constraints and to add proper ones for service?

Alex O
  • 7,746
  • 2
  • 25
  • 38
vodolaz095
  • 6,680
  • 4
  • 27
  • 42
  • 1
    Hello, I suspect you are referring to the service by name, which may match one or more service id .. I'd suggest to refer to the service by id instead.. (see docker service ls) – kalou.net Aug 08 '19 at 09:29
  • 1
    I remember an issue where I added new nodes to the swarm and the docker daemon would not spread the service containers evenly throughout the swarm network to efficiently utilize the newly added nodes. Could you type in `docker service update --force ` after applying the constraint rules to check if it works properly? I'd create an answer if it does. – 7_R3X Aug 12 '19 at 09:21
  • thanks for suggestions, 7_R3X, but unfortunately it not worked for me, same mess with duplicate constraints – vodolaz095 Aug 13 '19 at 14:02

0 Answers0