Skip to content

Instantly share code, notes, and snippets.

@lizrice
Last active February 3, 2023 02:21
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save lizrice/69d3b28979391287176b3b7155a327b9 to your computer and use it in GitHub Desktop.
Save lizrice/69d3b28979391287176b3b7155a327b9 to your computer and use it in GitHub Desktop.
Vagrant file for setting up a single-node Kubernetes cluster that I can access from my desktop. Read more: https://medium.com/@lizrice/kubernetes-in-vagrant-with-kubeadm-21979ded6c63
# -*- mode: ruby -*-
# vi: set ft=ruby :
# This script to install Kubernetes will get executed after we have provisioned the box
$script = <<-SCRIPT
# Install kubernetes
apt-get update && apt-get install -y apt-transport-https
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
apt-get install -y kubelet kubeadm kubectl
# kubelet requires swap off
swapoff -a
# keep swap off after reboot
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# Get the IP address that VirtualBox has given this VM
IPADDR=`ip -4 address show dev eth1 | grep inet | awk '{print $2}' | cut -f1 -d/`
echo This VM has IP address $IPADDR
# Writing the IP address to a file in the shared folder
echo $IPADDR > /vagrant/ip-address.txt
# Set up Kubernetes
NODENAME=$(hostname -s)
kubeadm init --apiserver-cert-extra-sans=$IPADDR --node-name $NODENAME
# Set up admin creds for the vagrant user
echo Copying credentials to /home/vagrant...
sudo --user=vagrant mkdir -p /home/vagrant/.kube
cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
chown $(id -u vagrant):$(id -g vagrant) /home/vagrant/.kube/config
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |v|
v.memory = 16384
v.cpus = 2
end
# Specify your hostname if you like
# config.vm.hostname = "name"
config.vm.box = "bento/ubuntu-20.04"
config.vm.network "private_network", type: "dhcp"
config.vm.provision "docker"
# Specify the shared folder mounted from the host if you like
# By default you get "." synced as "/vagrant"
# config.vm.synced_folder ".", "/folder"
config.vm.provision "shell", inline: $script
end
@lizrice
Copy link
Author

lizrice commented May 22, 2020

Now using ip instead of ifconfig, and using systemd rather than cgroupfs driver

@kodefoundry
Copy link

Hi @lizrice,
I am seeing one issue,
Steps,
I do a vagrant halt, to shut down the VM
then do a 'vagrant up' again

After this 'kubectl' command does not work. I see the below error
The connection to the server <IP noted from $IPADDR>:6443 was refused - did you specify the right host or port?

Any clue??

@lizrice
Copy link
Author

lizrice commented Dec 11, 2020

@kodefoundry I have seen a few times that the swapoff doesn't "stick" between reboots (I don't know why, I'm afraid). Try sudo swapoff -a on the virtual machine. Wait a few seconds and then ps -eaf | grep kube on the virtual machine should show the running kubernetes components.

@saschagrunert
Copy link

A good way to retrieve the IP independently of the interface could be: ip route get 1.2.3.4 | cut -d ' ' -f7 | tr -d '[:space:]'

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