Kubernetes Cluster Setup on Your Own Vps

Kubernetes Cluster Setup and Configuration Guide

Disable Swap

1
2
3
4
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
sudo sed -i '/ swap / s/^/#/' /etc/fstab
sudo swapon --show

Set Hostname

1
2
sudo hostnamectl set-hostname "ngt-master"
exec bash

Edit Host File

1
2
3
4
publicIP=$(curl -s http://checkip.amazonaws.com)
echo "$publicIP ngt-master" | sudo tee -a /etc/hosts > /dev/null
echo "Added $publicIP ngt-master to /etc/hosts"
ping -c 4 ngt-master

Set Up the IPV4 Bridge on All Nodes

1
2
3
4
5
6
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter

Apply Sysctl Params Required by Setup

1
2
3
4
5
6
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
sudo sysctl --system

Install Main Components

1
2
3
4
5
6
7
8
9
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo chmod 644 /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
sudo apt install -y kubelet kubeadm kubectl

Install Docker

1
2
3
4
5
6
7
sudo apt install docker.io
sudo mkdir /etc/containerd
sudo sh -c "containerd config default > /etc/containerd/config.toml"
sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
sudo systemctl restart containerd.service
sudo systemctl restart kubelet.service
sudo systemctl enable kubelet.service

Initialize the Kubernetes Cluster on the Master Node

1
2
3
4
5
sudo kubeadm config images pull
sudo kubeadm init --pod-network-cidr=10.10.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Configure kubectl and Calico

1
2
3
4
5
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/tigera-operator.yaml
curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/custom-resources.yaml -O
sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10.10.0.0\/16/g' custom-resources.yaml
kubectl create -f custom-resources.yaml
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

Install and Configure a Network File System (NFS)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sudo apt update
sudo apt -y install nfs-kernel-server
sudo systemctl status nfs-server
sudo mkdir /exports
sudo mkdir /exports/data
sudo mkdir /exports/nginx
sudo vi /etc/exports
/exports/data <IPADDRESS>(rw,sync,no_subtree_check,no_root_squash)
/exports/nginx <IPADDRESS>(rw,sync,no_subtree_check,no_root_squash)
sudo exportfs -ar
sudo exportfs -v

Auto NFS

1
2
3
4
helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
  --set nfs.server=<nfs-server-ip> \
  --set nfs.path=/exported/path

Define a Storage Class for NFS

1
2
3
4
5
6
7
8
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client
reclaimPolicy: Delete
mountOptions:
  - hard
  - nfsvers=4.1

Troubleshooting

If you face system disk issues, you may refer to this post by Praveen Kumar Muppala, and the configuration should look like this image:

alt text