Launching a service

Rather than launching containers using the docker container run command you need to create a service A service defines a task which the manager then passes to one of the workers and then a container is launched.

Let's launch a simple service called cluster which uses the image we looked at in Chapter 2, Launching Applications Using Docker:

docker service create --name cluster -p:80:80/tcp russmckendrick/cluster

That's it, we should now have a single container running on one of our three nodes. To check that the service is running and get a little more information about the service, run the following commands:

docker service ls
docker service inspect cluster --pretty

Now that we have confirmed that our service is running, you will be able to open your browser and enter the IP address of one of your three nodes (which you can get by running docker-machine ls).One of the features of Docker Swarm is it's routing mesh.

Launching a service

A routing mesh? When we exposed the port using the -p:80:80/tcp flag, we did a little more than map port 80 on the host to port 80 on the container, we actually created a Swarm load balancer on port 80 across all of the hosts within the cluster. The Swarm load balancer then directs requests to containers within our cluster.

Running the commands below, should show you which tasks are running on which nodes, remember tasks are containers which have been launched by the service:

docker node ps swarm01
docker node ps swarm02
docker node ps swarm03

Like me, you probably have your single task running on swarm01:

Launching a service

We can make things more interesting by scaling our service to add more tasks, to do this simply run the following commands to scale and check our service:

docker service scale cluster=6
docker service ls
docker service inspect cluster --pretty

As you should see, we now have 6 tasks running within our cluster service:

Launching a service

Checking the nodes should show that the tasks are evenly distributed between our three nodes:

docker node ps swarm01
docker node ps swarm02
docker node ps swarm03
Launching a service

Hitting refresh in your browser should also update the hostname under the Docker image change, another way of seeing this on Mac and Linux is to run the following command:

curl -s http://$(docker-machine ip swarm01)/ | grep class=

As you can see from the following terminal output, our requests are being load balanced between the running tasks:

Launching a service

Before we terminate our Docker Swarm cluster let's look at another way we can launch services, before we do we need to remove the currently running service, to do this simply run:

docker service rm cluster

Now that the service has been removed, we can launch a stack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.102.248