# Setup master and worker node VM's

## Setup VPS and install docker (all nodes)

Refer: [https://projects-srv2.kondgekar.com/projects/cbofferwall/wiki/0000-preliminary-setup](https://projects-srv2.kondgekar.com/projects/cbofferwall/wiki/0000-preliminary-setup)

## Fix swap (all nodes)

Installing kubernetes needs swap should be disabled. Check if swap is enabled and then disable if swap is enabled.

Disable swap

```shell
sudo swapoff -a
```

Remove / comment out respective swap entry from fstab file

```shell
sudo nano /etc/fstab
```

[![image-1621422218303.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/mO69xr1PlpzHdlsX-image-1621422218303.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/mO69xr1PlpzHdlsX-image-1621422218303.png)

## Install kubeadm, Kubelet And Kubectl (all nodes)

Refer: [https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/)

```shell
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
```

```shell
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
```

```shell
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
```

```shell
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
```

## Update Kubernetes Configuration (all nodes)

```shell
sudo nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
```

This will open a text editor, enter the following line after the last “Environment Variable”:

```shell
Environment=”cgroup-driver=systemd/cgroup-driver=cgroupfs”
```

[![image-1621422742852.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/upEArvctUCMdfAlE-image-1621422742852.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/upEArvctUCMdfAlE-image-1621422742852.png)

## Start kubernetes cluster (on master)

We are going to use [Flannel](https://github.com/flannel-io/flannel) as a networking for pods

```shell
sudo kubeadm init --apiserver-advertise-address=<ip-address-of-kmaster-vm> --pod-network-cidr=10.244.0.0/16
```

1. You will get the below output. The commands marked as (1), execute them as a non-root user. This will enable you to use kubectl from the CLI
2. The command marked as (2) should also be saved for future. This will be used to join nodes to your cluster

[![image-1621423604564.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/FNlPooLqPNhEDBY5-image-1621423604564.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/FNlPooLqPNhEDBY5-image-1621423604564.png)

Execute commands as mentioned above.

```shell
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
```

## Verify if cluster is running (on master)

```shell
kubectl get pods -o wide --all-namespaces
```

[![image-1621424004202.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/FW47jgOhKYFRYXfQ-image-1621424004202.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/FW47jgOhKYFRYXfQ-image-1621424004202.png)

Notice that all pods are running except coredns. It will be running once we setup pod network in the next step

## Install POD network (Flannel) (on master)

```shell
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
```

[![image-1621424554088.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/gQA1X11iriCE203h-image-1621424554088.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/gQA1X11iriCE203h-image-1621424554088.png)

```shell
kubectl get pods -o wide --all-namespaces
```

[![image-1621424641282.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/WKpJ8yFtyEgDnnfq-image-1621424641282.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/WKpJ8yFtyEgDnnfq-image-1621424641282.png)

Notice that all pods are now running

## Install Kubernetes Dashboard (on master)

Refer: [https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/](https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/)

## Check available nodes (on master)

```shell
kubectl get nodes
```

[![image-1621425332028.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/MKPOKdNPG38IYDVD-image-1621425332028.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/MKPOKdNPG38IYDVD-image-1621425332028.png)

Notice that only one node is available which is a master node.

## Add worker node (on worker node)

Use below command to join Kuberneter cluster from worker node

```shell
kubeadm join <master-node-ip-address>:6443 --token <generated-token> \
    --discovery-token-ca-cert-hash <generated-hash>
```

[![image-1621425842025.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/glVFQ2vpeVOml3iS-image-1621425842025.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/glVFQ2vpeVOml3iS-image-1621425842025.png)

## Check if node is added in cluster (on master)

Initially worker node will show status as not ready

[![image-1621425912746.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/Cig5VWG6dDPwfecP-image-1621425912746.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/Cig5VWG6dDPwfecP-image-1621425912746.png)

Wait for some time and it will be shown as Ready

[![image-1621425989576.png](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/scaled-1680-/paBijGeZINEbKUZR-image-1621425989576.png)](https://book-stack.rapidoreach.com/uploads/images/gallery/2021-05/paBijGeZINEbKUZR-image-1621425989576.png)

> Kubernetes cluster is now running. You can now run containerized applications and make it available over web using specific setup