{"id":985,"date":"2015-01-13T14:37:11","date_gmt":"2015-01-13T14:37:11","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=985"},"modified":"2015-01-13T14:37:11","modified_gmt":"2015-01-13T14:37:11","slug":"docker-tutorial-getting-started-with-docker-swarm","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=985","title":{"rendered":"Docker Tutorial : Getting Started With Docker Swarm"},"content":{"rendered":"<p><strong>Docker swarm<\/strong> is a clustering tool for docker. You can form a swarm cluster using one or more docker hosts. Like Kubernetes cluster manager, swarm schedules the containers on to the swarm cluster based on resource availability. You can also specify the resource for a container while deploying it. Swarm is designed to pack containers on to a host by saving other host resources for bigger containers rather than scheduling it randomly to a host in the cluster.<\/p>\n<p>This tutorial is based on old swarm version. Find the latest swarm cluster tutorial <a href=\"https:\/\/devopscube.com\/how-to-setup-and-configure-docker-swarm-cluster\/\" rel=\"noopener noreferrer\">here<\/a><\/p>\n<p>In this article, we will learn how to <strong>set up  docker swarm<\/strong> cluster.<\/p>\n<h2 id=\"docker-swarm-setup\">Docker Swarm Setup<\/h2>\n<p>In this setup, we will use two docker host (node1 and node2)  and one docker client machine (devopscube) with swarm configured on it.<\/p>\n<p><strong>You Might Like:<\/strong> <a href=\"https:\/\/pluralsight.pxf.io\/4qNE0?ref=devopscube.com\">First Look: Native Docker Clustering Video Course<\/a><\/p>\n<p><em><strong>Note:<\/strong> All the machines should have Docker installed on it. All the machines should have Docker installed on them.<\/em><a href=\"https:\/\/devopscube.com\/how-to-install-and-configure-docker\/\"><em>How to install and configure docker<\/em><\/a><\/p>\n<p>You need to configure the docker hosts to accept connections from a remote client on port 2375. You can configure this by following the steps given below. You need to perform theses stop on all the hosts.<\/p>\n<ol>\n<li>Stop the docker service.<\/li>\n<\/ol>\n<pre><code>sudo service docker.io stop<\/code><\/pre>\n<pre><code>ubuntu@node1:~$ sudo service docker.io stop\ndocker.io stop\/waiting\nubuntu@node1:~$<\/code><\/pre>\n<p>2.  Execute the following command to start and make the docker host listen to connections on 2375<\/p>\n<pre><code>sudo docker -H tcp:\/\/0.0.0.0:2375 -d<\/code><\/pre>\n<pre><code>ubuntu@node1:~$ sudo docker -H tcp:\/\/0.0.0.0:2375 -d\n2015\/01\/13 11:46:45 docker daemon: 1.0.1 990021a; execdriver: native; graphdriver:\n[0189987e] +job serveapi(tcp:\/\/0.0.0.0:2375)\n[0189987e] +job initserver()\n[0189987e.initserver()] Creating server\n2015\/01\/13 11:46:45 Listening for HTTP on tcp (0.0.0.0:2375)\n[0189987e] -job initserver() = OK (0)\n[0189987e] +job acceptconnections()\n[0189987e] -job acceptconnections() = OK (0)<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><i><b><strong class=\"italic\" style=\"white-space: pre-wrap;\">Note:<\/strong><\/b><\/i><i><em class=\"italic\" style=\"white-space: pre-wrap;\"> You need to perform the above step in all the nodes which is going to be the part of the swarm cluster. Also, if your server in the public network, do not use 0.0.0.0. instead, use the ip of the docker client machine in which you execute the swarm commands.<\/em><\/i><\/div>\n<\/div>\n<p>3.  Now, leave the connection open. You will need these terminals to see what&#8217;s happening in each host.<\/p>\n<p>Now we have two nodes and one client machine with Docker installed and the docker daemon listening to 2375. Now in the client machine, we need to setup swarm for creating the cluster. Perform the steps given below on the client machine, not on node1 and node2.<\/p>\n<ol>\n<li>Install go using the following command.<\/li>\n<\/ol>\n<pre><code> sudo apt-get install -y golang<\/code><\/pre>\n<ol start=\"2\">\n<li>create a directory named \u201cgo\u201d<\/li>\n<\/ol>\n<pre><code>mkdir go<\/code><\/pre>\n<ol start=\"3\">\n<li>export the go path using the following command.<\/li>\n<\/ol>\n<pre><code>export GOPATH=$HOME\/go<\/code><\/pre>\n<ol start=\"4\">\n<li>Get the swarm binary using the following command. It will download the swarm files to the gopath (go directory) we set in the 3rd step.<\/li>\n<\/ol>\n<pre><code>go get -u github.com\/docker\/swarm<\/code><\/pre>\n<ol start=\"5\">\n<li>Add swarm to the PATH variable using the following command.<\/li>\n<\/ol>\n<pre><code>export PATH=$HOME\/go\/bin:$PATH<\/code><\/pre>\n<ol start=\"6\">\n<li>Verify the swarm setup by executing the swarm version command.<\/li>\n<\/ol>\n<pre><code>swarm --version<\/code><\/pre>\n<pre><code>ubuntu@devopscube:~$ swarm --version\nswarm version 0.0.1\nubuntu@devopscube:~$<\/code><\/pre>\n<ol start=\"7\">\n<li>You can view all the available swarm options by just executing the swarm command.<\/li>\n<\/ol>\n<pre><code>swarm<\/code><\/pre>\n<pre><code>ubuntu@devopscube:~$ swarm\nNAME:\n   swarm - docker clustering\nUSAGE:\n   swarm [global options] command [command options] [arguments...]\nVERSION:\n   0.0.1\nCOMMANDS:\n   create, c    create a cluster\n   list, l      list nodes in a cluster\n   manage, m    manage a docker cluster\n   join, j      join a docker cluster\n   help, h      Shows a list of commands or help for one command\nGLOBAL OPTIONS:\n   --debug              debug mode [$DEBUG]\n   --help, -h           show help\n   --version, -v        print the version\nubuntu@devopscube:~$<\/code><\/pre>\n<p>Now we have the swarm setup ready. Next is to create a cluster by combining node1 and node2.<\/p>\n<h3 id=\"setting-up-swarm-cluster\">Setting Up Swarm Cluster<\/h3>\n<p>1. From the client machine (devopscube) execute the following command to create a swarm cluster. <\/p>\n<pre><code>swarm create<\/code><\/pre>\n<pre><code>ubuntu@devopscube:~$ swarm create\n73f8bc512e94195210fad6e9cd58986f\nubuntu@devopscube:~$<\/code><\/pre>\n<p>2.  As you can see, the swarm create command created a unique  cluster id<\/p>\n<p>(73f8bc512e94195210fad6e9cd58986f) . Note down the unique ID because we will be using the unique ID to join node1 and 2 to the swarm cluster.<\/p>\n<p>3. Nodes can be joined to the cluster using the following command syntax.<\/p>\n<pre><code>swarm join --discovery token:\/\/&lt;unique_cluster_id&gt; --addr=&lt;node_ip:2375&gt;<\/code><\/pre>\n<p>Execute the following command on with relevant parameters to join node1 to the cluster. You will not see any output for the command.<\/p>\n<pre><code>swarm join --discovery token:\/\/73f8bc512e94195210fad6e9cd58986f --addr=&lt;node1_ip:2375&gt;<\/code><\/pre>\n<pre><code>ubuntu@devopscube:~$ swarm join --discovery token:\/\/73f8bc512e94195210fad6e9cd58986f  --addr=54.149.184.25:2375<\/code><\/pre>\n<p>Close the terminal and execute the above command with node2 parameters to join it to the cluster.<\/p>\n<p>3. Now let\u2019s list the nodes in our swarm cluster using the following command.<\/p>\n<pre><code>Syntax: swarm list --discovery token:\/\/73f8bc512e94195210fad6e9cd58986f &lt;swarm_ip:swarm_port&gt;<\/code><\/pre>\n<pre><code>ubuntu@devopscube:~$ swarm list --discovery token:\/\/73f8bc512e94195210fad6e9cd58986f 54.69.173.225:2375\n54.167.134.89:2375\n54.178.104.203:2375\nubuntu@devopscube:~$<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/docker-tutorial-getting-started-with-docker-swarm\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker Tutorial : Getting Started With Docker Swarm \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/docker-tutorial-getting-started-with-docker-swarm\/<\/p>\n","protected":false},"author":1,"featured_media":986,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-985","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=985"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/986"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}