Created
December 22, 2017 14:06
-
-
Save aduzsardi/846f27b9cfdff040a6877d00cd360846 to your computer and use it in GitHub Desktop.
AWS Ubuntu16 consul cluster with autojoin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create 3 EC2 instances - add tag=consul_join , value=cluster to each | |
# EC2 cmds (as root): | |
# ----- | |
apt-get -yqq install unzip &>/dev/null | |
cd /tmp | |
curl -sLo consul.zip https://releases.hashicorp.com/consul/1.0.2/consul_1.0.2_linux_amd64.zip | |
unzip consul.zip >/dev/null | |
chmod +x consul | |
mv consul /usr/local/bin/consul | |
mkdir /mnt/consul /etc/consul.d/ /run/consul | |
PRIVATE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4) | |
PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) | |
cat <<EOF >/etc/consul.d/config.json | |
{ | |
"bind_addr": "$PRIVATE_IP", | |
"advertise_addr": "$PRIVATE_IP", | |
"advertise_addr_wan": "$PUBLIC_IP", | |
"data_dir": "/mnt/consul", | |
"disable_remote_exec": true, | |
"disable_update_check": true, | |
"leave_on_terminate": true, | |
"bootstrap_expect": 3, | |
"node_name": "consul-kms-server-0", | |
"retry_join": ["provider=aws tag_key=consul_join tag_value=cluster"], | |
"server": true | |
} | |
EOF | |
#systemctl svc unit | |
#------------------ | |
cat <<'EOF' > /lib/systemd/system/consul.service | |
[Unit] | |
Description=Consul service discovery agent | |
Requires=network-online.target | |
After=network-online.target | |
[Service] | |
User=consul | |
Group=consul | |
PermissionsStartOnly=true | |
PIDFile=/run/consul/consul.pid | |
EnvironmentFile=-/etc/default/consul | |
Environment=GOMAXPROCS=2 | |
Restart=on-failure | |
ExecStartPre=[ -f "/run/consul/consul.pid" ] && /usr/bin/rm -f /run/consul/consul.pid | |
ExecStart=/usr/local/bin/consul agent $OPTIONS -config-dir=/etc/consul.d | |
ExecReload=/bin/kill -HUP $MAINPID | |
KillSignal=SIGINT | |
TimeoutStopSec=5 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
useradd -d /mnt/consul/ -r -s /bin/sh -U consul | |
chown consul:consul /mnt/consul/ && chown consul:root /etc/consul.d/ -R && chown consul:root /run/consul/ | |
touch /etc/default/consul | |
systemctl enable consul | |
systemctl start consul |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment