Created
November 18, 2021 17:10
-
-
Save smitjainsj/0fc5a8939b4b4c9408d841ec215933c1 to your computer and use it in GitHub Desktop.
Multi VM Vagrantfile
This file contains hidden or 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
$SCRIPT = <<'EOF' | |
#!/bin/bash -x | |
#IPADDR=$(ip -4 addr show eth1 | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | head -1) | |
IPADDR=$(ifconfig -a | grep "192.168" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -1) | |
HOSTS="/etc/hosts" | |
FQDN=$(hostname -f) | |
SDN=$(hostname -s) | |
# Grep if the entry already exists | |
if grep -q "$(hostname -f)" /etc/hosts && ! grep -q "${IPADDR}[[:space:]]*${FQDN}[[:space:]]*${SDN}" "${HOSTS}" | |
then | |
echo -e "fixing the ${HOSTS} file ..." | |
LINE=$(grep -n "${FQDN}" "${HOSTS}" | cut -d: -f1) | |
sudo sed -i "${LINE}d" /etc/hosts | |
echo -e "${IPADDR} ${FQDN} ${SDN}" | sudo tee -a "${HOSTS}" | |
fi | |
EOF | |
nodes = [ | |
{ :hostname => 'cp1', :ip => '192.168.56.51', :mem => '3072', :cpus => '2', }, | |
{ :hostname => 'w1', :ip => '192.168.56.52', :mem => '3072', :cpus => '2', }, | |
{ :hostname => 'w2', :ip => '192.168.56.53', :mem => '3072', :cpus => '2', }, | |
{ :hostname => 'w3', :ip => '192.168.56.54', :mem => '2048', :cpus => '1', }, | |
] | |
Vagrant.configure(2) do |config| | |
# Landrush config | |
config.landrush.tld = ['example.com'] | |
config.landrush.upstream 'x.x.x.x' | |
config.landrush.upstream 'y.y.y.y' | |
config.landrush.enabled = true | |
config.landrush.guest_redirect_dns = false | |
config.landrush.host_redirect_dns = true | |
config.landrush.host_interface_excludes = [/lo[0-9]*/, /docker[0-9]+/, /tun[0-9]+/] | |
config.landrush.host_interface = 'enp0s8' | |
config.landrush.host_interface_class = :ipv4 | |
config.landrush.host 'kubernetes.example.com', 'cp1.example.com' | |
# end | |
# box config | |
config.vm.box = 'mycentos/7' # custom created vagrant box | |
config.vm.box_check_update = false | |
config.vbguest.auto_update = false | |
# end | |
nodes.each do |node| | |
config.vm.define node[:hostname] do |nodeconfig| | |
nodeconfig.vm.network :private_network, ip: node[:ip] | |
nodeconfig.vm.hostname = node[:hostname] + ".example.com" | |
nodeconfig.vm.provider :virtualbox do |vb| | |
vb.customize [ "modifyvm", :id, "--memory", node[:mem], "--cpus", node[:cpus], ] | |
vb.customize [ "modifyvm", :id, "--natdnshostresolver1", "on" ] | |
end | |
nodeconfig.vm.synced_folder ".","/vagrant", type: "nfs" | |
if nodeconfig.vm.hostname =~ /^cp1/ | |
nodeconfig.vm.network "forwarded_port", guest: 6443, host: 6443 | |
end | |
nodeconfig.vm.provision :shell, inline: $SCRIPT | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment