How to Install Consul Server on Ubuntu 16

Posted on : May 19, 2017 , Last Updated on : May 19, 2017 By in UBUNTU HOWTO
| Reply More

consul server ubuntuIn this article, we are going to show you the installation and configuration of Consul server on Ubuntu 16.04 LTS. Consul a solution for service discovery and configuration management system which is completely distributed, highly available, and scales to thousands of nodes and services across multiple datacenters. It helps finding services applications requirements like database, emails, and queues, configure services like enabling maintenance mode for a web server and health checking services. It keeps data safe and works with the largest of infrastructures, embracing modern practices and friendly to existing DevOps tools. Consul provides many features to provide consistent and available information about your infrastructure. When we talk about service discovery, the clients of Consul can provide a service, such as api or http, and other clients can use Consul to discover providers of a given service, using either DNS or MySQL, applications can easily find the services they depend upon.

Let's see how we are going to install Consul, Consul Web-UI and Consul Template on Ubuntu 16.04. But you can implement the same steps on other Linux distributions with some modifications.

Prerequisites

In this article, we are using the single instance of Ubuntu 16.04 with base OS installed, but you can use multiple consul servers running in each datacenter as per recommendations, to avoid data loss in the event of a server failure. These are the component that does the heavy lifting and store information about services and key/value information.

Login to your server using root or sudo privileged user to install some basic packages that will be used during the process, before we look into the consul application. We need to get unzip to extract the executables and update the local systems package cache and then install the package using below 'apt' commands.

$ sudo apt-get update
$ sudo apt-get install unzip

We also need to install 'tmux', that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session.

$ sudo apt-get install tmux

In order to access the Consul using its consul web ui package, we need to install a Web server, for this purpose we are going use Nginx. You can install Nginx on your Ubuntu server using below command.

$ sudo apt-get install nginx

These are the basic system requirements, now let's move to the subject to install Consul on Ubuntu.

How to Install Consul on Ubuntu 16

Consul package is not available in the default repositories of Ubuntu, but we can install it from the source package which can be downloaded from the consul web page. Select the latest version, copy its source location and then use below 'wget' command to install its package on your server in 'bin' directory.

$ cd /usr/local/bin
$ sudo wget https://releases.hashicorp.com/consul/0.8.0/consul_0.8.0_linux_amd64.zip

After downloading, extract the package and remove the zipped package using below commands.

$ sudo unzip consul_0.8.0_linux_amd64.zip
$ sudo rm -rf consul_0.8.0_linux_amd64.zip

Now, from the same web page of consul, you will find its web-ui page that can be used to open its web console to manage all services from the web page. Let's run the following command to download the Web-UI package after creating a new directory in your user's home directory.

$ mkdir consul-ui
$ cd consul-ui
$ wget https://releases.hashicorp.com/consul/0.8.0/consul_0.8.0_web_ui.zip

After downloading, extract the package and remove the zipped file.

$ unzip consul_0.8.0_web_ui.zip
$ rm -rf consul_0.8.0_web_ui.zip

consul web-ui

Next, we need to configure Consul server and its Web UI to make it accessible through the web browser.

How to Configure Consul server

Create a new file 'config.json' in the following new directory and put the following configuration parameters in it.
directory and put the following configuration in it. Here P.P.P.P is the private IP of the instance

$ cd ~
$ mkdir -p consul-config/server

Now using below command to create a new file using your command line editor to put the following configuration.

~$ vim consul-config/server/config.json
{
    "bootstrap": true,
    "server": true,
    "log_level": "DEBUG",
    "enable_syslog": true,
    "datacenter": "server1",
    "addresses" : {
      "http": "0.0.0.0"
    },
    "bind_addr": "XX.XX.XX.XX",
    "node_name": "XX.XX.XX.XX",
    "data_dir": "/home/k/consuldata",
    "ui_dir": "/home/k/consul-ui",
    "acl_datacenter": "server1",
    "acl_default_policy": "allow",
    "encrypt": "5KKufILrf186BGlilFDNig=="
}

Here in this configuration file, the first configuration parameter is for bootstrapping. If you are setting up a cluster of multiple consul servers, then make sure that you put this configuration file on only one of your consul servers, or on all of them which gives you more options for bootstrapping. The configuration files are stored in simple JSON, so they're quite easy to manage.

Next, we have implemented some encryption to the whisper protocol that consul uses. It has this functionality built in using a shared secret system. The secret must be a 16-bit base-64 encoded string. In the terminal, we can use the consul command to generate a key of the necessary length and encoding.

$ consul keygen
5KKufILrf186BGlilFDNig==

Make sure to update your bind_addr and node_name with your own servers IP or FQDN. After making your required changes, save and close the editor using ':wq!' and move to the next step to configure your web server.

Create a new configuration file to be placed under the Nginx web server's configuration directory with the following content in it.

$ sudo vim /etc/nginx/conf.d/consul.conf
server
{
  listen 80 ;
  server_name XX.XX.XX.XX;
  root /home/k/consul-ui;
      location / {
        proxy_pass http://127.0.0.1:8500;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
    }
}

Save and close the configuration file, but make sure to replace 'XX' with your domain name or servers' IP address. After that run the command below to restart Nginx service.

$ sudo systemctl restart nginx.service

After restarting Nginx services, you start consul server.

How to start and use Consul server

After configuring Consul server, now we will start its service under the 'tmux' terminal by using below command.

$ tmux
$ consul agent -config-dir ~/consul-config/server -ui-dir ~/consul-ui -bootstrap true -client=0.0.0.0

Now, you will the consul processes that should be running on the tmux session as shown below.

run consul

You can leave the process running by using "ctrl+b than d" to run the consul in the background and your tmux session will be detached.

Now open your web browser following the IP or FQDN of your server and port '8500' to manage your consul server from the web browser.

http://localhost:8500

consul web

That's it, now you can use this interface to check the health of your servers and get an overview of your services and infrastructure. when you don't want to use the web UI, you can close the SSH tunnel or search for the process's pid number using the ps command and grep to search for the port number we forwarded that is "8500" to kill the process.

Conclusion

In this article, we have successfully installed and setup consul server along with its web-ui on Ubuntu 16.04. It is built to be datacenter aware and can support any number of regions without complex configuration. Consul runs on Linux, Mac OS X, and Windows but it's recommended to run the Consul servers only on Linux, however.

Filed Under : DEVOPS, UBUNTU HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.