{"id":741,"date":"2018-09-06T09:49:38","date_gmt":"2018-09-06T09:49:38","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=741"},"modified":"2018-09-06T09:50:08","modified_gmt":"2018-09-06T09:50:08","slug":"docker-swarm-introduction-and-basics","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=741","title":{"rendered":"Docker Swarm: Introduction and basics"},"content":{"rendered":"<p>A docker instance is good to run task in a small scale. However as the traffic increases, or when more processing power is required, when we want to scale-up the infrastructure, the way to move is Docker Swarm, which is a group of Docker nodes joined into a cluster.<\/p>\n<p>The Docker Swam cluster is managed by one node, that will be designated as the <em>Swarm Manager<\/em>.<\/p>\n<p>In this tutorial we will create a Docker Swarm with 3 nodes.<\/p>\n<p><b>Prerequisites<\/b><strong>\u00a0for the lab setup:<\/strong><\/p>\n<ul>\n<li>Deploy three Docker nodes with IPs <em>192.168.1.101<\/em>, <em>192.168.1.102<\/em> and <em>192.168.1.103<\/em> respectively. You may follow the example posted earlier by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=722\" target=\"_blank\">here<\/a>.<\/li>\n<li>Set hostnames of the nodes as <em>docker1<\/em>, <em>docker2<\/em> and <em>docker3<\/em> respectively.<\/li>\n<li>Ensure Selinux is disabled in all nodes<\/li>\n<li>Ensure firewall such as firewalld is disabled in all three nodes.<\/li>\n<\/ul>\n<p>Let us make the node &#8220;<em>docker1<\/em>&#8221; as the <em>master\/manager<\/em> node. Run the following command and copy the line in the output that you will need to run on the other two nodes to make then &#8220;<em>worker<\/em>&#8221; nodes.<br \/>\n<em><strong>[root@docker1 ~]# docker swarm init &#8211;advertise-addr 192.168.1.101<\/strong><\/em><\/p>\n<p>Run the following command in the Manager node, and take a note of the lines that shows the number of &#8220;<em>Managers<\/em>&#8221; and &#8220;<em>Nodes<\/em>&#8221;<br \/>\n<em><strong>[root@docker1 ~]# docker info<\/strong><\/em><\/p>\n<p>Now let us list the nodes in the cluster using the following command. As we add mode nodes, the entries in the list too will increase:<br \/>\n<em><strong>[root@docker1 ~]# docker node ls<\/strong><\/em><\/p>\n<p>Let us join docker2 and docker3 to the cluster by running the following command in both nodes. This is a command that was displayed when we created the Manager node. This\u00a0will defer from cluster to cluster.<\/p>\n<p><em><strong>[root@<\/strong>docker3<strong> ~]# docker swarm join &#8211;token SWMTKN-1-4r2573r9xchcpeldaare4wv14yp3ropmwse2celwfpxyoenmmg-c9851tevz3xbhn3ctg7m2di8v 192.168.1.101:2377<\/strong><\/em><\/p>\n<p><em><strong>[root@docker2 ~]# docker swarm join &#8211;token SWMTKN-1-4r2573r9xchcpeldaare4wv14yp3ropmwse2celwfpxyoenmmg-c9851tevz3xbhn3ctg7m2di8v 192.168.1.101:2377<\/strong><\/em><\/p>\n<p>Now let us list the nodes in the cluster using the following command in the Manager node:<br \/>\n<em><strong>[root@docker1 ~]# docker node ls<\/strong><\/em><\/p>\n<p>Before proceeding, ensure you have as http server Docker image &#8220;c<em>entos_http<\/em>&#8221; in your Manager node following the tutorial &#8220;<em>Docker container: CentOS with Apache<\/em>&#8220;, which can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=732\" target=\"_blank\">here<\/a>. Ensure the web page can be viewed by accessing the URL &#8220;<em>http:\/\/192.168.1.101:1337<\/em>&#8220;. Once this test is completed, ensure the container is stopped and no images are running, nor anything listening to the port 1337 that was used earlier.<\/p>\n<p>Let us start the above http application as a <em>service<\/em> in the cluster by running the following command in the Master node:<br \/>\n<em><strong>[root@docker1 docker]# docker service create &#8211;replicas 1 &#8211;name myWebpage1 -p 1337:80 centos_http<\/strong><\/em><\/p>\n<p>Now try accessing the following URLs:<\/p>\n<ul>\n<li><em>URL <strong>http:\/\/192.168.1.101:1337<\/strong><\/em><\/li>\n<li><em>URL <strong>http:\/\/192.168.2.101:1337<\/strong><\/em><\/li>\n<li><em>URL <strong>http:\/\/192.168.3.101:1337<\/strong><\/em><\/li>\n<\/ul>\n<p>You can list the serives running on the cluster by running the following command from the Master node. Notice the column &#8220;<em>REPLICAS<\/em>&#8221; and &#8220;<em>ID<\/em>&#8220;.<br \/>\n<em><strong>[root@docker1 docker]# docker service ls<\/strong><\/em><\/p>\n<p>Now let us get more details of the service referenced by the <em>ID <\/em>listed above:<br \/>\n<em><strong>[root@docker1 docker]# docker service inspect &#8211;pretty &lt;service ID&gt;<\/strong><\/em><\/p>\n<p>Identify the node where the service is running. Notice the column &#8220;NODE&#8221;.<br \/>\n<em><strong>[root@docker1 docker]# docker service ps\u00a0&lt;Node name&gt;<\/strong><\/em><\/p>\n<p>In the above example you are running the service only in one of the node in the cluster. You may come to a need of <em>scaling<\/em> it to two or more. The command to scale the setup to use two nodes is as follows:<\/p>\n<p><em>[root@docker1 docker]# docker service ls<\/em><br \/>\n<strong><em> [root@docker1 docker]# docker service scale &lt;Node ID&gt;=2<\/em><\/strong><\/p>\n<p>Verify if the service is running in two nodes now by using the command:<br \/>\n<em><strong>[root@docker1 docker]# docker service ps myWebpage1<\/strong><\/em><\/p>\n<p>Once you are done with this lab, you may delete the service by running the command:<br \/>\n<em><strong>[root@docker1 docker]# docker service rm myWebpage1<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A docker instance is good to run task in a small scale. However as the traffic increases, or when more processing power is required, when <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=741\" title=\"Docker Swarm: Introduction and basics\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":730,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,21,25],"tags":[],"class_list":["post-741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cluster","category-devops","category-docker"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=741"}],"version-history":[{"count":3,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":744,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions\/744"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/730"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}