{"id":802,"date":"2019-04-21T11:07:16","date_gmt":"2019-04-21T11:07:16","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=802"},"modified":"2019-04-21T11:07:16","modified_gmt":"2019-04-21T11:07:16","slug":"hsetup-configure-consul-agent-client-mode","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=802","title":{"rendered":"How to Setup and Configure Consul Agent On Client Mode &#8211; Consul Guide"},"content":{"rendered":"<p>In <a href=\"https:\/\/devopscube.com\/setup-consul-cluster-guide\/\" rel=\"noreferrer noopener\">our last consul post<\/a>, we have explained the steps to setup up a multi-node consul cluster which runs on server more.<\/p>\n<p>If you want to use consul services for your application on a server, you need to set up a consul agent on the client mode to talk to the consul cluster. A consul client agent is also a member of the system which can obtain the configurations present in the consul cluster.<\/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\/consul-agent-architecture-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"632\" height=\"415\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/consul-agent-architecture-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/consul-agent-architecture-1.png 632w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>In this post, we will look into the steps involved in running a consul agent on client mode for querying and retrieving services and information from the consul servers.<\/p>\n<h2 id=\"install-configure-consul-agent-on-client-mode\">Install &amp; Configure Consul Agent On Client Mode<\/h2>\n<p><strong>Step 1<\/strong>: Update the package repositories and install unzip.<\/p>\n<p>For RHEL\/Centos,<\/p>\n<pre><code>sudo yum update -y\nsudo yum install unzip -y<\/code><\/pre>\n<p>For Ubuntu,<\/p>\n<pre><code>sudo apt-get update -y\nsudo apt-get install unzip -y<\/code><\/pre>\n<p><strong>Step 2<\/strong>: Head over to <a href=\"https:\/\/www.consul.io\/downloads.html?ref=devopscube.com\" rel=\"noreferrer noopener\">consul downloads page.<\/a> and get the link for Linux 64 bit.<\/p>\n<p><strong>Step 3<\/strong>: Download the consul binary to \/opt directory.<\/p>\n<pre><code>cd \/opt\nsudo curl -o consul.zip https:\/\/releases.hashicorp.com\/consul\/1.4.4\/consul_1.4.4_linux_amd64.zip<\/code><\/pre>\n<p><strong>Step 4<\/strong>:  Unzip consul binary.<\/p>\n<pre><code>sudo unzip consul.zip<\/code><\/pre>\n<p><strong>Step 5<\/strong>:  Move consul executable to <code>\/usr\/bin<\/code> directory to be accessible system-wide. You can also move it a location which is in your system path.<\/p>\n<pre><code>sudo mv consul \/usr\/bin\/<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Verify the consul executable by executing the consul command. It should list the available commands.<\/p>\n<pre><code>consul<\/code><\/pre>\n<p><strong>Step 7: <\/strong>Create consul config directories.<\/p>\n<pre><code>sudo mkdir -p \/etc\/consul.d\/client\nmkdir \/var\/consul<\/code><\/pre>\n<p><strong>Step 9:<\/strong> Create a consul config file.<\/p>\n<pre><code>sudo vi \/etc\/consul.d\/client\/config.json<\/code><\/pre>\n<p>Copy the following config content to the config.json file. You should have the value of <code>encrypt<\/code> which was used during the consul server configuration. If you don&#8217;t have this value, you can get it from the consul server from <code>\/var\/consul\/serf\/local.keyring<\/code> file. Also, provide the correct <code>datacenter<\/code> value available in the consul server<\/p>\n<pre><code>{\n    \"server\": false,\n    \"datacenter\": \"Us-Central\",\n    \"data_dir\": \"\/var\/consul\",\n    \"encrypt\": \"gsdfHJ3KZvpC\/Zsdf9JZSTQQ==\",\n    \"log_level\": \"INFO\",\n    \"enable_syslog\": true,\n    \"leave_on_terminate\": true,\n    \"start_join\": [\n        \"10.128.0.3\"\n    ]\n}<\/code><\/pre>\n<p><strong>Step 11:<\/strong> Create a consul client service file.<\/p>\n<pre><code>sudo vi \/etc\/systemd\/system\/consul-client.service<\/code><\/pre>\n<p>Copy the following contents<\/p>\n<pre><code>[Unit]\nDescription=Consul Startup process\nAfter=network.target\n \n[Service]\nType=simple\nExecStart=\/bin\/bash -c '\/usr\/bin\/consul agent -config-dir \/etc\/consul.d\/client'\nTimeoutStartSec=0\n \n[Install]\nWantedBy=default.target<\/code><\/pre>\n<p><strong>Step 12:<\/strong> Reload system daemon.<\/p>\n<pre><code>sudo systemctl daemon-reload<\/code><\/pre>\n<p><strong>Step 13:<\/strong> Start &amp; check the status of consul client service.<\/p>\n<pre><code>sudo systemctl start consul-client\nsudo systemctl status consul-client<\/code><\/pre>\n<p><strong>Step 14:<\/strong> Check the consul members using the following command.<\/p>\n<pre><code>consul members<\/code><\/pre>\n<p>You should see your client node in your list of members. It will be of type <code>client<\/code>. The server cluster members will be of type <code>server<\/code>.<\/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\/image-334.png\" class=\"kg-image\" alt=\"consul list client members\" loading=\"lazy\" width=\"1378\" height=\"170\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-334.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-334.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-334.png 1378w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<h3 id=\"query-services-key-value-pairs-from-consul-server\">Query Services &amp; Key Value Pairs From Consul Server<\/h3>\n<p>Now that we have successfully configured the client, lets run some checks by retrieving data from the consul server.<\/p>\n<h4 id=\"list-all-available-services\">List All Available Services<\/h4>\n<p><strong>Method 1<\/strong>: Using consul command<\/p>\n<pre><code>consul catalog services<\/code><\/pre>\n<p><strong>Method 2:<\/strong> Using API<\/p>\n<pre><code>curl http:\/\/127.0.0.1:8500\/v1\/catalog\/services\\?pretty<\/code><\/pre>\n<p><strong>Method 3: <\/strong>Using DNS query. Here you need to specify your service name to get the details.<\/p>\n<pre><code>dig @127.0.0.1 -p 8600 consul.service.consul SRV<\/code><\/pre>\n<h3 id=\"query-key-values-from-consul-client\">Query key Values From Consul Client<\/h3>\n<p>Lets list a key  named <code>backend<\/code> recursively,<\/p>\n<pre><code>consul kv get -recurse backend<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/hsetup-configure-consul-agent-client-mode\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup and Configure Consul Agent On Client Mode &#8211; Consul Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/hsetup-configure-consul-agent-client-mode\/<\/p>\n","protected":false},"author":1,"featured_media":803,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-802","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\/802","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=802"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/802\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/803"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}