Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active March 2, 2024 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkn3rd/c5684022928bbe2a564c89fd4512998b to your computer and use it in GitHub Desktop.
Save darkn3rd/c5684022928bbe2a564c89fd4512998b to your computer and use it in GitHub Desktop.
Consul Lab 0 - KodeKloud

Installation

Goal: Install Consul 1.17.0.

# INSTALL
# Source: https://developer.hashicorp.com/consul/downloads
wget -O- https://apt.releases.hashicorp.com/gpg \
  | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
  | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update

# find packages
apt-cache show consul | grep 1\.17\.0

# install 1.17.0
sudo apt install consul=1.17.0-1

Dev Mode

IPADDR=$(hostname -i)

# Run Consul in Dev Mode
consul agent -dev \
  -advertise=$IPADDR \
  -bind=$IPADDR \
  -client=0.0.0.0
  
# Check Ports bound to 3600
sudo kill -9 3000

# Kill the Consul process
kill -9 $(pgrep consul)

Install Consul Server on Node01

# Install
wget -O- https://apt.releases.hashicorp.com/gpg \
  | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
  | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install consul=1.17.0-1

# Configure Systemd Unit
cat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description="Consul"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl

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

[Install]
WantedBy=multi-user.target
EOFcat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description="Consul"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl

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

[Install]
WantedBy=multi-user.target
EOF

# Configure Consul Server Config
IPADDR=$(hostname -i)

cat <<EOF > /etc/consul.d/consul.hcl
log_level  = "INFO"
server = true
bootstrap_expect = 1
ui_config {
  enabled = true
}

datacenter = "consul-cluster"
data_dir   = "/opt/consul/data"

client_addr    = "0.0.0.0"
bind_addr      = "$IPADDR"
advertise_addr = "$IPADDR"
EOF

# Check Status
sudo systemctl start consul
sudo systemctl status consul
journalctl -u consul.service

# Enable 
sudo systemctl enable consul

Install Consul Client on Node02 and Node03

Log into Node02 and Nod03 and run this

# Install
wget -O- https://apt.releases.hashicorp.com/gpg \
  | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
  | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install consul=1.17.0-1

# Configure Systemd Unit
cat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description="Consul"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl

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

[Install]
WantedBy=multi-user.target
EOFcat <<EOF > /etc/systemd/system/consul.service
[Unit]
Description="Consul"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl

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

[Install]
WantedBy=multi-user.target
EOF

# Configure Consul Server Config
IPADDR=$(hostname -i)
SVRADDR="192.0.43.3"

cat <<EOF > /etc/consul.d/consul.hcl
log_level  = "INFO"
server = false
retry_join = ["$SVRADDR"]

datacenter = "consul-cluster"
data_dir   = "/opt/consul/data"

client_addr    = "0.0.0.0"
bind_addr      = "$IPADDR"
advertise_addr = "$IPADDR"
EOF

# Check Status
sudo systemctl start consul
sudo systemctl status consul
journalctl -u consul.service

# Enable 
sudo systemctl enable consul

Check Membership

consul members
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment