# Vagrant + VirtualBox

## Virtual Machines

{% code title="Vagrantfile" %}

```ruby
N_NODES=2
ENV["LC_ALL"] = "en_US.UTF-8"
ID = "#{rand(01..99)}"

$script = <<-SCRIPT

apt update

# Disable swap
#/etc/fstab /etc/fstab-BKP
#swapoff -a
#sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# Install Docker
sudo apt-get -y install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
    
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
   
sudo apt-get update

sudo apt-get -y install docker-ce=18.06.2~ce~3-0~ubuntu

# Install kubeadm

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 -

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update

sudo apt-get install -y kubelet=1.13.12-00 kubeadm=1.13.12-00 kubectl=1.13.12-00

sudo apt-mark hold kubelet kubeadm kubectl

SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.provision "shell", inline: $script
  #config.ssh.private_key_path = '~/.ssh/id_rsa'
  #config.ssh.password = "123456"
  config.vm.network "public_network", type: "dhcp", bridge: "wlp2s0"
  config.vm.provider :virtualbox do |v|
    v.customize ["modifyvm", :id, "--memory", 1024]
    v.customize ["modifyvm", :id, "--cpus", 2]
  end
  
  (1..N_NODES).each do |i|
    config.vm.define "node#{i}" do |subconfig|
      subconfig.vm.hostname = "node#{i}"
      #subconfig.vm.network :private_network, ip: "10.0.2.#{10 + i}"
      #subconfig.vm.network "forwarded_port", guest: 22, host: "#{2220 + i}", host_ip: "127.0.0.1", auto_correct: true
      #subconfig.vm.network "forwarded_port", guest: 22, host: "#{2230 + i}", host_ip: "192.168.0.102", auto_correct: true
    end
  end
  
end

```

{% endcode %}

{% hint style="warning" %}
When I tried permanently disabling swap, the VMs did not boot anymore. So, run manually run `swapoff -a` before starting kubelet.
{% endhint %}

## Kubeadm init

```ruby
# Get all IPs
EXTRA=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | paste -s -d ",")

kubeadm init \
  --service-cidr=10.10.0.0/24 \
  --pod-network-cidr=10.20.0.0/24 \
  --apiserver-advertise-address=PUT.YOUR.VM.IP.HERE \
  --apiserver-cert-extra-sans="$EXTRA"
  
# Deploy CNI
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=10.20.0.0/24"

```

## Join Node

Create Kubelet extra args.

{% code title="/etc/default/kubelet" %}

```ruby
KUBELET_EXTRA_ARGS=--node-ip=PUT.YOUR.NODE.IP.HERE
```

{% endcode %}

Run the join command using your master node IP.

## References

<https://github.com/weaveworks/weave/issues/3363#issuecomment-409622225>

## Troubleshoot

### \[kube-peers] Could not get peers: Get <https://10.96.0.1:443/api/v1/nodes>: dial tcp 10.96.0.1:443: i/o timeout

After deploying Weave, your worker node might be "NotReady" and the docker container k8s\_weave\_weave-net might log the error above.

To fix it, create a new route on your node:

```ruby
ip route add 10.96.0.1/32 dev eth1 src 192.168.0.126
```

Where 192.168.0.126 is the IP of your Node.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.devops.buzz/public/kubeadm/setup-cluster-1/vagrant-+-virtualbox.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
