Skip to content

Instantly share code, notes, and snippets.

@avthart
Last active December 27, 2018 11:48
Show Gist options
  • Save avthart/d050b13cad9e5a991cdeae2bf43c2ab3 to your computer and use it in GitHub Desktop.
Save avthart/d050b13cad9e5a991cdeae2bf43c2ab3 to your computer and use it in GitHub Desktop.
Vagrant and Kubeadmin for setting up a single-node Kubernetes Cluster for local development and training. Read more here: https://www.avthart.com/posts/create-your-own-minikube-using-vagrant-and-kubeadm/
# -*- 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 | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y docker.io kubelet kubeadm kubectl
# Kubelet requires swap off (after reboot):
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# Make sure kubelet will use the same cgroup driver as Docker:
sed -i '0,/ExecStart=/s//Environment="KUBELET_EXTRA_ARGS=--cgroup-driver=cgroupfs"\n&/' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# Install using kubeadm
IPADDR=`ifconfig enp0s8 | grep Mask | awk '{print $2}'| cut -f2 -d:`
NODENAME=$(hostname -s)
kubeadm init --apiserver-cert-extra-sans=$IPADDR --node-name $NODENAME
# Copy admin credentials to vagrant user
mkdir -p /home/vagrant/.kube
cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
chown vagrant:vagrant /home/vagrant/.kube/config
# remove master role taint
kubectl taint nodes --all node-role.kubernetes.io/master-
# deploy calico network
kubectl apply -f \
https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/etcd.yaml
kubectl apply -f \ https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml
# get componentstats
kubectl get componentstatus
# get all resources
kubectl get all --all-namespaces
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.hostname = "kubedev"
config.vm.network "private_network", type: "dhcp"
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