DEV Community

Cover image for Docker Swarm in AWS (set up)
Pranjal-Agrawal
Pranjal-Agrawal

Posted on

Docker Swarm in AWS (set up)

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

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

Image description

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

Image description

Image description

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

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

Image description

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

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

Top comments (0)