Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active May 12, 2017 05: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 gnilchee/56fc399ee54859d124142c1630f55147 to your computer and use it in GitHub Desktop.
Save gnilchee/56fc399ee54859d124142c1630f55147 to your computer and use it in GitHub Desktop.
Vagrantfile to deploy a Nomad cluster with Consul
# -*- mode: ruby -*-
# vi: set ft=ruby :
$script = <<SCRIPT
# Installing required packages
yum -y update
yum install -y unzip curl wget vim-enhanced
# Download Nomad
NOMAD_VERSION=0.5.6
echo Fetching Nomad...
cd /tmp/
curl -sSL https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip -o nomad.zip
echo Installing Nomad...
unzip nomad.zip
chmod +x nomad
mv nomad /usr/bin/nomad
mkdir -p /etc/nomad.d
chmod a+w /etc/nomad.d
# Bring up private interface
ifup eth1
# Set hostname's IP to made advertisement Just Work
sed -i -e "s/.*nomad.*/$(ip addr show eth1 | grep inet | awk '{print $2}' | head -1 | cut -d'/' -f1) $(hostname -s)/" /etc/hosts
# Install Consul only on server
if [ "$(hostname -s)" == "nomad-server" ]; then
# Download Consul
CONSUL_VERSION=0.8.2
echo Fetching Consul...
cd /tmp/
curl -sSL https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip -o consul.zip
echo Installing Consul...
unzip consul.zip
chmod +x consul
mv consul /usr/bin/consul
mkdir -p /etc/consul.d
chmod a+w /etc/consul.d
# Start Consul server
consul agent -server -bootstrap-expect=1 -data-dir=/opt/consul -node=$(hostname -s) -bind=192.168.50.10 \
-client="0.0.0.0" -config-dir=/etc/consul.d -ui &>> /var/log/consul.log &
fi
# Clean up
yum clean all
rm -rf /tmp/*.zip
SCRIPT
$nomadbootstrap = <<BOOTSTRAP
if [ "$(hostname -s)" == "nomad-server" ]; then
# Server nomad config
cat << EOF > /etc/nomad.d/server.hcl
# log verbosity
log_level = "DEBUG"
# Setup data dir
data_dir = "/opt/nomad/server"
# Enable the server
server {
enabled = true
# Self-elect
bootstrap_expect = 1
}
EOF
# Starting Nomad Server
/usr/bin/nomad agent -config /etc/nomad.d/server.hcl &>> /var/log/nomad.log &
else
# Agent nomad config
cat << EOF > /etc/nomad.d/agent.hcl
# log verbosity
log_level = "DEBUG"
# Setup data dir
data_dir = "/opt/nomad/agent"
# Enable the client
client {
enabled = true
# For demo assume we are talking to server1. For production,
# this should be like "nomad.service.consul:4647" and a system
# like Consul used for service discovery.
servers = ["192.168.50.10:4647"]
}
EOF
# Starting Nomad Agent
/usr/bin/nomad agent -config /etc/nomad.d/agent.hcl &>> /var/log/nomad.log &
fi
BOOTSTRAP
Vagrant.configure(2) do |config|
config.vm.define "server" do |server|
server.vm.box = "centos/7"
server.vm.hostname = "nomad-server"
server.vm.network "forwarded_port", guest: 8500, host: 8500
server.vm.network "private_network", ip: "192.168.50.10"
server.vm.provision "shell", inline: $script
server.vm.provision "shell", inline: $nomadbootstrap
server.vm.provision "docker"
end
config.vm.define "agent-1" do |agent1|
agent1.vm.box = "centos/7"
agent1.vm.hostname = "nomad-agent-1"
agent1.vm.network "private_network", ip: "192.168.50.20"
agent1.vm.provision "shell", inline: $script
agent1.vm.provision "shell", inline: $nomadbootstrap
agent1.vm.provision "docker"
end
config.vm.define "agent-2" do |agent2|
agent2.vm.box = "centos/7"
agent2.vm.hostname = "nomad-agent-2"
agent2.vm.network "private_network", ip: "192.168.50.30"
agent2.vm.provision "shell", inline: $script
agent2.vm.provision "shell", inline: $nomadbootstrap
agent2.vm.provision "docker"
end
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment