in Uncategorized

Docker Swarm on local MacOS

Recently docker has released Docker for Mac and it works really well since it is a native application and does not use docker machine concept, but if you want to play with docker swarm you can not.

In this tutorial I will show you how to start with docker swarm with all required commands.

Docker Machine

First of all you need to utilize docker-machine and create 3 machines ( you can do more if you want but 3 will do it). These machines are going to be our cluster to play with. Execute next commands:

To make sure that all is executed correctly execute:

output should be something like this:

Awesome now we have created 3 docker machines and are ready to make the swarm.

Docker Swarm

Docker swarm is simple to create but first we must ssh into the manager1 node using

To initialize the cluster execute (observe the IP address that is bolded above):

output is going to be similar to this one (copied from official documentation). Command made this node the manager.

In the message above we have explanation how to add a worker node  and how to add manager node into the swarm. To get us started with our playground we need only two worker nodes, but if you want go and see what happens.

Once we have our first manager we will ssh to both worker nodes using ssh command and paste the swarm join command.

After you add both worker nodes to the swarm it is time to verify that all is correctly done using

Here is the glitch , it prints an error. Reason for this error is that docker client does not point to correct docker machine.

To fix this issue you need to execute ( this just sets the environment variables like dockerHost, machineName, certPath and TLSVerify):

now if you repeat the command docker node ls you should see something like this (observe that manager1 is elected as a Leader):

Here is a few commands that you will use:

  • docker node ls

  • docker node ps

  • docker node inspect manager1

  • docker node update –availability active worker1

  • docker node update –availability drain worker1

Docker Swarm In Action

Using above commands we have created a swarm with the manager and have pointed our docker client to our swarm. Now we need to start creating the services.

To get started I will deploy nginx and scale it to 3 nodes with some extra parameters for fun.

  • replicas, specifies how many replicas do you want
  • name, is the name of the service
  • reserve-memory, specifies the RAM available for the service
  • publish, is the port that is swarm listening to and where our app can be accessed
  • update-delay is related to the deploying the new version

Lets make sure all is ok using

output would be similar to

To access the service you should open the browser to any of the next addresses, each address is the IP address of the node. Regardless if this node contains the job or not all will work fine.

  1. http://192.168.99.101:8080
  2. http://192.168.99.102:8080
  3. http://192.168.99.103:8080

Change the scaling

Deploy new version

docker service update --image nginx:version webapp

Apache2 load balancing

Lets assume that you want to add apache2 in front of the docker swarm that acts as a load balancer, here is how to do this

 

Write a Comment

Comment