Skip to content

Instantly share code, notes, and snippets.

@mak3r
Last active August 16, 2018 19:11
Show Gist options
  • Save mak3r/432be4affbf253bef0fef104a2d8945b to your computer and use it in GitHub Desktop.
Save mak3r/432be4affbf253bef0fef104a2d8945b to your computer and use it in GitHub Desktop.
Get a VM with a specific version of the kubectl client.
# encoding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
#put <<FYI
# create a virtualbox vm with the latest version of kubectl
# vagrant up
# create a virtualbox vm with a specific version of kubectl
# KUBECTL_VER=v1.9.7 vagrant up
#FYI
# Box / OS
VAGRANT_BOX = 'minimal/xenial64'
if( ENV['KUBECTL_VER'] == nil)
SUFFIX = 'v-latest'
else
SUFFIX = ENV['KUBECTL_VER'].dup.gsub!(/[\.]/, '-').to_s
end
# kubectl version
## Uncomment for the latest version
ENV['KUBECTL_VER'] = '$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)'
# VM Name with kubectl version suffix
VM_NAME = 'kubectl-' + SUFFIX
# VM User — 'vagrant' for simplification across local vms
VM_USER = 'vagrant'
# passthrough some environment variables from the host to the guest machine
# when the shell starts with the install script, it will load environment variables from /etc/profile.d/*.sh
$set_environment_variables = <<SCRIPT
tee "/etc/profile.d/vagrantvars.sh" > "/dev/null" <<EOF
# The kubectl version
export KUBECTL_VER=#{ENV['KUBECTL_VER']}
EOF
SCRIPT
Vagrant.configure(2) do |config|
# Vagrant box from Hashicorp
config.vm.box = VAGRANT_BOX
#use the default 'insecure' key
# this simplifies repackaging the box later
# and for a non-server, non-public, local machine this is fine
config.ssh.insert_key = false
# Actual machine name
config.vm.hostname = VM_NAME
# Set VM name in Virtualbox
config.vm.provider "virtualbox" do |v|
v.name = VM_NAME
v.memory = 2048
end
#DHCP — comment this out if planning on using NAT instead
config.vm.network "private_network", type: "dhcp"
# Disable default Vagrant folder, use a unique path per project
config.vm.synced_folder '.', '/home/' + VM_USER + '/host_' + VM_NAME
# Put the environment
config.vm.provision "shell", inline: $set_environment_variables, run: "always"
# Install kubectl
config.vm.provision "shell", inline: <<-SHELL
curl -LO https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_VER/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
SHELL
end
@mak3r
Copy link
Author

mak3r commented Aug 16, 2018

Vagrantfile to:
create a virtualbox vm with the latest version of kubectl
vagrant up
create a virtualbox vm with a specific version of kubectl
KUBECTL_VER=v1.9.7 vagrant up

If you halt and up again, you can specify a different version. If you halt and don't specify any version, you will get latest

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