Atlantic.Net Blog

How to Install Consul Server on Ubuntu 20.04

Hitesh Jethva
by Atlantic.Net (86posts) under VPS Hosting
0 Comments

Consul is a service mesh solution offering a full-featured control plane, including segmentation functionality, configuration, and service discovery. These features may be used individually as required, or they may be used together for creating a full-service mesh. Consul operates over a data plane and supports both a proxy and a native integration model. Consul ships with a simple built-in proxy that ensures everything works out-of-the-box. Consul also supports 3rd party proxy integrations, such as Envoy.

Key features of Consul include:

  • Service Discovery
  • Health Checking
  • KV Store
  • Secure Service Communication
  • Multi-Datacenter Capabilities

In this tutorial, we will show you how to install a Consul server on Ubuntu 20.04.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.Net Cloud Platform
  • A root password is configured on your server

Step 1 – Create an Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install Consul Server

First, install the required packages with the following command:

apt-get install unzip gnupg2 curl wget -y

Next, download the latest version of Consul with the following command:

wget https://releases.hashicorp.com/consul/1.8.4/consul_1.8.4_linux_amd64.zip

Once the download is completed, unzip the downloaded file with the following command:

unzip consul_1.8.4_linux_amd64.zip

Next, move the consul binary to the /usr/local/bin directory with the following command:

mv consul /usr/local/bin/

Next, verify the Consul version using the following command:

consul --version

You should get the following output:

Consul v1.8.4
Revision 12b16df32
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

Step 3 – Create Consul Service File

First, you will need to create a separate user and group for Consul. You can create them with the following command:

groupadd --system consul
useradd -s /sbin/nologin --system -g consul consul

Next, create required directories with the following command:

mkdir -p /var/lib/consul
mkdir /etc/consul.d

Next, change the ownership and permission of those directories:

chown -R consul:consul /var/lib/consul
chmod -R 775 /var/lib/consul
chown -R consul:consul /etc/consul.d

Next, create a Consul systemd service file with the following command:

nano /etc/systemd/system/consul.service

Add the following lines:

[Unit]
Description=Consul Service Discovery Agent
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -server -ui \
            -advertise=your-server-ip \
            -bind=your-server-ip \
            -data-dir=/var/lib/consul \
            -node=consul-01 \
            -config-dir=/etc/consul.d
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
TimeoutStopSec=5
Restart=on-failure
SyslogIdentifier=consul
[Install]
WantedBy=multi-user.target

Save and close the file when you are finished.

Note: Provide proper server-ip, data directory path, and config directory that you created earlier.

Next, reload the systemd daemon with the following command:

systemctl daemon-reload

Step 4 – Configure Consul Server

First, you will need to generate a key to the necessary length and encoding. You can generate it with the following command:

consul keygen

You should get the following output:

TJ8iw/XJ+0/BSUMGuLFWkeT23LmGnfhmF/qWgA25wZU=

Next, you will need to create a Json configuration file for Consul. You can create it with the following command:

nano /etc/consul.d/config.json

Provide your server IP, hostname and Consul key as shown below:

{
"bootstrap": true,
"server": true,
"log_level": "DEBUG",
"enable_syslog": true,
"datacenter": "server1",
"addresses" : {
"http": "0.0.0.0"
},
"bind_addr": "your-server-ip",
"node_name": "ubuntu2004",
"data_dir": "/var/lib/consul",
"acl_datacenter": "server1",
"acl_default_policy": "allow",
"encrypt": "TJ8iw/XJ+0/BSUMGuLFWkeT23LmGnfhmF/qWgA25wZU="
}

Save and close the file when you are finished.

Next, start the Consul service and enable it to start at system reboot with the following command:

systemctl start consul
systemctl enable consul

Next, verify the status of the Consul with the following command:

systemctl status consul

You should get the following output:

● consul.service - Consul Service Discovery Agent
     Loaded: loaded (/etc/systemd/system/consul.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-10-01 10:35:52 UTC; 4min 11s ago
   Main PID: 5511 (consul)
      Tasks: 9 (limit: 2353)
     Memory: 17.4M
     CGroup: /system.slice/consul.service
             └─5511 /usr/local/bin/consul agent -server -ui -advertise=your-server-ip -bind=your-server-ip -data-dir=/var/lib/consul -node=con>

At this point, the Consul server is started and listening on port 8500. You can check it with the following command:

ss -plunt | grep 8500

You should get the following output:

tcp   LISTEN 0      4096                                  *:8500              *:*                                                                                users:(("consul",pid=5511,fd=17))           

Step 5 – Configure Nginx as a Reverse Proxy

Next, it is a good idea to install and configure Nginx as a reverse proxy to access the Consul on port 80.

First, install the Nginx server with the following command:

apt-get install nginx -y

Once installed, remove the Nginx default virtual host configuration file:

rm -rf /etc/nginx/sites-enabled/default

Next, create a Consul virtual host configuration file with the following command:

nano /etc/nginx/sites-available/consul.conf

Add the following lines:

server {
listen 80 ;
server_name your-server-ip;
root /var/lib/consul;
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 file, then activate the virtual host with the following command:

ln -s /etc/nginx/sites-available/consul.conf /etc/nginx/sites-enabled/

Next, check the Nginx for any syntax errors with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, restart the Nginx service with the following command:

systemctl restart nginx

If you get any errors, then you will need to edit the Nginx default server configuration file and set server_names_hash_bucket_size:

nano /etc/nginx/nginx.conf

Add the following line below http {:

server_names_hash_bucket_size 64;

Save and close the file, then restart the Nginx service to apply the changes:

systemctl restart nginx

Step 6 – Access Consul Dashboard

Next, open your web browser and access the Consul web interface using the URL http://your-server-ip/ui. You should see the Consul dashboard in the following page:

Click on the Nodes to list the active nodes in your server as shown below:

Conclusion

Congratulations! You have successfully installed Consul server on Ubuntu 20.04. You can now add more client nodes to the Consul server and start managing them from the Consul dashboard. For more information, you can visit the Consul documentation. Get started with Consul on VPS Hosting from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources