Setting up Docker Swarm

tested with Docker CE on Ubuntu and Slackware

Requirements

assuming you got docker up and running

Note – you need the Swarm nodes to be able to pull the images from a common repository

Initialization

on the node to become manager and leader

inititialize the cluster

docker swarm init --help
docker swarm init --advertise-addr CLUSTER-IP
docker swarm join-token manager
docker swarm join-token worker

add more managers

docker swarm join --token MANAGER-TOKEN x.x.x.x:2377

add more workers

docker swarm join --token WORKER-TOKEN x.x.x.x

Ready to go

on some manager node

run an instance cluster-wide

docker service create --help
docker service create --name train-shuttle --hostname train-shuttle --workdir /root pbraun9/ubuntu
# --replicas 2
# --mode=global

status

docker node ls
docker node ps
# --no-trunc

docker service ls
docker service ps train-shuttle
docker service logs train-shuttle

stop/REMOVE

docker service rm train-shuttle

Usage

there’s no absolute need for docker-compose. we can either define the number of replicas ourselves

docker service create --replicas 2 ...

or enable global mode

docker service create --mode=global ...

promote a worker to a leader/manager

docker node promote HOSTNAME

downgrade a leader/manager to a worker node

docker node demote HOSTNAME

Additional notes

the docker daemon doesn’t listen on additional ports but Swarm does make your farm somehow insecure in case it has external interfaces, therefore MAYBE ENABLE SYSTEM FIREWALL FOR ONCE (load before dockerd)

vi /etc/rc.d/rc.inet1

    echo -n nftables...
    /usr/sbin/nft -f /etc/nftables.conf && echo done || echo FAIL

re-balance the instances after a host has recovered (--replicas mode)

docker service ls
docker service update SERVICE --detach=false --force

for a worker to leave the cluster

docker swarm leave

for a manager to leave the cluster

docker swarm leave --force

DIY web cluster

prepare a test image PER NODE, and test it with Docker first

docker pull larsks/thttpd
docker images/
mkdir /var/tmp/content/

node 1

echo '<p>kube1' > /var/tmp/content/index.html
docker run -d --name thttpd-app -p 80:80 -v /var/tmp/content:/content larsks/thttpd /content

node 2

echo '<p>kube2' > /var/tmp/content/index.html
docker run -d --name thttpd-app -p 80:80 -v /var/tmp/content:/content larsks/thttpd /content

node 3

echo '<p>kube3' > /var/tmp/content/index.html
docker run -d --name thttpd-app -p 80:80 -v /var/tmp/content:/content larsks/thttpd /content

and check

http://NODE1/
http://NODE1/
http://NODE1/

Resources

https://docs.docker.com/engine/swarm/swarm-tutorial/#open-protocols-and-ports-between-the-hosts

https://platform9.com/blog/compare-kubernetes-vs-docker-swarm/

http://callistaenterprise.se/blogg/teknik/2017/03/09/go-blog-series-part5/

docker swarm init https://docs.docker.com/engine/reference/commandline/swarm_init/

volume

https://docs.docker.com/engine/reference/builder/#notes-about-specifying-volumes

https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-host-directory-as-a-data-volume

https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

misc

https://docs.docker.com/registry/recipes/mirror/

notes

docker swarm leave https://docs.docker.com/engine/reference/commandline/swarm_leave/

What is opposite of docker swarm init? https://stackoverflow.com/questions/48345602/what-is-opposite-of-docker-swarm-init

global mode

https://docs.docker.com/engine/reference/commandline/service_create/

https://docs.docker.com/engine/swarm/services/

https://docs.docker.com/engine/swarm/key-concepts/

https://success.docker.com/article/mode-global-can-cause-platform-mismatch-for-replica

diy

https://hub.docker.com/r/larsks/thttpd


Docker Swarm connection errors while VMware Snapshots https://access.redhat.com/discussions/2950631

Docker Swarm: bulk sync to node failed, https://forums.docker.com/t/docker-swarm-bulk-sync-to-node-failed/91510

Swarm is having occasional network connection problems between nodes. #32195 https://github.com/moby/moby/issues/32195


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | NU | HTML5
Copyright © 2022 Pierre-Philipp Braun