Skip to content

Instantly share code, notes, and snippets.

@tillig
Last active December 12, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tillig/76017acc83a4f5e993fc331fbfa25be9 to your computer and use it in GitHub Desktop.
Save tillig/76017acc83a4f5e993fc331fbfa25be9 to your computer and use it in GitHub Desktop.
Vagrantfile for a Google/Azure management environment
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Most boxes use 'vagrant' as the username
# but recent Ubuntu builds use 'ubuntu'.
$username = "vagrant"
# Check for required plugins.
# Vagrant 2.1.3, 2.1.4 plugin checks are broken.
# https://github.com/hashicorp/vagrant/issues/10161
vagrant_version = Gem::Version.new(Vagrant::VERSION)
if Vagrant.plugins_enabled? && (vagrant_version < Gem::Version.new("2.1.3") || vagrant_version > Gem::Version.new("2.1.4"))
installed_plugins = false
required_plugins = %w( vagrant-vbguest vagrant-reload vagrant-cachier vagrant-env )
required_plugins.each do |p|
unless Vagrant.has_plugin? p
# Trying to install using system commands fails in Windows.
# https://github.com/hashicorp/vagrant/issues/8126
puts "Missing Vagrant plugin! Please run:"
puts " vagrant plugin install #{p}"
exit
end
end
end
$provision = <<-SCRIPT
# Don't run apt-get upgrade during provisioning.
# - keyboard-configuration hangs because there's no keyboard.
# - grub-pc hangs because it's not an interactive session.
# https://bugs.launchpad.net/ubuntu/+source/console-setup/+bug/989396/comments/6
# https://github.com/scotch-io/scotch-box-pro/issues/3
printenv
echo "Preparing apt to communicate over HTTPS..."
apt-get update -y
apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
-y
echo "Adding custom apt repos..."
export DISTRO="$(lsb_release -c -s)"
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
apt-key --keyring /etc/apt/trusted.gpg.d/Microsoft.gpg adv --keyserver packages.microsoft.com --recv-keys BC528686B50D79E339D3721CEB3E94ADBE1229CF
add-apt-repository "deb http://packages.cloud.google.com/apt cloud-sdk-$DISTRO main"
add-apt-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $DISTRO stable"
add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $DISTRO main"
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
echo "Refreshing packages for new repos..."
apt-get update -y
echo "Removing old/stale apt packages..."
apt-get remove docker docker-engine docker.io -y
echo "Installing utilities via apt..."
apt-get install \
jq \
openssl \
tar \
gzip \
bash \
git \
snapd \
kubelet \
kubectl \
kubeadm \
google-cloud-sdk \
azure-cli \
docker-ce \
-y
echo "Cleaning up apt packages..."
apt-get autoremove -y
echo "Post-configure Docker install..."
systemctl enable docker
groupadd docker
usermod -aG docker "vagrant"
docker run hello-world
echo "Post-configure kubelet..."
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
if (! which pwsh); then
echo "Installing PowerShell Core (pwsh)..."
snap install powershell --classic
fi
if (! which helm); then
echo "Installing helm..."
curl "https://kubernetes-helm.storage.googleapis.com/helm-v2.11.0-linux-amd64.tar.gz" | tar zx
mv linux-amd64/helm /usr/bin/
rm -rf linux-amd64
helm version --client
fi
if (! which stern); then
echo "Installing stern..."
curl -L -o /usr/local/bin/stern https://github.com/wercker/stern/releases/download/1.10.0/stern_linux_amd64
chmod +x /usr/local/bin/stern
stern --version
fi
if (! which hal); then
echo "Installing halyard..."
curl -O https://raw.githubusercontent.com/spinnaker/halyard/master/install/debian/InstallHalyard.sh
bash InstallHalyard.sh --user #{$username} -y
rm /home/#{$username}/InstallHalyard.sh
hal -v
fi
if (! which minikube); then
echo "Installing minikube..."
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.31.0/minikube-linux-amd64 && chmod +x minikube && cp minikube /usr/local/bin/ && rm minikube
minikube version
fi
echo "Restoring ownership of user home folder..."
chown #{$username}:#{$username} /home/#{$username} -R
if [ -d /home/#{$username}/.docker ]; then
chmod g+rwx /home/#{$username}/.docker -R
fi
echo "Provision script complete."
SCRIPT
$post_up_message = <<-MESSAGE
If this is the first time using the machine, you will need to:
For Google...
- gcloud auth login
- gcloud projects list
- gcloud config set project [PROJECTID]
- gcloud container clusters list
- gcloud container clusters get-credentials [CLUSTERID] --zone [ZONE]
For Azure...
- az login
- az configure
Optionally, you can:
- sudo apt-get upgrade -y
Run minikube on the local VM with:
sudo -E minikube start --vm-driver=none
sudo chown #{$username}:#{$username} /home/#{$username} -R
You don't have to move any .kube/config files. The sudo -E takes
care of that. However, you do need to take ownership of the generated
.minikube files back.
MESSAGE
Vagrant.require_version ">= 1.9.5"
Vagrant.configure("2") do |config|
config.ssh.username = $username
config.ssh.password = "vagrant"
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "private_network", type: "dhcp"
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh"
config.vm.network "forwarded_port", guest: 80, host: 8080, id: "http"
config.vm.network "forwarded_port", guest: 443, host: 8443, id: "https"
config.vm.post_up_message = $post_up_message
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = true
config.vbguest.iso_path = "http://download.virtualbox.org/virtualbox/%{version}/VBoxGuestAdditions_%{version}.iso"
end
if Vagrant.has_plugin?("vagrant-cachier")
# The vagrant-cachier plugin (optional) will speed up rebuilds by reusing downloaded artifacts
# Configure cached packages to be shared between instances of the same base box.
config.cache.scope = :box
end
config.vm.provision "shell", inline: $provision
config.vm.provision :reload
config.vm.provider "virtualbox" do |v|
v.name = "kubernetes-environment"
v.memory = 4096
v.cpus = 2
v.customize ["modifyvm", :id, "--cpuexecutioncap", "100"]
v.customize ["modifyvm", :id, "--monitorcount", "1"]
v.customize ["modifyvm", :id, "--vram", "128"]
v.customize ["modifyvm", :id, "--ioapic", "on"]
v.customize ["modifyvm", :id, "--accelerate3d", "on"]
v.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
v.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/ --timesync-set-threshold", 10000]
end
config.vm.provider "hyperv" do |v|
# Hyper-V requires...
# - Run vagrant as administrator
# - Shared drive usage requires an IPv4 address (https://github.com/hashicorp/vagrant/issues/3247)
# - Shared drive credentials should be just username, not username@domain
v.vmname = "kubernetes-environment"
v.maxmemory = 4096
v.cpus = 2
v.enable_virtualization_extensions = true
v.linked_clone = true
v.vm_integration_services = {
key_value_pair_exchange: true,
guest_service_interface: true,
heartbeat: true,
shutdown: true,
time_synchronization: true
}
end
config.vm.synced_folder ".", "/vagrant"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment