Consul Installation : Tutorial

This is a quick guide for setting up consul on ubuntu instances.
This tutorial uses aws instances. We will create three consul servers and one client machine.
Three Servers –  Use to handle queries and maintain consistent view of the system. Also store information about services and key-value information.
One Client – Can connect to other three members of the system for information about infrastructure
Server  – IP
server1 – P1.P1.P1.P1
server2 – P2.P2.P2.P2
server3 – P3.P3.P3.P3
client    – P4.P4.P4.P4

Consul Members configuration: common for all server

To start with, install consul on all the four instances using the following steps after logging in to your instances.

Intall unzip and screen

apt-get update
apt-get install unzip screen

Now, screen and unzip are installed.
To start screen session

$screen

Hit Enter and you are in a new screen session
You can copy the download link of consul from this page and use it in the wget command

$cd /usr/local/bin
$sudo wget https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip

The above URL might have changed but you can still get the link from here.
Now unzip the downloaded package

$sudo unzip *.zip
$rm *.zip

Now you should have consul command ready

$consul

We are done with the basic installation

Preparing server1 in bootstrap mode

Start the server in bootstrap mode (bootstrap mode will allow the server to elect itself as a leader)

$consul agent -server -bootstrap -data-dir /tmp/consul

Since we have started the consul server in bootstrap mode it will acquire itself as a leader which you can see in the output after running the above command.
type  CTRL-A C to start new screen session and than run the following command to see if the consul server is running or not.

$consul members

Joining server2 and server3 to the cluster

On server2 and server3, we can now start the consul service without the bootstrap option by running following command.

$consul agent -server -data-dir /tmp/consul

To join server2 and server3 on with server1. Type the following command in the fresh terminal (CTRL-A C) of screen session of window one. To switch between screen session use CTRL-A N

$consul join p2.p2.p2.p2 p3.p3.p3.p3

where p2.p2.p2.p2 is the private IP of server2 and p3.p3.p3.p3 is the private IP of server3
Now you can again run to check the consul members in the cluster

$consul members

Now, we have three servers, so we can rejoin the server1 without bootstrap option so that a leader can be appointed by the election between three server

$consul agent -server -data-dir /tmp/consul

Client configuration

Client will serve the request being received and will also serve the web-ui of consul.
Download and unzip the web-ui of consul
In the wget command, you can copy the link location from the download-web-ui button of this page

$cd ~
$wget https://dl.bintray.com/mitchellh/consul/0.5.2_web_ui.zip
$unzip *.zip
$rm *.zip 


This will leave “dist” directory in the home folder
Now we will start the client with the following command by specifying the client flag and the web-ui directory “dist” and make it to join the cluster

$consul agent -data-dir /tmp/consul -client p4.p4.p4.p4 -ui-dir /home/your-username/dist/ -join p1.p1.p1.p1


This will join our client machine to the cluster as a regular, non-server agent.
Now run the “consul members” command in the server1 fresh terminal screen session to see if the client has join the cluster or not.

Now we can access the web-ui on the public ip of our client after running the following command

$consul members -rpc-addr=p4.p4.p4.p4:8400

Now we can access the web-ui on the following url

http://p4.p4.p4.p4:8500/ui

Now we are done with the basic configuration of consul with 3+1 members.

Leave a Reply

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