{"id":784,"date":"2019-09-21T07:50:36","date_gmt":"2019-09-21T07:50:36","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=784"},"modified":"2019-09-21T07:50:36","modified_gmt":"2019-09-21T07:50:36","slug":"setup-consul-cluster-guide","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=784","title":{"rendered":"How To Setup Consul Cluster (Multi-Node) on Linux &#8211; Beginners Guide"},"content":{"rendered":"<p><a href=\"https:\/\/www.consul.io\/?ref=devopscube.com\">Consul<\/a> is an open source key-value store. It is used for use cases such as <a href=\"https:\/\/devopscube.com\/open-source-service-discovery\/\" rel=\"noopener noreferrer\">service discovery<\/a>, config management, etc. This guide has detailed instructions to set up a consul cluster with multiple nodes.<\/p>\n<h3 id=\"prerequisites\">Prerequisites<\/h3>\n<p><!--kg-card-begin: html--><\/p>\n<ol class=\"cool-list\">\n<li>Three Linux servers<\/li>\n<li>Following ports opened between all three servers. If you on AWS, Azure or GCP make sure you have the security groups and firewall tags added properly to allow communications of the below-mentioned ports.\n<ul>\n<li>8300&nbsp; &#8211; TCP<\/li>\n<li>8301&nbsp; &#8211; TCP &amp; UDP<\/li>\n<li>8302&nbsp; &#8211;&nbsp;TCP &amp; UDP<\/li>\n<li>8400&nbsp; &#8211;&nbsp;TCP<\/li>\n<li>8500&nbsp; &#8211;&nbsp;TCP<\/li>\n<li>8600&nbsp; &#8211;&nbsp;TCP &amp; UDP<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"setup-consul-cluster\">Setup Consul Cluster<\/h2>\n<p>This tutorial is based on a three-node consul cluster. The nodes are named as follows.<\/p>\n<ol>\n<li>consul-1<\/li>\n<li>consul-2<\/li>\n<li>consul-3<\/li>\n<\/ol>\n<p>Follow the steps given below for a fully functional consul cluster.<\/p>\n<p><strong>Install and Configure Consul on All the Three Nodes<\/strong><\/p>\n<p>The following steps have to be performed on all the three nodes except step 4.<\/p>\n<p><strong>Step 1:<\/strong> CD into bin directory and download Linux consul binary <a href=\"https:\/\/www.consul.io\/downloads.html?ref=devopscube.com\">from here<\/a><\/p>\n<pre><code>cd \/usr\/local\/bin\nsudo curl -o consul.zip https:\/\/releases.hashicorp.com\/consul\/1.6.0\/consul_1.6.0_linux_amd64.zip<\/code><\/pre>\n<p><strong>Step 2: <\/strong>Unzip the downloaded file and remove the zip file.<\/p>\n<pre><code>unzip consul.zip\nsudo rm -f consul.zip<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Create the following two directories.<\/p>\n<pre><code>sudo mkdir -p \/etc\/consul.d\/scripts\nsudo mkdir \/var\/consul<\/code><\/pre>\n<p><strong>Step 4: <\/strong>Create a consul secret using the following command from one of the three servers. Copy the secret to a text file.<\/p>\n<pre><code>consul keygen<\/code><\/pre>\n<p><strong>Step 5: <\/strong>Create a config file on all three servers.<\/p>\n<pre><code>sudo vi \/etc\/consul.d\/config.json<\/code><\/pre>\n<p>Copy the following config to the file. Replace <code>encrypt<\/code> value with the secret created in step 4 and <code>start_join<\/code> IP&#8217;s with your server IP&#8217;s.<\/p>\n<pre><code>{\n    \"bootstrap_expect\": 3,\n    \"client_addr\": \"0.0.0.0\",\n    \"datacenter\": \"Us-Central\",\n    \"data_dir\": \"\/var\/consul\",\n    \"domain\": \"consul\",\n    \"enable_script_checks\": true,\n    \"dns_config\": {\n        \"enable_truncate\": true,\n        \"only_passing\": true\n    },\n    \"enable_syslog\": true,\n    \"encrypt\": \"goplCZgdmOFMZ2Q43To0jw==\",\n    \"leave_on_terminate\": true,\n    \"log_level\": \"INFO\",\n    \"rejoin_after_leave\": true,\n    \"server\": true,\n    \"start_join\": [\n        \"10.128.0.2\",\n        \"10.128.0.3\",\n        \"10.128.0.4\"\n    ],\n    \"ui\": true\n}<\/code><\/pre>\n<h3 id=\"create-a-consul-service\">Create a Consul Service<\/h3>\n<p>Execute the following steps on all the three nodes.<\/p>\n<p><strong>Step 1:<\/strong> Create a systemd file.<\/p>\n<pre><code>sudo vi \/etc\/systemd\/system\/consul.service<\/code><\/pre>\n<p>Copy the following contents to the file.<\/p>\n<pre><code>[Unit]\nDescription=Consul Startup process\nAfter=network.target\n\n[Service]\nType=simple\nExecStart=\/bin\/bash -c '\/usr\/local\/bin\/consul agent -config-dir \/etc\/consul.d\/'\nTimeoutStartSec=0\n\n[Install]\nWantedBy=default.target<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Reload the system daemons<\/p>\n<pre><code>sudo systemctl daemon-reload<\/code><\/pre>\n<h3 id=\"bootstrap-and-start-the-cluster\">Bootstrap and Start the Cluster<\/h3>\n<p><strong>Step 1:<\/strong> On consul-1 server, start the consul service<\/p>\n<pre><code>sudo systemctl start consul<\/code><\/pre>\n<p><strong>Step 2: <\/strong>Start consul on other two servers (Consul-2 and consul-3) using the following command.<\/p>\n<pre><code>sudo systemctl start consul<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Check the cluster status by executing the following command.<\/p>\n<pre><code>\/usr\/local\/bin\/consul members<\/code><\/pre>\n<p>You should get an output like the following. It means your consul cluster is up and running.<\/p>\n<pre><code>[devopscube@consul-1 ~]$ \/usr\/local\/bin\/consul members\nNode      Address          Status  Type    Build  Protocol  DC          Segment\nconsul-1  10.128.0.2:8301  alive   server  1.2.0  2         us-central  &lt;all&gt;\nconsul-2  10.128.0.3:8301  alive   server  1.2.0  2         us-central  &lt;all&gt;\nconsul-3  10.128.0.4:8301  alive   server  1.2.0  2         us-central  &lt;all&gt;\n<\/code><\/pre>\n<h3 id=\"access-consul-ui\">Access Consul UI<\/h3>\n<p>From consul version 1.20, UI is an inbuilt consul component.<\/p>\n<p>You can access the consul web UI using the following URL syntax.<\/p>\n<pre><code>http:\/\/&lt;consul-IP&gt;:8500\/ui<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code>http:\/\/35.238.163.87:8500\/ui<\/code><\/pre>\n<p>You can view the UI as shown below.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/screen-shot-2018-07-04-at-12-47-21-am-1.jpg\" class=\"kg-image\" alt=\"consul web ui setup\" loading=\"lazy\" width=\"1572\" height=\"662\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/screen-shot-2018-07-04-at-12-47-21-am-1.jpg 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/screen-shot-2018-07-04-at-12-47-21-am-1.jpg 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/screen-shot-2018-07-04-at-12-47-21-am-1.jpg 1572w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>Also, you can view a complete UI demo <a href=\"https:\/\/demo.consul.io\/ui\/dc1\/services?ref=devopscube.com\" rel=\"noopener noreferrer\">from here<\/a><\/p>\n<p>Other Consul Blog Series,<\/p>\n<ol>\n<li><a href=\"https:\/\/devopscube.com\/hsetup-configure-consul-agent-client-mode\/\" rel=\"noreferrer noopener\">Setup And Configure Consul Agent On Client Mode<\/a><\/li>\n<\/ol>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-consul-cluster-guide\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Setup Consul Cluster (Multi-Node) on Linux &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-consul-cluster-guide\/<\/p>\n","protected":false},"author":1,"featured_media":785,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-784","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\/784","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=784"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/784\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/785"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}