Skip to content

Instantly share code, notes, and snippets.

@dhgouveia2
Last active March 27, 2024 18:41
Show Gist options
  • Save dhgouveia2/639df3c42c2434f419f79c3c6eb21778 to your computer and use it in GitHub Desktop.
Save dhgouveia2/639df3c42c2434f419f79c3c6eb21778 to your computer and use it in GitHub Desktop.
Kube 3cluster lab - vmware_desktop provider
Vagrant.configure("2") do |config|
#VM settings
config.vm.provider "vmware_desktop" do |v|
v.vmx["memsize"] = "2048"
v.vmx["numvcpus"] = "2"
end
config.vm.box = "bento/ubuntu-22.04"
(1..3).each do |i|
if i == 1
vm = "master"
else
n = i - 1
vm = "n#{n}"
end
ssh_pub_key = File.readlines("./id_rsa.pub").first.strip
config.vm.define "k8s-lab-#{vm}" do |node|
node.vm.provision "shell",
inline: <<-SHELL
mkdir -p /root/.ssh
echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo echo -e 'overlay\nbr_netfilter' >> /etc/modules-load.d/containerd.conf
sudo modprobe overlay
sudo modprobe br_netfilter
sudo echo 'net.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.d/kubernetes.conf
sudo echo 'net.bridge.bridge-nf-call-iptables = 1' >> /etc/sysctl.d/kubernetes.conf
sudo echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.d/kubernetes.conf
sudo sysctl --system
sudo apt update && apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo apt update && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt install -y containerd.io
sudo containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
sudo curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
sudo echo deb '[signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update && apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
SHELL
if i == 1
node.vm.network "private_network", ip: "192.168.12.100"
node.vm.hostname = "k8s-lab-master"
node.vm.provision "shell", privileged: false,
inline: <<-SHELL
sudo grep -v "k8s-lab-#{vm}" /vagrant/files/hosts | sudo tee -a /etc/hosts
sudo kubeadm init --control-plane-endpoint=k8s-lab-master \
--apiserver-advertise-address=192.168.12.100 \
--pod-network-cidr=10.244.0.0/16
sudo kubeadm token create --print-join-command | sed 's/join/join --discovery-token-unsafe-skip-ca-verification /g' > /vagrant/k8s-join.sh
sudo systemctl enable kubelet
sudo systemctl start kubelet
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
sudo mkdir -p /root/.kube
sudo cp -i /etc/kubernetes/admin.conf /root/.kube/config
sudo cp -i /etc/kubernetes/admin.conf /vagrant/files/config
sudo kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.2/manifests/calico.yaml
sudo systemctl daemon-reload
sudo systemctl restart kubelet
SHELL
else
node.vm.network "private_network", ip: "192.168.12.10#{n}"
node.vm.hostname = "k8s-lab-n#{n}"
node.vm.provision "shell", inline: "sudo grep -v k8s-lab-n#{vm} /vagrant/files/hosts | sudo tee -a /etc/hosts", privileged: false
node.vm.provision "shell", inline: "sudo /vagrant/k8s-join.sh", privileged: false
node.vm.provision "shell", privileged: false,
inline: <<-SHELL
sudo systemctl daemon-reload
sudo systemctl restart kubelet
sudo systemctl enable kubelet
SHELL
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment