How to Install and Configure Consul Server on Ubuntu 18.04

29/12/2020
Consul is an open source service discovery tool which is based and built on Golang. It helps you discovering services application requirements like database, queues, and emails. It comes with some awesome features like Service Discovery, Health Check Status, Key/Value Store, Multi-Datacenter Deployment, and Web UI. It is highly available and scalable upto thousands of nodes. It is fully secured and work with large infrastructure adapting modern practices for DevOps.

Prerequisites

  • A New Ubuntu 18.04 instance with at least 1 GB RAM
  • A sudo user

Update the System

We recommend you to upgrade all the available packages and update the system, before installing any new package on the system. Execute the following command and it will do the job for you.

sudo apt-get update

Next, you will need to install some required packages such as unzip and tmux if you don’t have installed them already. tmux will help you to access multiple separate terminal sessions inside a single terminal window.

sudo apt-get install unzip sudo apt-get install tmux

Install Nginx

Once the system is updated, you will need to install a web server to access Consul through Web UI from a web browser. Here, we will be installing Nginx web server, you can install Apache web server if you wish. Run the following command to install the Nginx web server.

sudo apt-get install nginx

Download and Install Consul

We have installed all the required dependencies now we are ready to download and install Consul. First, change your current directory to /usr/local/bin using the following command.

cd /usr/local/bin

Now you can download the latest stable version of Consul from the official Consul Download Webpage using wget command. Execute the following command and it will do the job for you.

sudo wget https://releases.hashicorp.com/consul/1.4.4/consul_1.4.4_linux_amd64.zip

Next, you will need to unzip the downloaded package and remove the zipped package. Simply, execute the following commands and they’ll do the job for you.

sudo unzip consul_1.4.4_linux_amd64.zip sudo rm -rf consul_1.4.4_linux_amd64.zip

Configure Consul Server

Now we have downloaded the Consul server successfully. Next, you will need to configure the consul server. Create a new directory for configuration files.

cd ~ mkdir -p consul-config/server

Next, create a new configuration file config.json for Consul configuration using any text editor. Here, we are using nano text editor. You can also install it using sudo apt-get install nano command.

sudo nano consul-config/server/config.json

Add the following configuration into the file.

{
"bootstrap": true,
"server": true,
"log_level": "DEBUG",
"enable_syslog": true,
"datacenter": "server1",
"addresses" : {
"http": "0.0.0.0"
},
"bind_addr": "IP Address",
"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=="
}

Make sure to change bind_addr and node_name with your own servers IP or FQDN, then save and close the file.

Next, you will need to create a virtual host in order to configure your web server. So, create a new directory for creating a virtual host for nginx using the following command.

sudo nano /etc/nginx/conf.d/consul.conf

Add the following content into the file.

server
{
listen 80 ;
server_name YourServerIP;
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;
}
}

Make sure to replace YourServerIP with you actual server IP address or domain name and then save and close the file.

Next, reload the Nginx services for these changes to take effect. Execute the following command to do so. sudo systemctl restart nginx.service

Start the Consul Server

You have successfully installed and configured the consul server with its required dependencies. Now let’s start the Consul server and use it. Run the following command to start its services under the tmux terminal. tmux

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

Now you will see the consul process is running in your terminal, you can leave these processes running in the background by using CTRL+B then hit the D key from the keyboard to detach the terminal.

Now that you have successfully installed Consul, you can proceed further to access Consul web interface. But first we will need to set up our consul agent to access the web ui. Execute the following command and it will do the job for you.

consul agent -dev -ui

The above command will take some time.

Open up your favorite web browser and navigate to the http://YourServerIP/ui. Now you can use the consul server to check the health of your servers and an overview of your services.

Conclusion

In this tutorial, you have learned how to install and configure the Consul server on Ubuntu 18.04. We hope now you have enough knowledge to work with the Consul server. Now you can deploy your new services and overview of those services using Consul.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Nvidia Drivers on Ubuntu

Install Nvidia Optimus Graphics Drivers on Ubuntu 17.10 In this article, I will show you how to install Nvidia Optimus drivers...
12/02/2020

Install ZFS File System on Ubuntu 18.04 LTS

In this article, I will show you how to install and setup ZFS Filesystem on Ubuntu 18.04 LTS. Let’s get started. Installing...
28/12/2020

How To Install Bitcoin Core on Ubuntu

In this article, I will talk about what Bitcoin is, what a Bitcoin wallet is, how to install Bitcoin core on Ubuntu and how...
12/02/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021