Let's use Docker Swarm for the reason it is
designed, which is to have multiple replicas of a
container or to use services with multiple containers themselves.
We'll create a service
called web-server from latest nginx image and have
3 replicas for it. We have also mentioned port
mapping information with -p flag.
Once we hit enter, you can see that our service has been
divided into three tasks, and each task has been
carried out individually. Once the tasks are
complete, the service has been verified, and once
the service creation is complete, we can list it
using docker service ls command. First of all, we
have service ID, then we have the NAME of service,
which is same as we had provided with the command web-server.
Then we have mode of service, it is
replicated, which means the same image has been
replicated more than once and multiple instances
or multiple containers are created from the same image.
We have three replicas in particular.
The image, which has been used is the latest nginx,
and we also have port mapping information for TCP.
If we want to have a look at the containers
running inside the service, the command is pretty
simple. Just write docker service ps followed by
the name of the service. Here we have three
containers, and the naming convention is pretty simple.
The names are web-server.1, web-server.2, and web-server.3.
They are up and running for about the same time and all of
them share the common nginx:latest image.
Just like we had done with docker-compose, let's inspect our service.
And as we go further, along
with the generic information, we also get some
additional information like the mode of the
service, which is replicated, and details regarding
all of the hosts, or all of the machines, where
each and every container of the service is
provisioned. Unlike docker service ps, if we
regularly run docker ps -a command on any
of the node, we will get to know that each of the
node is only running one container.
That is because, the service has been deployed across the
cluster, which means that the load was divided evenly.
Since we had 3 replicas, all of these
containers were sheduled on an individual node,
web-server.1 was scheduled on manager node,
web-server.2 was scheduled on worker-1 node
and web-server.3 was scheduled on worker
worker to node just like a regular container,
we can inspect this web-server.1 as well.
Now, this means that all of these 3 nodes are
running at least one instance of nginx web server.
So all of them should be serving nginx default web page
on their respective IP addresses on their port 8080.
Let's go to browser and check this fact with our manager node.
See that we are navigating
to the IP of the manager which is 192.168.99.100,
and we are mentioning the port 8080.
It seems like a success. Now, let's do the same with
worker-1 and worker-2. This means that the
service is running successfully. And Docker Swarm
is hosting nginx web server on all of these 3 nodes.