Multinode kubernetes cluster in virtualbox

Submitted on Fri, 08/13/2021 - 09:12

1.Update the package repository (All hosts)
sudo apt-get update

2. Install Docker (All hosts)
sudo apt-get install -y

3. Access Repos via HTTPS (All hosts)
sudo apt-get install apt-transport-https curl -y

4. Add K8S key and Repo (All hosts)
curl -s | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb kubernetes-xenial main

5. Update the package repository and Install K8S components (All hosts):
sudo apt-get update
sudo apt-get install -y kubelet=1.18.1-00 
sudo apt-get install -y kubeadm=1.18.1-00 
sudo apt-get install -y kubectl=1.18.1-00
sudo apt-mark hold kubelet kubeadm kubectl

6. Add the hosts entry (All hosts)
edit the file "/etc/hosts"

7. Disable SWAP (All hosts)
sudo swapoff -a
edit /etc/fstab to remove the swap entry

8. Initiate the Cluster(Only on Master node)

sudo kubeadm config images pull
sudo kubeadm init --control-plane-endpoint kube-master:6443 --pod-network-cidr

9. Set the kubectl context auth to connect to the cluster(Only on Master node)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

In worker nodes,

cp /etc/kubernetes/kubelet.conf $HOME/.kube/config

10. Pod Network Addon(Calico) (Only on Master node)
curl -O
vi calico.yaml

11. Generate Token to add worker Node(Only on Master node)
#Create a new Token
sudo kubeadm token create
#List Tokens created
sudo kubeadm token list
#Find Certificate Hash on Master
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | 
   openssl rsa -pubin -outform der 2>/dev/null | 
   openssl dgst -sha256 -hex | sed 's/^.* //'

12. Join Nodes (Only on Worker nodes)
sudo kubeadm join --token TOKEN_ID CONTROL_PLANE_HOSTNAME:CONTROL_PLANE_PORT --discovery-token-ca-cert-hash sha256:HASH
(Formed using outputs from step 10, treat CAPS as variables to be replaced)

13. Want to run workloads on Master?(Only on Master Node)
kubectl taint nodes --all

14. Sample Deployment file:
apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
    app: nginx
  replicas: 1
      app: nginx
        app: nginx
      - name: nginx
        image: nginx
        - containerPort: 80

15. Apply the deployment:
kubectl apply -f FILE_NAME