Skip to content

Instantly share code, notes, and snippets.

@johnp789
Created October 30, 2020 18:13
Show Gist options
  • Save johnp789/94d9cb784ac2cae31c59d1a10fe9e0ca to your computer and use it in GitHub Desktop.
Save johnp789/94d9cb784ac2cae31c59d1a10fe9e0ca to your computer and use it in GitHub Desktop.
nomad large batch of small tasks test
# -*- mode: ruby -*-
# # vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
servers = [
{
"name" => "nomad-server",
"ram" => "3072",
"cpus" => "1",
"ip" => "172.17.8.101"
}
]
clients = [
{
"name" => "nomad-client-one",
"ram" => "3072",
"cpus" => "1",
"ip" => "172.17.8.102"
},
{
"name" => "nomad-client-two",
"ram" => "2048",
"cpus" => "1",
"ip" => "172.17.8.103"
}
]
### client setup script, this starts the job via nomad-client-one ###
$server_script = <<SCRIPT
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y unzip curl wget
cd /tmp/
curl -sSL https://releases.hashicorp.com/nomad/1.0.0-beta2/nomad_1.0.0-beta2_linux_amd64.zip -o nomad.zip
unzip nomad.zip
sudo chmod +x nomad
sudo mv nomad /usr/bin/nomad
sudo mkdir -p /etc/nomad.d
sudo chmod a+w /etc/nomad.d
cat <<EOF | sudo tee /etc/nomad-server.hcl
log_level = "INFO"
data_dir = "/var/tmp/nomad/server"
server {
enabled = true
bootstrap_expect = 1
}
advertise {
http = "0.0.0.0:4646"
rpc = "172.17.8.101:4647"
serf = "172.17.8.101:4648"
}
EOF
cat <<EOF | sudo tee /etc/systemd/system/nomad-server.service
[Unit]
Description=Nomad server
Wants=network-online.target
After=network-online.target
[Service]
ExecStart= /bin/sh -c "/usr/bin/nomad agent -config=/etc/nomad-server.hcl"
Restart=always
RestartSec=10
IPAccounting=yes
MemoryAccounting=yes
CPUAccounting=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now nomad-server
SCRIPT
### client setup script, this starts the job via nomad-client-one ###
$client_script = <<SCRIPT
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y unzip curl wget
cd /tmp/
curl -sSL https://releases.hashicorp.com/nomad/1.0.0-beta2/nomad_1.0.0-beta2_linux_amd64.zip -o nomad.zip
unzip nomad.zip
sudo chmod +x nomad
sudo mv nomad /usr/bin/nomad
sudo mkdir -p /vagrant/alpine/\$HOSTNAME
cat <<EOF | sudo tee /etc/nomad-client.hcl
log_level = "INFO"
data_dir = "/var/tmp/nomad/client"
client {
enabled = true
servers = ["172.17.8.101:4647"]
host_volume "alpine" {
path = "/vagrant/alpine/\$HOSTNAME"
read_only = false
}
}
advertise {
http = "{{ GetPrivateInterfaces | include \\\"network\\\" \\\"172.17.0.0/16\\\" | attr \\\"address\\\" }}:4646"
rpc = "{{ GetPrivateInterfaces | include \\\"network\\\" \\\"172.17.0.0/16\\\" | attr \\\"address\\\" }}:4647"
serf = "{{ GetPrivateInterfaces | include \\\"network\\\" \\\"172.17.0.0/16\\\" | attr \\\"address\\\" }}:4648"
}
EOF
cat <<EOF | sudo tee /etc/systemd/system/nomad-client.service
[Unit]
Description=Nomad client
Wants=network-online.target
After=network-online.target
[Service]
ExecStart= /bin/sh -c "/usr/bin/nomad agent -config=/etc/nomad-client.hcl"
Restart=always
RestartSec=10
IPAccounting=yes
MemoryAccounting=yes
CPUAccounting=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now nomad-client
cat <<EOF | tee alpineBatch.hcl
job "alpineBatch-25000" {
datacenters = ["dc1"]
type = "batch"
group "alpines" {
count = 25000
volume "data" {
type = "host"
read_only = false
source = "alpine"
}
task "alpineExample" {
driver = "docker"
config {
image = "alpine:3"
command = "sh"
args = [ "-c", join("", [
"echo ",
"\\\`adjtimex | awk '/(time.tv_sec|time.tv_usec):/ { printf(\\\"%09d\\\", \\\$2) }'\\\` ",
"\\\${node.unique.name} ",
"\\\${NOMAD_JOB_NAME} ",
"\\\${NOMAD_ALLOC_INDEX} ",
">> /data/alpineExample.log"])]
}
resources {
cpu = 100
memory = 32
}
volume_mount {
volume = "data"
destination = "/data"
read_only = false
}
}
}
}
EOF
if [ \$HOSTNAME == "nomad-client-one" ] ; then
until nomad node status | grep ready ; do
sleep 1
done
nomad run alpineBatch.hcl || echo failure is OK
fi
SCRIPT
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
servers.each do |machine|
config.vm.define machine["name"] do |m|
m.vm.hostname = machine["name"]
m.vm.provision "shell", inline: $server_script, privileged: false
m.vm.network "forwarded_port", guest: 4646, host: 4646, auto_correct: true, host_ip: "127.0.0.1"
m.vm.box = "ubuntu/focal64"
m.vm.network "private_network", ip: machine["ip"]
m.vm.provider :virtualbox do |vb|
vb.name = machine["name"]
vb.memory = machine["ram"]
vb.cpus = machine["cpus"]
vb.gui = false
end
end
end
clients.each do |machine|
config.vm.define machine["name"] do |m|
m.vm.hostname = machine["name"]
m.vm.provision "docker"
m.vm.provision "shell", inline: $client_script, privileged: false
m.vm.box = "ubuntu/focal64"
m.vm.network "private_network", ip: machine["ip"]
m.vm.provider :virtualbox do |vb|
vb.name = machine["name"]
vb.memory = machine["ram"]
vb.cpus = machine["cpus"]
vb.gui = false
end
end
end
end
@johnp789
Copy link
Author

plot2

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