2. Kubernetes Cluster Installation and Configuration
2.1 Pre-configuration on all nodes
Run these commands on all nodes (master and workers):
# Disable swap
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
# Configure required kernel modules
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# Configure network parameters
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
2.2 Install K3s on the master node
On the master node (k3s-master), run:
# Install K3s as server (master)
curl -sfL https://get.k3s.io | sh -s - --disable=traefik
# Verify installation
sudo kubectl get nodes
# Generate token to add worker nodes
TOKEN=$(sudo cat /var/lib/rancher/k3s/server/node-token)
echo "Node token: $TOKEN"
Save the generated token, you will need it for the worker nodes.
2.3 Install K3s on worker nodes
On each worker node (k3s-worker1 and k3s-worker2), run:
# Replace IP_MASTER and TOKEN with correct values
curl -sfL https://get.k3s.io | K3S_URL=https://192.168.1.127:6443 K3S_TOKEN=THETOKENYOUSAVED sh -
2.4 Verify cluster installation
On the master node, run:
sudo kubectl get nodes --kubeconfig /etc/rancher/k3s/k3s.yaml
# You should see something like:
# NAME STATUS ROLES AGE VERSION
# k3s-master Ready control-plane,master 5m32s v1.27.x+k3s1
# k3s-worker1 Ready worker or none 2m8s v1.27.x+k3s1
# k3s-worker2 Ready worker or none 1m15s v1.27.x+k3s1
2.5 Configure remote access to the cluster
To access the cluster from your Windows machine, you need the kubeconfig file:
# On the master node:
sudo cat /etc/rancher/k3s/k3s.yaml
Copy the content of this file and save it on your Windows machine as C:\Users\<your-user>\.kube\config.
Important: Modify the line server: https://127.0.0.1:6443 to use the actual IP of the master node: server: https://192.168.1.127:6443.
Alternatively, to facilitate usage of kubectl and helm on the master node, you can create a copy of the kubeconfig file for your user:
# On the master node
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $USER:$USER ~/.kube/config
chmod 600 ~/.kube/config