Last active
May 8, 2020 07:27
-
-
Save yudaykiran/bdd8e1827e641c6bfce6794f6cdc9f0e to your computer and use it in GitHub Desktop.
VagrantFile for automating Kubernetes Setup
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
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! | |
VAGRANTFILE_API_VERSION = "2" | |
Vagrant.require_version ">= 1.6.0" | |
# K8s Master Nodes | |
M_NODES = ENV['M_NODES'] || 1 | |
# K8s Minion Nodes | |
H_NODES = ENV['H_NODES'] || 2 | |
# Master node Memory & CPUs | |
M_MEM = ENV['M_MEM'] || 512 | |
M_CPUS = ENV['M_CPUS'] || 1 | |
# Minion Host Memory & CPUs | |
H_MEM = ENV['H_MEM'] || 1024 | |
H_CPUS = ENV['H_CPUS'] || 1 | |
#Local Variables | |
master_ip_address = %Q(ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1) | |
get_master_ip_address = "" | |
token = "a5f0cb.caa312d12724203d" | |
# Generic installer script common for server(s) & client(s) | |
# This expects arguments that provide runtime values | |
$installer = <<SCRIPT | |
#!/bin/bash | |
echo Will run the common installer script ... | |
# Update apt and get dependencies | |
sudo apt-get update | |
sudo apt-get install -y unzip curl wget | |
# Install docker and K8s | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - | |
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list | |
deb http://apt.kubernetes.io/ kubernetes-xenial main | |
EOF | |
apt-get update | |
# Install docker if you don't have it already. | |
apt-get install -y docker.io | |
apt-get install -y kubelet kubeadm kubectl kubernetes-cni | |
SCRIPT | |
$kubeadminit = <<SCRIPT | |
#!/bin/bash | |
echo Will run the kubeadm init script ... | |
kubeadm init --api-advertise-addresses=$1 --token=$2 | |
SCRIPT | |
#Will be used in coming revisions | |
#$kubeadmjoin = <<SCRIPT | |
#!/bin/bash | |
#echo Will run the kubeadm join script ... | |
#kubeadm join --token=$1 $2 | |
#SCRIPT | |
required_plugins = %w(vagrant-cachier vagrant-triggers) | |
required_plugins.each do |plugin| | |
need_restart = false | |
unless Vagrant.has_plugin? plugin | |
system "vagrant plugin install #{plugin}" | |
need_restart = true | |
end | |
exec "vagrant #{ARGV.join(' ')}" if need_restart | |
end | |
def configureVM(vmCfg, hostname, cpus, mem) | |
vmCfg.vm.box = "ubuntu/xenial64" | |
vmCfg.vm.hostname = hostname | |
vmCfg.vm.network "private_network", type: "dhcp" | |
#Adding Vagrant-cachier | |
if Vagrant.has_plugin?("vagrant-cachier") | |
vmCfg.cache.scope = :machine | |
vmCfg.cache.enable :apt | |
vmCfg.cache.enable :gem | |
#To fix the apt permission issue. | |
vmCfg.cache.synced_folder_opts = {owner: "_apt", group: "_apt"} | |
end | |
# Set resources w.r.t Virtualbox provider | |
vmCfg.vm.provider "virtualbox" do |vb| | |
vb.memory = mem | |
vb.cpus = cpus | |
vb.customize ["modifyvm", :id, "--cableconnected1", "on"] | |
end | |
# Script will make some directories before installation procedure | |
vmCfg.vm.provision "shell", inline: $installer, privileged: true | |
return vmCfg | |
end | |
# Entry point of this Vagrantfile | |
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
# K8s Master related only !! | |
1.upto(M_NODES.to_i) do |i| | |
hostname = "master-%02d" % [i] | |
cpus = M_CPUS | |
mem = M_MEM | |
config.vm.define hostname do |vmCfg| | |
vmCfg = configureVM(vmCfg, hostname, cpus, mem) | |
vmCfg.vm.provision :shell, inline: "echo Setting up the Master using IPAdress: $1", :args => "`#{master_ip_address}`" | |
vmCfg.vm.provision :shell, inline: "echo Setting up the Master using Token: $1", :args => "#{token}" | |
vmCfg.vm.provision :shell, inline: $kubeadminit, :args => "`#{master_ip_address}` #{token}", privileged: true | |
end | |
end | |
# K8s Host related only !! | |
1.upto(H_NODES.to_i) do |i| | |
hostname = "host-%02d" % [i] | |
cpus = H_CPUS | |
mem = H_MEM | |
config.vm.define hostname do |vmCfg| | |
vmCfg = configureVM(vmCfg, hostname, cpus, mem) | |
#We want to run this only when the VM is first provisioned and get the Master IP to join the cluster | |
vmCfg.vm.provision :trigger, :force => true, :stdout => true, :stderr => true do |trigger| | |
trigger.fire do | |
master_hostname = "master-01" | |
get_ip_address = %Q(vagrant ssh #{master_hostname} -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1') | |
get_master_ip_address = `#{get_ip_address}` | |
info"Getting the Master IP to join the cluster..." | |
if get_master_ip_address == "" | |
info"The Kubernetes Master is down, bring it up and manually run:" | |
info"kubeadm --token=<token> <master_ip_address>" | |
info"in order to join the cluster." | |
else | |
info"Setting up the Minion using IPAddress: #{get_master_ip_address}" | |
info"Setting up the Minion using Token: #{token}" | |
@machine.communicate.sudo("kubeadm join --token=#{token} #{get_master_ip_address.strip}") | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment