Docker Swarm in AWS (set up)
Published: 2022-05-29 11:22:17

**What is a Swarm?**
Docker Swarm is a cluster management and orchestration tool that makes it easy to scale and manage your already existing docker services. A swarm consists of multiple Docker hosts that run in the so-called swarm mode and act as managers (managing member relationships) or as workers (run the services). A given Docker host can be a manager, worker or can perform both roles.
When creating a service in a swarm you define the optimal state of your service (number of replicas, ports of the service, network and storage resources, and more). Docker will try to maintain this desired state by restarting/rescheduling unavailable tasks and balancing the load between different nodes.
**Managers Nodes:**
Manager nodes distribute and schedule incoming tasks onto the Worker nodes, maintain the cluster state and perform orchestration and cluster management functions. Manager Nodes can also optionally run services for Worker nodes.
**Worker Nodes:**
Worker nodes are also instances of the Docker Engine whose sole purpose is to execute containers and services as instructed by the Manager Nodes.


Steps To Host A Website In Docker Container And Managing It Using Docker Swarm. 

         
Launch 4 Ec2 instances with tags as Master, Worker-1 , Worker-2,Worker-3.
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dsz29thjzonlmb91vyq.png)
 

Connect To Servers using the Xshell  and initialize Docker. 
Command yum install docker* -y
Command to start docker is- systemctl start docker
Command to enable docker is :- systemctl enable docker


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxehvn97mo4suesb5yn7.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7t1gomcnpzxnuymrrt8.png)


Initialize Docker Swarm into your instances.
Command to initialize docker Swarm :- docker swarm init


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kuq7fn9eop2pzkehtwd.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zknbqbgs1qrm11xhdml.png)
   

Now, a token will be shown . Copy this token and paste it in the other nodes. Now the other nodes will become the Worker nodes.

You can check the joining status of the nodes by using the command  docker node ls  in the Master Node.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg0yg9esp61d9xwltpts.png)

 
write the docker file to carry out the following functions. And Docker file should written into the VI Editor.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/022m471glnzn57mgq35r.png)


Copy and run the same Docker file into the worker node's as it will be helpful in building the image of the same Docker Container into the worker node's also.
Save the docker file into the VI Editor and run the following to command to build  the image of the docker container into the master node.

Build the image by command -
docker image build -t appimage:1 .
You can check the image by using the command - 
docker image ls



 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eln2djyumqhkz9tk0hjl.png)
 


Now, you have to run the services from the Master Node  command docker service create --name #name_of_the_service -p 80:80 #any_name_of_image:1

Check the service id using command docker service ls .
Note this service ID.
Create the containers using command: docker service scale #service_id = 4(you can give any number of container).
The containers are randomly deployed on any one the Master or Worker nodes.
Docker Swarm also provides the Load Balancing features thus providing maximum availability.

Finally, your docker swarm is configured and ready.
When you hit the IP of any worker or the master node, you will be able to view your site mapped.

(This is the sample site I have downloaded from internet and httpd into my EC2 master instance).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39s0ifd0d5lcc8dgj156.png)