A step-by-step guide to setting up Consul Server on Ubuntu Linux

|

|

This guide provides detailed instructions on installing Consul, a service networking tool by HashiCorp, on Ubuntu Linux. It includes system updates, downloading and installing Consul, and setting up the necessary directories and configurations. The article outlines creating a non-root Consul user, configuring the Consul service with systemd, and starting the service with systemd commands. Additionally,…

This article explains how to install the Consul server on Ubuntu Linux.

Consul by HashiCorp is a service networking solution that automates network configurations, discovers services, and enables secure connectivity across any cloud or runtime environment.

Consul is an excellent tool for managing microservices, service meshes, and distributed systems. Installing a Consul server on Ubuntu Linux allows you to take advantage of these features in your environment.

With Consul, you can organize your services, manage their dependencies, and automate common networking tasks. Consul also provides a web UI for easy administration and monitoring.

Prerequisites

  • A machine running Ubuntu Linux (18.04 or later)
  • Sudo privileges
  • Stable internet connection for software download

Update System Packages

Open up your terminal and begin by updating your package lists:

sudo apt-get update

Then, upgrade your system packages to the latest versions:

sudo apt-get upgrade

Download Consul

Fetch the latest version of Consul from the official HashiCorp releases page. You can do it with wget.

First, install wget if you do not have it already:

sudo apt-get install wget unzip

Next, download the Consul zip archive. You can find the latest version from the link below:

Consul latest version

CONSUL_VERSION="1.17.2"
wget https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip

Replace "1.17.2" with the latest Consul version if there is a newer one available.

Install Consul

Unzip the Consul archive:

unzip consul_${CONSUL_VERSION}_linux_amd64.zip

Move the Consul binary to your system’s PATH so that it can be executed from anywhere:

sudo mv consul /usr/local/bin/

Verify the installation:

consul --version

The command should output similar lines shown below;

Consul v1.17.2
Revision 7736539d
Build Date 2024-01-22T16:55:18Z
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

Create a Consul User

For security reasons, it’s best not to run Consul as root. Create a new user for Consul:

sudo useradd --system --home /etc/consul.d --shell /bin/false consul

Configure Consul

Create configuration directories for Consul:

sudo mkdir --parents /opt/consul
sudo mkdir --parents /etc/consul.d

Grant the Consul user ownership of those directories:

sudo chown --recursive consul:consul /opt/consul
sudo chown --recursive consul:consul /etc/consul.d

Create a basic configuration file by running the command below.

sudo nano /etc/consul.d/consul.hcl

Then, please copy and paste the content below into the file and save it. Remember to replace the bind_addr and advertise_addr addresses with your server’s own.

{
  "datacenter": "dc1",
  "data_dir": "/opt/consul",
  "log_level": "INFO",
  "server": true,
  "ui": true,
  "bind_addr": "192.168.128.2",
  "client_addr": "0.0.0.0",
  "advertise_addr": "192.168.128.2",
  "bootstrap_expect": 1
}

Create a Systemd Service File

You’ll now create a systemd service file to manage the Consul process by running the command below.

sudo nano /etc/systemd/system/consul.service

Then, please copy and paste the content below into the file and save it.

[Unit]
Description=Consul Service
After=network.target

[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

Next, reload the systemd daemon and enable the Consul service to run on system boot:

sudo systemctl daemon-reload
sudo systemctl enable consul

Start and Verify the Consul Service

Start the Consul service:

sudo systemctl start consul

Check the status of the service:

sudo systemctl status consul

You should see similar lines as shown below.

 consul.service - Consul Service
     Loaded: loaded (/etc/systemd/system/consul.service; enabled; preset: enabled)
     Active: active (running) since Fri 2024-01-26 08:17:23 CST; 1min 43s ago
   Main PID: 9990 (consul)
      Tasks: 8 (limit: 4571)
     Memory: 25.9M
        CPU: 1.255s
     CGroup: /system.slice/consul.service
             └─9990 /usr/local/bin/consul agent -config-dir=/etc/consul.d/

Access the Consul Web UI

Consul has a web UI that you can access from your browser. Since you’ve set up the server to bind to all client addresses with "client_addr": "0.0.0.0", you should access the UI at:

http://<Your_Ubuntu_Machine_IP>:8500/ui/

Replace <Your_Ubuntu_Machine_IP> with the actual IP address of your Ubuntu machine.

That should do it!

Conclusion:

Congratulations! You have successfully installed Consul on your Ubuntu server. You can now begin to join other nodes to your cluster, configure service discovery, and set up service meshes according to your organization’s needs.

Like this:



Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.