-
-
Save ajessup/46ca239af0be209ce56a64b5c1738cf0 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
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 : | |
# This script to install Kubernetes will get executed after we have provisioned the box | |
$script = <<-SCRIPT | |
# Install docker (we do this rather than use the vagrant provisioner so we can fetch a specific version) | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | |
sudo apt-get update | |
sudo apt-cache policy docker-ce | |
sudo apt-get install docker-ce=18.06.0~ce-0~ubuntu | |
# 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 | |
sudo sed -i '0,/ExecStart=/s//Environment="KUBELET_EXTRA_ARGS=--cgroup-driver=cgroupfs"\n&/' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf | |
# Get the IP address that VirtualBox has given this VM | |
IPADDR=`ifconfig eth1 | grep inet | awk '{print $2}'| cut -f2 -d:` | |
echo This VM has IP address $IPADDR | |
# Set up Kubernetes | |
NODENAME=$(hostname -s) | |
echo This VM has IP node name $NODENAME | |
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| | |
# Specify your hostname if you like | |
config.vm.hostname = "apiserver" | |
config.vm.box = "bento/ubuntu-18.04" | |
config.vm.network "private_network", type: "dhcp" | |
#config.vm.provision "docker" | |
config.vm.provision "shell", inline: $script | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment