Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sometimes I need to setup a small kube cluster without thinking about it...
Vagrant.configure(2) do |config|
CNI_LOOPBACK = <<-EOF
{
"cniVersion": "0.3.0",
"type": "loopback"
}
EOF
nodes = []
Node = Struct.new(:ip, :subnet, :pod_cidr)
(1...4).each do |i|
nodes[i] = Node.new("10.100.0.11#{i}", "255.255.0.0", "10.200.#{i}.0/24")
end
(1...4).each do |i|
node_info = nodes[i]
cni_bridge = <<-EOF
{
"cniVersion": "0.3.0",
"name": "bridge",
"type": "bridge",
"bridge": "cnio0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"ranges": [
[{"subnet": "#{node_info.pod_cidr}"}]
],
"routes": [{"dst": "0.0.0.0/0"}]
}
}
EOF
config.vm.define "kube-#{i}" do |config|
config.vbguest.auto_update = true
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: node_info.ip, netmask: node_info.subnet
config.vm.hostname = "kube-#{i}"
config.vm.provision "shell", inline: "sudo sed -i 's/127\.0\.0\.1\s*kube-#{i}.*/#{node_info.ip} kube-#{i}/' /etc/hosts"
(1...4).each do |j|
if i == j
next
end
other = nodes[j]
config.vm.provision "shell", inline: "grep kube-#{j} /etc/hosts && exit 0; sudo echo #{other.ip} kube-#{j} >> /etc/hosts"
config.vm.provision "shell", inline: "ip route | grep #{other.pod_cidr} && exit 0; ip route add #{other.pod_cidr} via #{other.ip}"
end
config.vm.provision "shell", inline: "which dockerd && exit 0; sudo curl https://get.docker.com | sh && sudo usermod -G docker ubuntu"
config.vm.provision "shell", inline: <<-eos
which kubeadm && exit 0
sudo apt-get update && apt-get install -y curl apt-transport-https inotify-tools
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update && sudo apt-get install -y kubelet kubeadm kubectl
eos
unless Dir.exists?("./net.d.#{i}")
Dir.mkdir("./net.d.#{i}")
end
f = File.new("./net.d.#{i}/99-loopback.conf", "w+")
f.write(CNI_LOOPBACK)
f.close()
f = File.new("./net.d.#{i}/10-bridge.conf", "w+")
f.write(cni_bridge)
f.close()
config.vm.synced_folder "./net.d.#{i}", "/etc/cni/net.d"
config.vm.provision "shell", inline: <<-eos
[ -d /opt/cni/bin ] && exit 0
echo Downloading CNI plugins
sudo /opt/cni/bin
curl -sSLf https://github.com/containernetworking/plugins/releases/download/v0.6.0/cni-plugins-amd64-v0.6.0.tgz | tar -zx -C /opt/cni/bin
eos
unless Dir.exists?("./share")
Dir.mkdir("./share")
end
config.vm.synced_folder "./share", "/share"
if i == 1
unless Dir.exists?("./.kube")
Dir.mkdir("./.kube")
end
config.vm.synced_folder ".kube", "/root/hostkube"
config.vm.provision "shell", inline: <<-eos
[ -f /share/join ] && kubeadm reset
sudo kubeadm init \
--apiserver-advertise-address #{node_info.ip} \
--apiserver-cert-extra-sans 127.0.0.1 \
| grep 'kubeadm join --token' > /share/join
cat /etc/kubernetes/admin.conf | sed s/#{node_info.ip.split(".").join('\.')}/127.0.0.1/ > /root/hostkube/config
rm -rf /root/.kube
mkdir -p /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config
eos
config.vm.network "forwarded_port", guest: 6443, host: 6443, guest_ip: node_info.ip, host_ip: "127.0.0.1"
else
config.vm.provision "shell", inline: "kubeadm reset && sudo bash -c 'eval $(cat /share/join)'"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.