Local Kubernetes Cluster with Multipass
Overview
Guide Information
Difficulty: Intermediate
Time Required: ~30 minutes
Last Updated: March 2024
Table of Contents
- Setup Multipass
- Provision Virtual Machines
- Server Preparation
- Configure System Settings
- Install Kubernetes Components
- Initialize Cluster
- Setup Network Interface
- Join Worker Nodes
- Verify Cluster
- Troubleshooting
- Cluster Maintenance
Architecture
graph TD
A[Host Ubuntu 24.04] --> B[Multipass]
B --> C[Master Node<br/>4GB RAM, 2 CPU]
B --> D[Worker1<br/>4GB RAM, 2 CPU]
B --> E[Worker2<br/>4GB RAM, 2 CPU]
C --> F[Control Plane]
F --> G[API Server]
F --> H[etcd]
F --> I[Controller Manager]
F --> J[Scheduler]
D --> K[kubelet]
D --> L[containerd]
E --> M[kubelet]
E --> N[containerd]
Setup Multipass
Quick Setup
Multipass provides a fast way to spin up Ubuntu VMs. It's lightweight and perfect for local Kubernetes clusters.
sudo snap install multipass
multipass version
multipass find
Provision Virtual Machines
Resource Allocation
We'll create one master node and two worker nodes. Adjust the resources based on your system capabilities.
# Create master node
multipass launch --name master --cpus 2 --mem 4G --disk 20G
# Create worker nodes
multipass launch --name worker1 --cpus 2 --mem 4G --disk 20G
multipass launch --name worker2 --cpus 2 --mem 4G --disk 20G
Expected Output
Launched: master
Launched: worker1
Launched: worker2
Access Nodes
multipass shell master
multipass shell worker1
multipass shell worker2
Get Node IPs
multipass list
Server Preparation
Important
Run these commands on ALL nodes (master and workers).
sudo apt update && sudo apt upgrade -y
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Containerd
Install and Configure Containerd | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Configure System Settings
Critical Step
Skipping these configurations may result in cluster initialization failures.
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
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 Kubernetes Components
Version Information
This guide uses Kubernetes v1.32. Adjust version numbers as needed.
# Add Kubernetes repository
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key | sudo gpg --dearmor -o /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.32/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install required packages
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
# Prevent accidental upgrades
sudo apt-mark hold kubelet kubeadm kubectl
# Enable kubelet
sudo systemctl enable kubelet
sudo systemctl start kubelet
Initialize Cluster
Master Node Only
Run these commands ONLY on the master node.
# Initialize cluster
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# Setup kubeconfig
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Save the Join Command
The initialization will output a kubeadm join
command. Save this for joining worker nodes.
Setup Network Interface
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl get pods -n kube-flannel
Join Worker Nodes
Worker Nodes Only
Run these commands on each worker node.
kubeadm token create --print-join-command
sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Verify Cluster
kubectl get nodes
kubectl get pods -A
Troubleshooting
Common Issues
- Check CNI pods:
kubectl get pods -n kube-system
- Check kubelet status:
systemctl status kubelet
- View kubelet logs:
journalctl -xeu kubelet
- Generate new token:
kubeadm token create
- Get discovery token CA cert hash:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | \ openssl rsa -pubin -outform der 2>/dev/null | \ openssl dgst -sha256 -hex | sed 's/^.* //'
- Check flannel pods:
kubectl get pods -n kube-flannel
- Check flannel logs:
kubectl logs -n kube-flannel <pod-name>
Cluster Maintenance
Backup Procedures
Backup etcd
sudo apt install etcd-client
ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
snapshot save snapshot.db
Scaling the Cluster
To add more worker nodes:
- Create new VM using multipass
- Follow server preparation steps
- Join the cluster using the join command
Cleanup
# On master node
kubectl drain <node-name> --ignore-daemonsets
kubectl delete node <node-name>
# On worker node
sudo kubeadm reset
multipass delete <vm-name>
multipass purge
Security Best Practices
- Keep Kubernetes version updated
- Use Network Policies
- Enable RBAC
- Regularly rotate certificates
- Monitor cluster with security tools
Next Steps
- Deploy sample applications
- Setup monitoring with Prometheus and Grafana
- Configure persistent storage
- Implement high availability
Need Help?
If you encounter any issues, check the official Kubernetes documentation or open an issue in the repository.