{"id":357,"date":"2016-05-14T20:14:33","date_gmt":"2016-05-14T20:14:33","guid":{"rendered":"http:\/\/tuxlabs.com\/?p=357"},"modified":"2017-11-11T08:54:41","modified_gmt":"2017-11-11T08:54:41","slug":"consul-for-service-discovery","status":"publish","type":"post","link":"https:\/\/tuxlabs.com\/?p=357","title":{"rendered":"Consul for Service Discovery"},"content":{"rendered":"<h2>Why Service Discovery ?<\/h2>\n<p>Service Discovery effectively replaces the process of having to manually assign or automate your own DNS entries for nodes on your network. Service Discovery aims\u00a0to move even further away from treating VM&#8217;s like pets to cattle, by getting rid of the age old practice of Hostname &amp; FQDN having contextual value. Instead when using services discovery nodes are automatically registered by an agent and automatically are configured in DNS for both nodes and services running on the machine.<\/p>\n<h2>Consul<\/h2>\n<p><a href=\"https:\/\/www.consul.io\/\">Consul by Hashicorp<\/a> is becoming the de-facto standard for Service Discovery. Consul&#8217;s full features &amp; simplistic deployment model make it an optimal choice for organizations looking to quickly deploy Service Discovery capabilities in their environment.<\/p>\n<h2>Components of Consul<\/h2>\n<ol>\n<li>The Consul Agent<\/li>\n<li>An optional JSON config file for each service located under \/etc\/consul.d\/&lt;service&gt;.json\n<ol>\n<li>If you do not specific a JSON file, consul can still start and will provide discovery for the nodes (they will have DNS as well)<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2>A Quick Example of Consul<\/h2>\n<h3>How easy is it to deploy console ?<\/h3>\n<ol>\n<li>Download \/ Decompress and install the Consul agent &#8211;\u00a0<a href=\"https:\/\/www.consul.io\/downloads.html\">https:\/\/www.consul.io\/downloads.html<\/a><\/li>\n<li class=\"p1\"><span class=\"s1\">Define services in a JSON file (if you want) &#8211;\u00a0<a href=\"https:\/\/www.consul.io\/intro\/getting-started\/services.html\">https:\/\/www.consul.io\/intro\/getting-started\/services.html<\/a><\/span><\/li>\n<li><span class=\"s2\">Start the agent on the nodes &#8211;\u00a0<a href=\"https:\/\/www.consul.io\/intro\/getting-started\/join.html\">https:\/\/www.consul.io\/intro\/getting-started\/join.html<\/a><\/span><\/li>\n<li><span class=\"s1\">\u00a0<\/span><span class=\"s2\">Make 1 node join 1 other node (does not matter which node) to join the cluster, which gets you access to all cluster metadata<\/span><\/li>\n<\/ol>\n<h4>Steps 1 and 2 Above<\/h4>\n<ol>\n<li>After downloading the Consul binary to each machine and decompressing it, copy it to \/usr\/local\/bin\/ so it&#8217;s in your path.<\/li>\n<li>Create the directory<br \/>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"5332ded3-ecef-4cf3-8d05-a563d06b5b94\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">sudo mkdir \/etc\/consul.d<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<li>Optionally, run the following to create a JSON file defining a fake service running<\/li>\n<\/ol>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9dGV4dH0&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"a0996ff5-e12f-4983-83cd-2010e29ea129\" data-macro-parameters=\"language=text\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">echo '{\"service\": {\"name\": \"web\", \"tags\": [\"rails\"], \"port\": 80}}' \\\r\n    &gt;\/etc\/consul.d\/web.json<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Step 3 Above<\/h4>\n<p>Run the agent on each node, changing IP accordingly.<\/p>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"644526a5-3e33-4560-a619-7bc303650180\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-d415:~$ consul agent -server -bootstrap-expect 1 -data-dir \/tmp\/consul -node=agent-one -bind=10.73.172.110 -config-dir \/etc\/consul.d<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Step 4 Above<\/h4>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"a18f7223-5f22-4236-adac-e5da465bc709\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-d415:~$ consul join 10.73.172.108\r\nSuccessfully joined cluster by contacting 1 nodes.<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Wow, simple&#8230;ok now for the examples&#8230;.<\/p>\n<h3>Show cluster members<\/h3>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"f6487539-f5a6-4c85-ac96-a1ddaa5489db\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-dcb3:~$ consul join 10.73.172.110\r\nSuccessfully joined cluster by contacting 1 nodes.<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Look up DNS for a node<\/h3>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"ceb14f7b-87a0-43da-a3d5-999baa175715\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-dcb3:~$ dig @127.0.0.1 -p 8600 agent-one.node.consul\r\n; &lt;&lt;&gt;&gt; DiG 9.9.5-3ubuntu0.8-Ubuntu &lt;&lt;&gt;&gt; @127.0.0.1 -p 8600 agent-one.node.consul\r\n; (1 server found)\r\n;; global options: +cmd\r\n;; Got answer:\r\n;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 2450\r\n;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0\r\n;; WARNING: recursion requested but not available\r\n;; QUESTION SECTION:\r\n;agent-one.node.consul.\t\tIN\tA\r\n;; ANSWER SECTION:\r\nagent-one.node.consul.\t0\tIN\tA\t10.73.172.110\r\n;; Query time: 1 msec\r\n;; SERVER: 127.0.0.1#8600(127.0.0.1)\r\n;; WHEN: Tue May 03 21:43:47 UTC 2016\r\n;; MSG SIZE  rcvd: 76\r\ntuxninja@consul-dcb3:~$<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Lookup DNS for a service<\/h3>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"07d483b1-9dc1-438a-baa5-a37dda5c4bb7\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-dcb3:~$  dig @127.0.0.1 -p 8600 web.service.consul\r\n; &lt;&lt;&gt;&gt; DiG 9.9.5-3ubuntu0.8-Ubuntu &lt;&lt;&gt;&gt; @127.0.0.1 -p 8600 web.service.consul\r\n; (1 server found)\r\n;; global options: +cmd\r\n;; Got answer:\r\n;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 55798\r\n;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0\r\n;; WARNING: recursion requested but not available\r\n;; QUESTION SECTION:\r\n;web.service.consul.\t\tIN\tA\r\n;; ANSWER SECTION:\r\nweb.service.consul.\t0\tIN\tA\t10.73.172.110\r\n;; Query time: 2 msec\r\n;; SERVER: 127.0.0.1#8600(127.0.0.1)\r\n;; WHEN: Tue May 03 21:46:54 UTC 2016\r\n;; MSG SIZE  rcvd: 70\r\ntuxninja@consul-dcb3:~$<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Query the REST API for Nodes<\/h3>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"80d48579-0b6a-464f-b2f7-f0498d84f5ac\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-dcb3:~$ curl localhost:8500\/v1\/catalog\/nodes\r\n[{\"Node\":\"agent-one\",\"Address\":\"10.73.172.110\",\"TaggedAddresses\":{\"wan\":\"10.73.172.110\"},\"CreateIndex\":3,\"ModifyIndex\":1311},{\"Node\":\"agent-two\",\"Address\":\"10.73.172.108\",\"TaggedAddresses\":{\"wan\":\"10.73.172.108\"},\"CreateIndex\":1338,\"ModifyIndex\":1339}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Query the REST API for Services<\/h3>\n<table class=\"wysiwyg-macro\" style=\"background-image: url('https:\/\/opswiki.symclab.net\/plugins\/servlet\/confluence\/placeholder\/macro-heading?definition=e2NvZGV9&amp;locale=en_GB&amp;version=2'); background-repeat: no-repeat;\" data-macro-name=\"code\" data-macro-id=\"4d4ea819-5571-414e-9fab-64334afe2ec7\" data-macro-schema-version=\"1\" data-macro-body-type=\"PLAIN_TEXT\">\n<tbody>\n<tr>\n<td class=\"wysiwyg-macro-body\">\n<pre class=\"\">tuxninja@consul-dcb3:~$ curl http:\/\/localhost:8500\/v1\/catalog\/service\/web\r\n[{\"Node\":\"agent-one\",\"Address\":\"10.73.172.110\",\"ServiceID\":\"web\",\"ServiceName\":\"web\",\"ServiceTags\":[\"rails\"],\"ServiceAddress\":\"\",\"ServicePort\":80,\"ServiceEnableTagOverride\":false,\"CreateIndex\":5,\"ModifyIndex\":772}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/tuxlabs.com\/?p=357\" rel=\"bookmark\" title=\"Permalink to Consul for Service Discovery\"><p>Why Service Discovery ? Service Discovery effectively replaces the process of having to manually assign or automate your own DNS entries for nodes on your network. Service Discovery aims\u00a0to move even further away from treating VM&#8217;s like pets to cattle, by getting rid of the age old practice of Hostname &amp; FQDN having contextual value. [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91,131,130,1,28,12],"tags":[132,135,113,137,136],"class_list":{"0":"post-357","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-architecture","7":"category-aws","8":"category-cloud","9":"category-howtos","10":"category-openstack-howtos","11":"category-systems-administration","12":"tag-cloud","13":"tag-consul","14":"tag-dns","15":"tag-hashicorp","16":"tag-service-discovery","17":"h-entry","18":"hentry"},"_links":{"self":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357"}],"version-history":[{"count":8,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/683"}],"wp:attachment":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}