Technology

How to Install a k8s Test Cluster with Vagrant and Kubeadm

A few days ago I tried running minikube on Windows and it didn’t work at all. After about 45 minutes of troubleshooting I gave up and decided to put together a bit more manual process using Vagrant and kubeadm. This post will walk you through standing up a small functional Kubernetes cluster in just a few minutes. This Vagrant config starts two VMs with each with 2vCPUs and 3 GB of RAM. They also have 3 networks attached, a public network (host machine’s connected network), static private network (used for Kubernetes internal communications), and a bridge network which is the default network added by Virtual Box.

Before You Begin

  1. Install Virtual Box https://www.virtualbox.org/wiki/Downloads
  2. Install Vagrant https://www.vagrantup.com/downloads.html
  3. Clone this git repo which contains the files necessary to stand up the cluster.

Let’s Go!

  1. From the root of the git repo cloned above. Run vagrant up. Note: Sometimes Vagrant will fail when trying to inject ssl keys. If this happens run vagrant reload && vagrant up Most of the time this will fix the issue.
  2. Once the machines boot and the vagrant command returns you should now have two VMs running. SSH into the master by typing vagrant ssh master
  3. Once you get logged into the VM run sudo kubeadm init --apiserver-advertise-address 10.0.3.15 if all goes well this will give you a functional kubernetes master listening on 10.0.3.15 which is the internal network between the master and node. After this returns you’ll see a command that looks something like this kubeadm join --token <tokenstring>  10.0.3.15:6443 copy it for use later on the node.
  4. You now need to setup the kubectl configs with this command mkdir -p .kube && sudo cp -i /etc/kubernetes/admin.conf ~/.kube/config && sudo chown $(id -u):$(id -g) ~/.kube/config
  5. Type exit to leave the master ssh session for now.
  6. Type vagrant ssh node
  7. Switch to root sudo -i
  8. Paste the kubeadm join command you copied earlier and execute it. It should display Node join complete with some info about certificates and security. Now type exit
    twice.
  9. Now that you have a node we need to enable networking to get things started. Log into the master again with vagrant ssh master
  10. Run the following to setup the flannel network required utilize the cluster kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml Note: You can also run several other pod networks by changing this step. I have not tested any of the others, but you can look at this page for more info on setting them up.
  11. Now run kubectl get nodes. You should see both the master and node listed. They may show NotReady at first, but should eventually report as Ready once the networking components have come online.

That’s it! If all completed successfully your should now have a functional Kubernetes cluster. The default resource allocation is 2 vCPUs and 3GB of RAM. Feel free to adjust these in the vagrant file before creating. It’s also worth noting that this should also work on Mac or Windows.

Updated 7/21/18: Corrected a github URL that had changed.

Please follow and like us:
error

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *