Last active
November 10, 2024 12:26
-
-
Save valeriansaliou/1591f7d326a6a8567c3499a8429434a5 to your computer and use it in GitHub Desktop.
Setup Nomad cluster on Alpine Linux on Vultr (1st boot script)
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 | |
# 1. Setup base dependencies | |
apk update | |
apk del ufw | |
apk add htop busybox-extras | |
# 2. Disable Swap | |
swapoff -a | |
sed -i "/swapfile/d" /etc/fstab | |
rm /swapfile | |
# 3. Install Nomad + Docker + Consul | |
apk add nomad | |
rc-update add nomad default | |
apk add consul | |
rc-update add consul default | |
if [[ $(hostname) = nomad-cluster-client-* ]]; then | |
apk add docker | |
rc-update add docker default | |
fi | |
# 4. Export environment variables | |
export NOMAD_CLUSTER_REGION="global" | |
export NOMAD_CLUSTER_DATACENTER="vultr-par" | |
export NOMAD_CLUSTER_NODE_SELF="{{ GetInterfaceIP \\\"eth1\\\" }}" | |
export NOMAD_CLUSTER_SERVER_1="10.24.96.3" | |
export NOMAD_CLUSTER_SERVER_2="10.24.96.4" | |
export NOMAD_CLUSTER_SERVER_3="10.24.96.5" | |
export CONSUL_CLUSTER_DATACENTER=${NOMAD_CLUSTER_DATACENTER} | |
export CONSUL_CLUSTER_NODE_SELF=${NOMAD_CLUSTER_NODE_SELF} | |
export CONSUL_CLUSTER_SERVER_1=${NOMAD_CLUSTER_SERVER_1} | |
export CONSUL_CLUSTER_SERVER_2=${NOMAD_CLUSTER_SERVER_2} | |
export CONSUL_CLUSTER_SERVER_3=${NOMAD_CLUSTER_SERVER_3} | |
export CONSUL_CLUSTER_CLIENT_LOCAL="127.0.0.1" | |
export CONSUL_CLUSTER_ENCRYPT_KEY="XXXXXXX" | |
# 5. Configure Consul | |
rm /etc/consul/* | |
touch /etc/consul/server.hcl | |
chmod 640 /etc/consul/server.hcl | |
chown consul:consul -R /etc/consul/ | |
if [[ $(hostname) = nomad-cluster-server-* ]]; then | |
cat << EOF > /etc/consul/server.hcl | |
datacenter = "${CONSUL_CLUSTER_DATACENTER}" | |
data_dir = "/var/consul" | |
disable_update_check = true | |
enable_syslog = false | |
bind_addr = "${CONSUL_CLUSTER_NODE_SELF}" | |
advertise_addr = "${CONSUL_CLUSTER_NODE_SELF}" | |
client_addr = "${CONSUL_CLUSTER_CLIENT_LOCAL}" | |
server = true | |
bootstrap_expect = 3 | |
encrypt = "${CONSUL_CLUSTER_ENCRYPT_KEY}" | |
retry_join = [ | |
"${CONSUL_CLUSTER_SERVER_1}", | |
"${CONSUL_CLUSTER_SERVER_2}", | |
"${CONSUL_CLUSTER_SERVER_3}" | |
] | |
ui_config { | |
enabled = false | |
} | |
EOF | |
else | |
cat << EOF > /etc/consul/server.hcl | |
datacenter = "${CONSUL_CLUSTER_DATACENTER}" | |
data_dir = "/var/consul" | |
disable_update_check = true | |
enable_syslog = false | |
bind_addr = "${CONSUL_CLUSTER_NODE_SELF}" | |
advertise_addr = "${CONSUL_CLUSTER_NODE_SELF}" | |
client_addr = "${CONSUL_CLUSTER_CLIENT_LOCAL}" | |
encrypt = "${CONSUL_CLUSTER_ENCRYPT_KEY}" | |
retry_join = [ | |
"${CONSUL_CLUSTER_SERVER_1}", | |
"${CONSUL_CLUSTER_SERVER_2}", | |
"${CONSUL_CLUSTER_SERVER_3}" | |
] | |
EOF | |
fi | |
# 6. Configure Nomad | |
if [[ $(hostname) = nomad-cluster-server-* ]]; then | |
cat << EOF > /etc/nomad.d/server.hcl | |
region = "${NOMAD_CLUSTER_REGION}" | |
datacenter = "${NOMAD_CLUSTER_DATACENTER}" | |
data_dir = "/var/lib/nomad" | |
plugin_dir = "/usr/lib/nomad/plugins" | |
disable_update_check = true | |
enable_syslog = false | |
addresses { | |
http = "0.0.0.0" | |
rpc = "${NOMAD_CLUSTER_NODE_SELF}" | |
serf = "${NOMAD_CLUSTER_NODE_SELF}" | |
} | |
advertise { | |
http = "${NOMAD_CLUSTER_NODE_SELF}" | |
rpc = "${NOMAD_CLUSTER_NODE_SELF}" | |
serf = "${NOMAD_CLUSTER_NODE_SELF}" | |
} | |
server { | |
enabled = true | |
bootstrap_expect = 3 | |
server_join { | |
retry_join = [ | |
"${NOMAD_CLUSTER_SERVER_1}", | |
"${NOMAD_CLUSTER_SERVER_2}", | |
"${NOMAD_CLUSTER_SERVER_3}" | |
] | |
retry_max = 0 | |
retry_interval = "5s" | |
} | |
} | |
consul { | |
address = "${CONSUL_CLUSTER_CLIENT_LOCAL}:8500" | |
} | |
ui { | |
enabled = true | |
} | |
EOF | |
else | |
cat << EOF > /etc/nomad.d/server.hcl | |
region = "${NOMAD_CLUSTER_REGION}" | |
datacenter = "${NOMAD_CLUSTER_DATACENTER}" | |
data_dir = "/var/lib/nomad" | |
plugin_dir = "/usr/lib/nomad/plugins" | |
disable_update_check = true | |
enable_syslog = false | |
addresses { | |
http = "0.0.0.0" | |
rpc = "${NOMAD_CLUSTER_NODE_SELF}" | |
serf = "${NOMAD_CLUSTER_NODE_SELF}" | |
} | |
advertise { | |
http = "${NOMAD_CLUSTER_NODE_SELF}" | |
rpc = "${NOMAD_CLUSTER_NODE_SELF}" | |
serf = "${NOMAD_CLUSTER_NODE_SELF}" | |
} | |
server { | |
enabled = false | |
} | |
client { | |
enabled = true | |
servers = [ | |
"${NOMAD_CLUSTER_SERVER_1}", | |
"${NOMAD_CLUSTER_SERVER_2}", | |
"${NOMAD_CLUSTER_SERVER_3}" | |
] | |
options = { | |
"driver.allowlist" = "docker" | |
} | |
} | |
consul { | |
address = "${CONSUL_CLUSTER_CLIENT_LOCAL}:8500" | |
} | |
plugin "docker" { | |
config { | |
volumes { | |
enabled = true | |
} | |
} | |
} | |
EOF | |
fi | |
# 7. Reboot | |
reboot now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment