<file_sep>POD_CIDR=$1
API_IP=$2

echo "[TASK 1] k8s cluster images pull and init"
sudo kubeadm config images pull
sudo kubeadm init  --apiserver-advertise-address=$API_IP --pod-network-cidr=$POD_CIDR >> /root/kubeinit.log 2>/dev/null

echo "[TASK 2] Correct internal ip for the nodes"
ETH0=$(ip -f inet addr show eth1 | grep -Po 'inet \K[\d.]+')
sudo sed -i "s/^ExecStart=\/usr\/bin\/kubelet.*/&  --node-ip $ETH0/"  /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
sudo systemctl daemon-reload && sudo systemctl restart kubelet

echo "[TASK 3] Configure kubectl"
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# for vagrant user
mkdir -p /home/vagrant/.kube
sudo cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
sudo chown $(id -u vagrant):$(id -g vagrant) /home/vagrant/.kube/config
# for root user
mkdir -p /root/.kube
sudo cp -i /etc/kubernetes/admin.conf /root/.kubeconfig
sudo chown $(id -u root):$(id -g root) /root/.kube/config

echo "[TASK 4] Deploy Calico network"
kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://docs.projectcalico.org/v3.18/manifests/calico.yaml >/dev/null 2>&1

echo "[TASK 5] Create join command"
kubeadm token create --print-join-command > /vagrant_data/join.sh
chmod +x /vagrant_data/join.sh

echo "[TASK 6] Install MetalLB"
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/metallb.yaml
kubectl apply -f /vagrant_data/metallb_configmap.yaml<file_sep>POD_CIDR=$1
echo "[TASK 1] Join node to Kubernetes Cluster"
sh /vagrant_data/join.sh

echo "[TASK 2] Correct internal ip for the Nodes"
ETH0=$(ip -f inet addr show eth1 | grep -Po 'inet \K[\d.]+')
sudo sed -i "s/^ExecStart=\/usr\/bin\/kubelet.*/&  --node-ip $ETH0/"  /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
sudo systemctl daemon-reload && sudo systemctl restart kubelet
<file_sep># image name to be used  as the base image for the hosts
IMAGE_NAME = "bento/ubuntu-20.04"
# subnet to be used for the nodes
SUBNET = "172.16.16."
# pod cidr to be used with kubeadm
POD_CIDR = "192.168.0.0/16"
# number of workers to be deployed
NodeCount = 1

Vagrant.configure("2") do |config|
    config.ssh.insert_key = false
    config.vm.box_check_update = false
    config.vm.provision "shell", path: "scripts/bootstrap.sh"

    config.vm.define "master" do |master|
        master.vm.box = IMAGE_NAME
        master.vm.network "private_network", ip: SUBNET + "#{10}"
        master.vm.hostname = "master.jlab.org"
		master.vm.synced_folder "data/", "/vagrant_data"
        master.vm.provider "virtualbox" do |v|
            v.name = "master"
            v.memory = 8192
            v.cpus = 2
        end
        master.vm.provision "shell", path: "scripts/master-pre-req.sh" do |s|
		  s.args = [POD_CIDR, SUBNET + "#{10}"]
        end
    end

    (1..NodeCount).each do |i|
        config.vm.define "worker-#{i}" do |node|
            node.vm.box = IMAGE_NAME
            node.vm.network "private_network", ip: SUBNET + "#{i + 10}"
            node.vm.hostname = "worker-#{i}.jlabs.org"
			node.vm.synced_folder "data/", "/vagrant_data"
            node.vm.provider "virtualbox" do |v|
                v.name = "worker-#{i}"
                v.memory = 8192
                v.cpus = 2
            end
			node.vm.provision "shell", path: "scripts/worker-pre-req.sh"
        end
    end
end<file_sep>
## Fully Automated Vagrant Kubernetes Cluster Setup, with one master and 'N' workers for VirtualBox

Special thanks to [<NAME>](https://github.com/vNugget), have
used his [repo](https://github.com/vNugget/Kubernetes/tree/main/AutoKube) as template

This will install:

- The Kubernetes version (currently 1.21.2)
- Docker as a container runtime
- Calico as a CNI.

Tested on:
```
Windows 10
Ubuntu 20.04 Desktop
```
## Requirements

You have to install:
- [Vagrant](https://www.vagrantup.com/) 
- [VirtualBox](https://www.virtualbox.org/)

## How To Use It

```bash
git clone https://github.com/sudonewdev/k8s-vagrant-windows10
```

```bash
cd k8s-vagrant-windows10
```

```bash
vagrant up
```
```bash
vagrant ssh master-1
```
After this stage you can use kubectl

```
➜  kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
master     Ready    control-plane,master   65m   v1.20.0
worker-1   Ready    <none>                 62m   v1.20.0
worker-2   Ready    <none>                 59m   v1.20.0
```


<file_sep>echo "[TASK 1] Enabling Swap off, fstab swap remove & firewall disable"
sudo sed -i '/ swap / s/^/#/' /etc/fstab
sudo swapoff -a
sudo systemctl disable --now ufw

echo "[TASK 2] Adding kernel settings"
cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sudo sysctl -p

echo "[TASK 3] Patching machine"
sudo apt-get update && sudo apt upgrade -y
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release\
    htop \
    vim \
    bash-completion \
    net-tools \
    unzip \
    tree \
    python3 \
    python3-pip \
    python \
    python-pip \
    git

echo "[TASK 4] Docker repo add & installation"
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo apt-mark hold docker-ce
sudo systemctl enable --now docker
sudo usermod -aG docker $USER

echo "[TASK 5] Add apt repo for k8s"
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" >/dev/null 2>&1

echo "[TASK 6] Installation of k8s components"
sudo apt-get update
sudo apt-get install -y kubelet=1.21.2-00 kubeadm=1.21.2-00 kubectl=1.21.2-00
sudo apt-mark hold kubelet kubeadm kubectl

echo "[TASK 7] kubectl bash completion"
echo "source <(k completion bash)" >> ~/.bashrc
echo "alias k=kubectl" >> /etc/bash.bashrc
echo "complete -F __start_kubectl k" >>  ~/.bashrc 

echo "[TASK 8] Set root password"
echo -e "root\nroot" | passwd root >/dev/null 2>&1
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo systemctl restart sshd

echo "[TASK 9] Update /etc/hosts"
echo "172.16.16.10 master" >> /etc/hosts
echo "172.16.16.11 worker-1" >> /etc/hosts
