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.

ONET 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, ONET 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

How to Lock Ubuntu Packages and Prevent Them from Updating

There are a few cases when you may want to freeze an Ubuntu package to a particular version. Reasons could range from a...
29/12/2020

Ubuntu Print Screen

Windows is perhaps the easiest and the most user-friendly interfaces I have ever come across. Its usage reminds us of how...
29/12/2020

Install Multitail Ubuntu

Tail is a program in Linux which is used to read a few lines from the end of a large file. You can also use tail to follow...
28/12/2020
Bài Viết

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

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025