Kiến Thức Linux Tutorial

Hướng dẫn cài đặt Consul trên Ubuntu 22.04

Multi-Datacenter Architecture

Consul by HashiCorp is a powerful tool for service discovery, configuration, and orchestration in distributed systems. This guide walks you through installing and setting up a Consul server on Ubuntu 22.04.


📦 Step 1: Install Consul Server

Begin by updating your system and installing required packages:

apt-get update -y
apt-get install unzip gnupg2 curl wget -y

Download the latest Consul binary:

wget https://releases.hashicorp.com/consul/1.21.0/consul_1.21.0_linux_amd64.zip

Unzip and move the binary to /usr/local/bin:

unzip consul_1.21.0_linux_amd64.zip
mv consul /usr/local/bin/

Verify the installation:

consul --version

👤 Step 2: Create Consul User and Directories

Create a system user and group for Consul:

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

Create necessary directories and set permissions:

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

⚙️ Step 3: Configure Consul Server

Generate a gossip encryption key:

consul keygen

Create the main configuration file:

nano /etc/consul.d/config.json

Insert the following configuration, replacing your-server-ip and the encryption key with your actual values:

{
  "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": "ubuntu2204",
  "data_dir": "/var/lib/consul",
  "acl_datacenter": "server1",
  "acl_default_policy": "allow",
  "encrypt": "your-generated-key"
}

Save and exit the file.


🛠️ Step 4: Create Systemd Service for Consul

Create a systemd service file:

nano /etc/systemd/system/consul.service

Add the following content, replacing your-server-ip with your server’s IP address:

[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

Reload systemd and start Consul:

systemctl daemon-reload
systemctl start consul
systemctl enable consul

Check the status:

systemctl status consul

🌐 Step 5: Configure Nginx as a Reverse Proxy (Optional)

Install Nginx:

apt-get install nginx -y

Remove the default site configuration:

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

Create a new site configuration:

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

Add the following content, replacing your-server-ip with your server’s IP address:

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;
  }
}

Enable the new site and restart Nginx:

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

🖥️ Step 6: Access Consul Web Interface

Open your web browser and navigate to:

http://your-server-ip/ui

You should see the Consul dashboard.


✅ Conclusion

You’ve successfully installed and configured Consul on Ubuntu 22.04. Consul is now running as a systemd service and accessible via the web interface. You can proceed to add client nodes or further configure your Consul cluster as needed.

Keywords: Consul installation Ubuntu 22.04, configure Consul server Ubuntu, Consul systemd service setup, Consul web interface Ubuntu

Hướng dẫn cài đặt Cluster Kafka trên Ubuntu


Add Comment

Click here to post a comment