Overview
In other technologies we have clusters, in docker, we have Swarm. Same concept, different name :)
A swarm in docker is set of servers who act as one and each server can be either: Manager or Worked. It is important to note, that manager is also a worked, but the reverse isn't so. Managers manage workers and workers obey, nothing different than clusters.
Below, you can see basic overview of the swarm:
Setup
Let's set it up. We will have 3 servers:
- 1 Manager
- 2 Workers
Automatic
You can see the automatic creation in the Vagrant section: Multiple Servers Configuration with Vagrant
After the setup is done, please be sure that workers, have joined, we can create the service as follows:
Manual
The creation of the VMs and installation of docker-ce will not be discussed as it has been done in at least 10 other sections. So let's check how to setup manager and 2 workers:
Initiation
The initiation of the Swarm is done from one of the managers as follow:
Init the Swarm
docker swarm init --listen-addr 10.100.199.200:2377 --advertise-addr 10.100.199.200:2377
That command will initialize a swarm with 1 manager who will listen on IP: 10.100.199.200:2377 for docker swarm related activities (advertise-addr) and income traffic (listen-addr)
Addition
To join a swarm a server must have the swarm's token. The token can be taken as follows:
Get Join Token for Manager & Worker
[root@mgr1 ~]# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-4mqe4htimsl9wrlzstzx6c18qmibjfjuuihdp3o91pnmssbvml-69b13glzhdgfzh58hbg7jwn69 10.100.199.200:2377 [root@mgr1 ~]# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-4mqe4htimsl9wrlzstzx6c18qmibjfjuuihdp3o91pnmssbvml-1h1yrqixgrme7aypq6c4m0npj 10.100.199.200:2377 [root@mgr1 ~]#
Now, it is good idea to ALWAYS add the advertise and listener address to the server:
Manually, workers and manager can be joined to the swarm as follows:
Add a node to the Swarm
docker swarm join --token SWMTKN-1-4mqe4htimsl9wrlzstzx6c18qmibjfjuuihdp3o91pnmssbvml-1h1yrqixgrme7aypq6c4m0npj \ manager_advertise_ip:2377 \ --advertise-addr worker_advert_ip_addr:2377 \ --listener-addr worker_listen_ip_addr:2377
Insert the correct token depending on, if you are adding worker or a manager. The above is for worker.
Create service
The creation of the service is rather easy:
[root@mgr1 ~]# docker service create --name httpDemoService --replicas 3 -p 80:1234 andonovj/httpserverdemo nz82mserbonjcoccp7iir7hfn overall progress: 3 out of 3 tasks 1/3: running [==================================================>] 2/3: running [==================================================>] 3/3: running [==================================================>] verify: Service converged [root@mgr1 ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS q8s7iiv4rwu7 httpDemoService replicated 3/3 andonovj/httpserverdemo:latest [root@mgr1 ~]# [root@mgr1 ~]# [root@mgr1 ~]# [root@mgr1 ~]# [root@mgr1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION occzvuwmny5rm8x8an4bctdc1 * mgr1 Ready Active Leader 19.03.7 7oubfm1qdun533xw9s14lt3pu worker01 Ready Active 19.03.7 sdcosdbt1f94rrvbr8m9ng30s worker02 Ready Active 19.03.7 [root@mgr1 ~]# [root@mgr1 ~]# docker service ps httpDemoService ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS avcanadogs3e httpDemoService.1 andonovj/httpserverdemo:latest mgr1 Running Running 27 seconds ago 6pgfba2xdg0q httpDemoService.2 andonovj/httpserverdemo:latest worker01 Running Running 9 seconds ago me2174rye7nu httpDemoService.3 andonovj/httpserverdemo:latest worker02 Running Running 8 seconds ago [root@mgr1 ~]#