not individual containers • Each service creates one or more replica tasks, which are run as containers • On manager, create a new service for a search microservice application: mgr-1$ docker service create -p 8080:8080 --name search \ --replicas 4 searchsvc:v1.0 mgr-1$ docker service ls ID NAME REPLICAS IMAGE COMMAND 2xtw9qipmbe9 search 4/4 searchsvc:v1.0
tasks • Manager nodes manage the swarm mgr-1$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS drwxwi4h2fb0tcrwgmpmma2x0 * mgr-1 Ready Active Leader 1mhtdwhvsgr3c26xxbnzdc3yp mgr-2 Ready Active Reachable 516pacagkqp2xc3fk9t1dhjor mgr-3 Ready Active Reachable 9j68exjopxe7wfl6yuxml7a7j worker-1 Ready Active 03g1y59jwfg7cf99w4lt0f662 worker-2 Ready Active dxn1zf6l61qsb1josjja83ngz worker-3 Ready Active
Worker Worker Worker Search service container Billing service container Search service container Search service container Billing service container Search service container
Search service container Billing service container Search service container Search service container Billing service container Search service container Manager Manager Manager
service container Billing service container Search service container Search service container Search service container Billing service container Manager Manager Manager
manager is fine in some scenarios • Any swarm meant to survive a manager failure should have 3 or 5 managers • No scaling benefit to adding additional managers • Each one replicates a full copy of the swarm's state
static IP addresses • Managers should have very reliable connectivity to each other • Swarms that span a big geographic area aren't recommended • Looking at federation as an eventual solution for multi- region • Spreading managers across a cloud provider's "availability zones" in one region may make sense
two out of three managers fail • The swarm won't be able to schedule tasks or perform administrative functions • You will see timeouts from commands like docker node ls if this happens
if these managers are gone forever? • docker swarm init --force-new-cluster on the surviving manager recovers from this state • This modifies the swarm so that it only has a single manager • From that point, new managers can be added
will be assigned tasks just like workers • This makes sense on small deployments • Best practice for serious deployments: avoid running container workloads on managers
to prevent them from running service tasks: mgr-1$ docker node update --availability=drain <manager id> • Alternatively, set the node.role == worker constraint on all services
• docker service update is a rolling update by default • Parameters: • Update delay (--update-delay) • Update failure action: pause or continue (--update-failure-action) • Parallelism (--update-parallelism)
authenticated with mutual TLS • Each node is identified by its certificate (CN = node ID) • The certificate authorizes the node to act as either a worker or manager (OU = swarm-manager or OU = swarm-worker) • By default, each manager operates as a certificate authority with the same CA key
a secret that authorizes the new node to receive either a worker or manager certificate • It also contains a digest of the root CA certificate, for protection against man-in-the-middle attacks • The node does not use or store the join token after joining
a manager, retrieve the join token: mgr-1$ docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5f7umqonkff6je2l1kqpxdsok3bwipn73hlr5dxtvx4lusy809 -5yn6jy5zqqq3tnummvq365y7m \ 172.17.0.2:2377
the command on the new worker: worker-1$ docker swarm join --token \ SWMTKN-1-5f7umqonkff6je2l1kqpxdsok3bwipn73hlr5dxtvx4lusy809 -5yn6jy5zqqq3tnummvq365y7m \ 172.17.0.2:2377 This node joined a swarm as a worker.
until they are rotated • It is good practice to periodically rotate them • docker swarm join-token --rotate worker generates a new worker token to replace the old one • docker swarm join-token --rotate manager generates a new manager token to replace the old one
Succesfully rotated worker join token. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-5f7umqonkff6je2l1kqpxdsok3bwipn73hlr5dxtvx4l usy809-6cq1skbwkkrp2xgv4ak0cgn01 \ 172.17.0.2:2377
• A manager will issue a renewed certificate to any node that can prove its identity with mutual TLS • The certificate validity period can be changed with mgr-1$ docker swarm update --cert-expiry=1000h • A shorter expiration time limits the time window where a leaked certificate is useful to an attacker
a hardened external CA • Swarm mode can be set up to call out to an external CA $ docker swarm init --external-ca \ protocol=cfssl,url=https://myca.domain.com
token, cert request Signed certificate Node registration Task assignments = TLS with no client certificate = Mutually authenticated TLS External CA Cert request Signed cert
JSON API • Swarm manager authenticates with the external CA using mutual TLS with its manager certificate • External CA becomes a single point of failure for granting and renewing certificates
nodes executing containers from the service if --with-registry-auth is specified: mgr-1$ docker service create \ --with-registry-auth myprivateimage • Note that the password or token is exposed to workers where the tasks are scheduled
crypto keys with services • The associated secrets are sent to the nodes where the service's tasks are assigned • They are made available inside a RAM filesystem mounted inside the container • Secrets are not written to disk as part of task execution
to spread out replicas over as many nodes as possible • This avoids concentrating the service's tasks on one node or a few nodes • Then it's harder for hardware/network failures to take out all replicas
to 3 replicas, old scheduling algorithm: Node 2 Billing service Billing service Billing service Node 1 Search service Search service Search service Node 1 has fewest tasks, so it receives the new task Search service
to 3 replicas, new scheduling algorithm: Node 2 Billing service Billing service Billing service Node 1 Search service Search service Search service Node 2 has fewest replicas of the search service, so it receives the new task Search service
accidentally roll out a bad update to a service, it will be possible to roll it back with a simple command: mgr-1$ docker service update --rollback <servicename>