Skip to content

Instantly share code, notes, and snippets.

@kawsark
Created April 15, 2020 22:22
Show Gist options
  • Save kawsark/d9a4b1c803e3a632383f42c007291625 to your computer and use it in GitHub Desktop.
Save kawsark/d9a4b1c803e3a632383f42c007291625 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set variables
export PATH="$${PATH}:/usr/local/bin"
export local_ip="$(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip)"
consul_version="1.7.2"
consul_url="https://releases.hashicorp.com/consul/${consul_version}/consul_${consul_version}_linux_amd64.zip"
curl "${consul_url}" -o consul.zip
unzip consul.zip
mv consul /usr/local/bin/consul
chmod +X /usr/local/bin/consul
echo "Installed consul binary: $(consul --version)"
CONSUL_CONFIG_DIR=/etc/consul.d
CONSUL_DATA_DIR=/opt/consul
echo "Creating directories"
useradd --system --home /etc/consul.d --shell /bin/false consul
mkdir -p $${CONSUL_CONFIG_DIR}
mkdir -p $${CONSUL_DATA_DIR}
echo "Writing consul systemd unit file"
cat <<-EOF > /etc/systemd/system/consul.service
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
Restart=always
RestartSec=15s
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM
EOF
export CONSUL_HTTP_ADDR="http://127.0.0.1:8501"
export CONSUL_HTTP_SSL_VERIFY=false
# Write consul client configuration
cat <<EOF > /etc/consul.d/client.hcl
datacenter = "${dc}"
data_dir = "$${CONSUL_DATA_DIR}"
bind_addr = "$${local_ip}"
server = false
ui = true
log_level = "trace"
retry_join = ${retry_join}
encrypt = "${consul_encrypt}"
encrypt_verify_incoming = false
encrypt_verify_outgoing = false
verify_incoming = false
verify_incoming_https = false
ports = {
http = 8500
}
EOF
chown -R consul:consul "$${CONSUL_CONFIG_DIR}"
chown -R consul:consul "$${CONSUL_DATA_DIR}"
echo "Starting consul client"
systemctl enable consul.service
systemctl daemon-reload
systemctl start consul.service
sleep 5
consul members
# Setup bash profile
cat <<PROFILE | sudo tee /etc/profile.d/consul.sh
export CONSUL_HTTP_ADDR="http://127.0.0.1:8500"
PROFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment