Docker Swarm is a clustering tool that turns a group of Docker hosts into a single virtual server.
Docker Swarm ensures availability and high performance for your application by distributing it over the number of Docker hosts inside a cluster.
Docker Swarm also allows you to increase the number of container instance for the same application.
Docker Swarm is made up of two main components:
1-Manager Nodes / Master Nodes
Manager Nodes Manager nodes are used to handle cluster management tasks such as maintaining cluster state, scheduling services, and serving swarm mode HTTP API endpoints.
If any Manager node dies unexpectedly, other one can pick up the tasks and restore the services to a stable state.
Worker Nodes Worker nodes are used to execute containers.
You can create a swarm of one Manager node, but you cannot have a Worker node without at least one Manager node.
You can also promote a worker node to be a Manager when you take a Manager node offline for maintenance.
To do this lab we need a master node and some workers node.
Lets launch 3 instance and name one of it as master and other as workers as shown below
Now install docker package in all the above instances.
yum install docker* -y systemctl start docker systemctl enable docker
Here I have used centos…
For Ubnuntu – apt install ..
Now install docker swarm in master node only
docker swarm init
Now this instance becomes your master node.
After running that command ,it will give a token.. run that command in all your workers node
as you can see both the instances becomes worker nodes
How to host a website using docker file
Ist create a docker file using vi editor inside master node
Our docker file is ready now lets build this using command
docker image build -t appimage:1 .
It will build your docker file
Follow the same process to deploy a website in workers node too..
or you can use docker registry or using docker hub push that image into docker hub and pull it in worker node and build it..
Launch web service in Docker Swarm
Run a service
docker service ls
docker service create --name app-server -p 80:80 appimage:1
You can also scale up as shown below
Test Docker Swarm
Apache web server is now running on Manager Node. You can now access web server by pointing your web browser to the Manager Node IP
or Worker Node IP as shown below:
Here I have attached a domain to my IP and used LetsEncryt to secure my website.