Setting up Kubernetes cluster in Windows using Ubuntu VMs
Below lists simple steps that can be followed to set up a cluster using ubuntu VMs. You can set it up in any OS. I have windows machine.
1 master+ 2 node deployment
Prerequisites
- Oracle VM Virtual Box : Download from https://www.virtualbox.org/
- Turn on hardware virtualization support in BIOS Settings. (Hyper V in windows 10 )
Create VMs
Create 2 VMs for 1 master with 1 worker node cluster with below requirements
- Version: Ubuntu (64-bit)
- Processor: 2 CPU
- 4 GB RAM
- Network : Select NAT as adapter 1 and Host only adapter as adapter 2
Initial setup on all nodes
- Turn off swap using sudo swapoff -a
Comment the line with swap in /etc/fstab
nano /etc/fstab
- Add IP address of all nodes in etc/hosts with details of ip address and hostname as below.
Note! Above IP addresses are selected by adding a new virtual host adapter as explained in Add new Adapter Interface . It is also possible to use default IP address selected by Virtual Box as below and no need to create a new adapter interface.
3. Install docker
apt-get update && apt-get install -y docker.io
4. Install Kubernetes
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat << EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update && apt-get install -y kubelet kubeadm kubectl
5. Update cgroup-driver
nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
Edit kubeadm.conf file and add below variable after the last Environment
Environment="cgroup-driver=systemd/cgroup-driver=cgroupfs"
On Master Node
1. kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=<IP_Address_of_MasterNode>
eg: kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=172.16.0.3
The above command will generate below comments as part of the output
"
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/
You can now join any number of machines by running the following on each nodeas root:
kubeadm join 172.16.0.3:6443 --token 1yy5o4.qokxr4wu5t58kxe1 --discovery-token-ca-cert-hash sha256:ca16640bf7de68d2e036dbe08e5097f6246379ffd87c0d08ef8f4e1d83f08fcc "
Save this join command and can be used to add any number of nodes to your cluster.
2. Start the cluster as mentioned in the result of step1
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. Join other nodes using the command after logging to worker nodes
kubeadm join 172.16.0.3:6443 --token 1yy5o4.qokxr4wu5t58kxe1 --discovery-token-ca-cert-hash sha256:ca16640bf7de68d2e036dbe08e5097f6246379ffd87c0d08ef8f4e1d83f08fcc
3. Check whether the nodes are running
kubectl get nodes -o wide
Note! The nodes status will be 'NotReady' as CNI is not yet installed. Proceed to step 4 for that.
4. Install CNI plugin. I have used calico. If your node has multiple interfaces make sure your /etc/hosts has the IP address of all nodes updated as mentioned in the category Initial setup on all nodes. Otherwise calico will pickup IP address of the first interface
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
5. Check whether all pods are running
watch kubectl get pods --all-namespaces
Recreate Token
If token has expired and want to join new nodes use below command on master node
kubectl create token
kubeadm list token
Use the token listed using the above command to join the new node