Skip to content

Instantly share code, notes, and snippets.

@euclid1990
Forked from yudaykiran/VagrantFile
Created June 24, 2019 08:41
Show Gist options
  • Save euclid1990/1e75c6d8fd1bbc2b6821c328196b46b9 to your computer and use it in GitHub Desktop.
Save euclid1990/1e75c6d8fd1bbc2b6821c328196b46b9 to your computer and use it in GitHub Desktop.

Revisions

  1. @yudaykiran yudaykiran revised this gist Feb 4, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -107,7 +107,7 @@ end
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|


    # Maya Master related only !!
    # K8s Master related only !!
    1.upto(M_NODES.to_i) do |i|
    hostname = "master-%02d" % [i]
    cpus = M_CPUS
    @@ -121,7 +121,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    end
    end

    # OpenEBS Host related only !!
    # K8s Host related only !!
    1.upto(H_NODES.to_i) do |i|
    hostname = "host-%02d" % [i]
    cpus = H_CPUS
  2. @yudaykiran yudaykiran revised this gist Feb 3, 2017. 1 changed file with 29 additions and 40 deletions.
    69 changes: 29 additions & 40 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@ 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
    @@ -20,7 +19,7 @@ M_CPUS = ENV['M_CPUS'] || 1
    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"
    @@ -48,13 +47,11 @@ 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
    @@ -64,7 +61,6 @@ SCRIPT
    #kubeadm join --token=$1 $2
    #SCRIPT

    #Required plugins for Vagrant.
    required_plugins = %w(vagrant-cachier vagrant-triggers)

    required_plugins.each do |plugin|
    @@ -98,12 +94,12 @@ def configureVM(vmCfg, hostname, cpus, mem)
    vmCfg.vm.provider "virtualbox" do |vb|
    vb.memory = mem
    vb.cpus = cpus
    vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
    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

    @@ -118,11 +114,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    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
    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

    # OpenEBS Host related only !!
    @@ -132,33 +128,26 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    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,:append_to_path =>"/usr/bin/", :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."
    end
    end
    end

    #The Master IP will not be empty when it is first provisioned
    #We do not want the Node to join the cluster on each vagrant up
    vmCfg.trigger.after :up, :stdout => true, :stderr => true do
    if get_master_ip_address != ""
    info"Setting up the Minion using IPAddress: #{get_master_ip_address}"
    info"Setting up the Minion using IPAddress: #{token}"
    @machine.communicate.sudo("kubeadm join --token=#{token} #{get_master_ip_address.strip}")
    end
    end
    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
  3. @yudaykiran yudaykiran revised this gist Feb 2, 2017. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -57,15 +57,14 @@ kubeadm init --api-advertise-addresses=$1 --token=$2
    SCRIPT

    $kubeadmjoin = <<SCRIPT
    #Will be used in coming revisions
    #$kubeadmjoin = <<SCRIPT
    #!/bin/bash
    #echo Will run the kubeadm join script ...
    #kubeadm join --token=$1 $2
    #SCRIPT

    echo Will run the kubeadm join script ...
    kubeadm join --token=$1 $2
    SCRIPT

    #Required plugins for Vagrant.
    required_plugins = %w(vagrant-cachier vagrant-triggers)

    required_plugins.each do |plugin|
  4. @yudaykiran yudaykiran created this gist Feb 2, 2017.
    165 changes: 165 additions & 0 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,165 @@
    # -*- 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


    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

    $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|


    # Maya 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

    # OpenEBS 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,:append_to_path =>"/usr/bin/", :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."
    end
    end
    end

    #The Master IP will not be empty when it is first provisioned
    #We do not want the Node to join the cluster on each vagrant up
    vmCfg.trigger.after :up, :stdout => true, :stderr => true do
    if get_master_ip_address != ""
    info"Setting up the Minion using IPAddress: #{get_master_ip_address}"
    info"Setting up the Minion using IPAddress: #{token}"
    @machine.communicate.sudo("kubeadm join --token=#{token} #{get_master_ip_address.strip}")
    end
    end
    end
    end
    end