With help from: https://devopscube.com/setup-kubernetes-cluster-kubeadm/
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: <<-SHELL
VERSION="1.29"
KUBERNETES_VERSION="$VERSION.2-1.1"
OS="xUbuntu_22.04"
sudo apt-get update -y
sudo echo "10.0.0.10 control" >> /etc/hosts
sudo echo "10.0.0.11 node-01" >> /etc/hosts
sudo echo "10.0.0.12 node-02" >> /etc/hosts
sudo swapoff -a
(crontab -l 2>/dev/null; echo "@reboot /sbin/swapoff -a") | crontab - || true
sudo echo "overlay" >> /etc/modules-load.d/crio.conf
sudo echo "br_netfilter" >> /etc/modules-load.d/crio.conf
sudo echo '1' > /proc/sys/net/ipv4/ip_forward
sudo modprobe overlay
sudo modprobe br_netfilter
sudo echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/modules-load.d/crio.conf
sudo echo "net.ipv4.ip_forward = 1" >> /etc/modules-load.d/crio.conf
sudo echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/modules-load.d/crio.conf
sudo sysctl --system
curl -fsSL https://pkgs.k8s.io/addons:/cri-o:/stable:/v$VERSION/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/stable:/v$VERSION/deb/ /" | sudo tee /etc/apt/sources.list.d/cri-o.list
sudo apt-get update -y
sudo apt-get install cri-o -y
sudo systemctl daemon-reload
sudo systemctl enable crio --now
curl -fsSL https://pkgs.k8s.io/core:/stable:/v$VERSION/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v$VERSION/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update -y
sudo apt-get install -y jq kubelet="$KUBERNETES_VERSION" kubectl="$KUBERNETES_VERSION" kubeadm="$KUBERNETES_VERSION"
local_ip="$(ip --json addr show eth0 | jq -r '.[0].addr_info[] | select(.family == "inet") | .local')"
echo "KUBELET_EXTRA_ARGS=--node-ip=$local_ip" | sudo tee /etc/default/kubelet
sudo kubeadm config images pull
SHELL
config.vm.define "control" do |cp|
cp.vm.box = "bento/ubuntu-22.04-arm64"
cp.vm.hostname = "control"
cp.vm.network "private_network", ip: "10.0.0.10"
cp.vm.provider "parallels" do |p|
p.memory = 4048
p.cpus = 2
end
cp.vm.provision "shell", inline: <<-SHELL
NODENAME=$(hostname -s)
POD_CIDR="192.168.0.0/16"
MASTER_PRIVATE_IP=$(ip addr show eth0 | awk '/inet / {print $2}' | cut -d/ -f1)
sudo kubeadm init --apiserver-advertise-address="$MASTER_PRIVATE_IP" --apiserver-cert-extra-sans="$MASTER_PRIVATE_IP" --pod-network-cidr="$POD_CIDR" --node-name "$NODENAME" --ignore-preflight-errors Swap
export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/tigera-operator.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/custom-resources.yaml
mkdir -p /home/vagrant/.kube
sudo cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
sudo chown 1000:1000 /home/vagrant/.kube/ /home/vagrant/.kube/config
sudo apt-get install bash-completion
echo "source <(kubectl completion bash)" >> /home/vagrant/.bashrc
echo "alias k=kubectl" >> /home/vagrant/.bashrc
echo "complete -F __start_kubectl k" >> /home/vagrant/.bashrc
SHELL
end
(1..2).each do |i|
config.vm.define "node-0#{i}" do |node|
node.vm.box = "bento/ubuntu-22.04-arm64"
node.vm.hostname = "node-0#{i}"
node.vm.network "private_network", ip: "10.0.0.1#{i}"
node.vm.provider "parallels" do |p|
p.memory = 2048
p.cpus = 1
end
end
end
end