{"id":39157,"date":"2016-10-13T15:56:09","date_gmt":"2016-10-13T10:26:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=39157"},"modified":"2016-11-02T11:35:20","modified_gmt":"2016-11-02T06:05:20","slug":"docker-swarm-multi-manager-setup","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/docker-swarm-multi-manager-setup\/","title":{"rendered":"Docker Swarm Multi-Manager Setup"},"content":{"rendered":"<p>Docker Swarm has gained a lot of popularity in the last few months. It is Docker&#8217;s orchestration tool. Docker has been coming up with a lot of updates and Docker Swarm is very stable as compared to when it was launched. It is as Docker says &#8220;Production Ready.&#8221; You can go through the\u00a0blog written by my colleague on\u00a0how to <a title=\"Docker Swarm on AWS\" href=\"http:\/\/www.tothenew.com\/blog\/docker-swarm-on-aws\/\">setup Docker Swarm on AWS <\/a>.<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-41404\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/10\/Screenshot-from-2016-10-03-100250.png\" alt=\"Screenshot from 2016-10-03 10:02:50\" width=\"532\" height=\"250\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/10\/Screenshot-from-2016-10-03-100250.png 532w, \/blog\/wp-ttn-blog\/uploads\/2016\/10\/Screenshot-from-2016-10-03-100250-300x140.png 300w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/p>\n<p>In this blog, I will be using <strong>Docker Machine<\/strong> to create my Docker Swarm nodes and masters using virtual box driver. Also, I will be using another virtual machine to set up Consul for Nodes and Masters discovery. Then I will also show you how setup HA for Docker Swarm Master (manager). So, let&#8217;s get started.<\/p>\n<p>Consul will be used for node discovery. For now, I just have multiple Consul containers in one virtual box Docker Machine. For production, we can put those on different machines for HA.<\/p>\n<h1>Prerequisites<\/h1>\n<p>1. Docker Machine should installed. Install it from <a href=\"https:\/\/docs.docker.com\/machine\/install-machine\/\">here<\/a>.<br \/>\n2. VirtualBox installed should be above version 4.0.<br \/>\n3. Docker Engine should be present on your local machine.<\/p>\n<h1>Steps To Setup Your Swarm<\/h1>\n<p>1. First, we will create a machine for Consul. \u00a0Use the following command to create Consul machine:<\/p>\n<p>[js]docker-machine ls<br \/>\n(to check the current state and details of machines)<\/p>\n<p>docker-machine create -d=virtualbox consul-machine<br \/>\n(-d means driver for docker-machine, you can even use &#8211;driver virtualbox [\/js]<\/p>\n<p>Other drivers like <a title=\"DevOps in aws\" href=\"http:\/\/www.tothenew.com\/devops-aws\">AWS<\/a> are also supported which will bring up new EC2 instances and make them a Docker Machine. You can read more on Docker documentation website.<\/p>\n<p>Just connect to the machine using:<\/p>\n<p>[js]docker-machine ssh\u00a0machine-name [\/js]<\/p>\n<p>Now, you just need to create a Docker Compose file and enter the following contents:<\/p>\n<p>[js]<br \/>\nmyconsul:<br \/>\n  image: progrium\/consul<br \/>\n  restart: always<br \/>\n  hostname: consul<br \/>\n  ports:<br \/>\n    &#8211; 8500:8500<br \/>\n  command: &quot;-server -bootstrap&quot;<br \/>\n[\/js]<\/p>\n<p>Install docker-compose from in the Consul machine by following the steps mentioned\u00a0<a href=\"https:\/\/docs.docker.com\/compose\/install\/\">here<\/a>.<br \/>\nNow, run the below command inside the Consul\u00a0machine to get Consul up and running:<\/p>\n<p>[js]docker-compose up -d<br \/>\n(if the filename of the compose file is docker-compose.yml else pass the filename explicitly using -f switch) [\/js]<\/p>\n<p>Just do a <strong>docker ps<\/strong> and <strong>netstat\u00a0<\/strong>&#8211;<strong>ntlp<\/strong> to check Consul is running on 8500 port of this machine.<\/p>\n<p>2. Now that Consul is setup, let&#8217;s set up the two managers (masters) for Docker Swarm, one being primary and the other being a standby. This can be done simply by using the following two commands:<\/p>\n<p>[js]<br \/>\ndocker-machine create -d virtualbox &#8211;swarm \\<br \/>\n  &#8211;swarm-master \\<br \/>\n  &#8211;swarm-opt replication \\<br \/>\n  &#8211;swarm-discovery consul:\/\/192.168.99.114:8500 \\<br \/>\n  &#8211;engine-opt cluster-store=consul:\/\/192.168.99.114:8500 \\<br \/>\n  manager-0<br \/>\n[\/js]<\/p>\n<p>The command above will bring your first manager up, and the command given below should bring the next one up:<\/p>\n<p>[js]<br \/>\ndocker-machine create -d virtualbox &#8211;swarm \\<br \/>\n  &#8211;swarm-master \\<br \/>\n  &#8211;swarm-opt replication \\<br \/>\n  &#8211;swarm-discovery consul:\/\/192.168.99.114:8500 \\<br \/>\n  &#8211;engine-opt cluster-store=consul:\/\/192.168.99.114:8500 \\<br \/>\n  manager-1<br \/>\n[\/js]<\/p>\n<p>To connect to any of these managers, use the below given command:<\/p>\n<p>[js]eval $(docker-machine env\u00a0manager-0)[\/js]<\/p>\n<p>We will connect to the Swarm after the nodes are up. So, let&#8217;s move on to that.<\/p>\n<p>3. Bringing up the nodes is just similar to using the above command without the <strong>&#8211;swarm-master<\/strong>. Use the command:<\/p>\n<p>[js]docker-machine create -d virtualbox &#8211;swarm   &#8211;swarm-discovery consul:\/\/192.168.99.114:8500   &#8211;engine-opt cluster-store=consul:\/\/192.168.99.114:8500   node-1 [\/js]<\/p>\n<p>4. Let&#8217;s connect to the Docker Swarm manager&#8217;s engine. For doing that just run the following command:<\/p>\n<p>[js]eval $(docker-machine env\u00a0&#8211;swarm manager-0)[\/js]<\/p>\n<p>Run <strong>docker ps<\/strong> to see what containers are running in the swarm, and it should show none.<\/p>\n<p>Now, let&#8217;s run a new container say Ubuntu in the Swarm. By default new containers in the Swarm run on the nodes, manager-0. If you wish that containers never come up on the manager then just use drain on the manager. Now, run a container using the command:<\/p>\n<p>[js]docker run -itd\u00a0&#8211;name ubuntu1 ubuntu[\/js]<\/p>\n<p>Do a <strong>docker ps<\/strong> and see. It will show, on which node the container is running:<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-39295 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-142029.png\" alt=\"Screenshot from 2016-08-22 14:20:29\" width=\"1300\" height=\"289\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-142029.png 1300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-142029-300x66.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-142029-1024x227.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-142029-624x138.png 624w\" sizes=\"(max-width: 1300px) 100vw, 1300px\" \/><\/p>\n<p>So, now you&#8217;re connected to Docker Swarm and every new container comes up in the Swarm.<\/p>\n<p>To work inside any of these containers, you can login to the nodes or else from the manager you can just run <strong>docker exec -it container-name bash<\/strong> (bash or any other command) to get into the desired container.<br \/>\nThere are some other features like you can launch a container on a node with a specific name, or where similar types of container are running using<strong> label<\/strong> and <strong>affinity<\/strong>.<\/p>\n<p>5. Let&#8217; see what happens if our primary manager goes down:<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-39298 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-144232.png\" alt=\"Screenshot from 2016-08-22 14:42:32\" width=\"680\" height=\"278\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-144232.png 680w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-144232-300x122.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/08\/Screenshot-from-2016-08-22-144232-624x255.png 624w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Running command to stop the manager-0:<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-41402 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144448.png\" alt=\"Screenshot from 2016-08-22 14:44:48\" width=\"437\" height=\"53\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144448.png 437w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144448-300x36.png 300w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/p>\n<p>Now, checking info of manager-1:<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-41403 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144524.png\" alt=\"Screenshot from 2016-08-22 14:45:24\" width=\"667\" height=\"217\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144524.png 667w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144524-300x97.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/09\/Screenshot-from-2016-08-22-144524-624x203.png 624w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><\/p>\n<p>Woohoo! The secondary manager is now the primary. So, this is how you can easily setup your own Docker Swarm. The blog also portrays how failover of managers takes place in Docker Swarm using <a title=\"Docker devOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">Docker<\/a> Machine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker Swarm has gained a lot of popularity in the last few months. It is Docker&#8217;s orchestration tool. Docker has been coming up with a lot of updates and Docker Swarm is very stable as compared to when it was launched. It is as Docker says &#8220;Production Ready.&#8221; You can go through the\u00a0blog written by [&hellip;]<\/p>\n","protected":false},"author":174,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":25},"categories":[1174,2348,1],"tags":[2366,3866,1892,1883,2651,4121,2590,4120,4119,4122],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39157"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=39157"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/39157\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=39157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=39157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=39157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}