Installing Consul on Linux/Centos

Consul is a distributed, highly available service discovery and configuration system. It can be used to present your services and ports in a flexible interface which the clients can access to have details of complete infrastructure. It has different features to keep the details uptodate, highly available and easily queryable.
Few of them are, key value store, dns/http api endpoints, health checks, tagging systems etc

In this article i will explain how to setup a single node consul and do service registration and discovery. Consul cluster setup will be explained in another article at  here

Prerequisites

To start with we will be configuring one server and client on same machine

Download

The consul project’s page provides download links to binary packages for Windows, OS X, and Linux. Go to the link and download the package matching your architechture and OS

copy the resulting file to /usr/local/bin

we’ll start the Consul agent in development mode for now. This mode is useful for bringing up a single-node Consul environment quickly and easily. This setup is not recommended in production environment , it doesn’t persist any state and data loss in obvious.

 

You can see agent started in server mode and taken the leadership role ( as its a single node setup)

Run the following command, to see the members of the cluster

As this is a single node setup, you will see only one member

Registering Services to Consul

Now we will register our first service and query that service.

There are two ways to do this

1. service definition
2. calling http api

We will use the first way to do this

First, create a directory for Consul configuration , where we keep all the service configurations , which in turn loaded by consul

create a new file web1.json, with following content

this configuration has many fields

1 . name : Name of the service
2. Port : Port where service listens
3. check : All the health checks configuration goes here

Now we can restart the consul agent, kill any existing agent processes and start it using

Consul will pick the config files from /etc/consul.d and start the agent

Now we can query the services, it can be done using either dns or http endpoint

DNS

By default, all DNS names are always in the consul namespace, though this is configurable. The service subdomain tells Consul we’re querying services, and the NAME is the name of the service.

 

The above command retuned empty results because health checks are failing, there is nothing listening on port 80. Now we will start some service which listen on port 80.

For example, i will install httpd server and start it

start it with

and make sure its running and listening on port 80

Now, you can query DNS server again using same command

Now you can see the results in the answer section contains the details of service, port and host .

Anyone can make this query and use these results to access that particular service. This is very useful in a microservice based architecture, where applications and ports are assigned dynamically. Whenever an application starts, it registers to the service which assigned to it. Other programmes/agents which want to use this service, can simply query the dns endpoint to find the available IP and Ports, which application exposed.

I will explain Consul clustering, in next article. Let me know if you face any issue.

One thought on “Installing Consul on Linux/Centos

Leave a Reply

Your email address will not be published. Required fields are marked *