TExcelTAD
Home › Kubernetes › Local Clusters

Local Clusters

You don't need a cloud account to learn Kubernetes. Several tools run a complete, conformant cluster on your laptop. Everything in this course works on any of them.

Tool How it runs nodes Good for
kind (Kubernetes IN Docker) Each node is a Docker container Fast, disposable, multi-node clusters; CI pipelines
minikube A VM or a Docker container, with many add-ons Beginners; built-in dashboard, ingress, metrics add-ons
k3d / k3s k3s is a lightweight distribution; k3d runs it in Docker Low-memory machines, edge devices
Docker Desktop / Rancher Desktop A built-in single-node cluster One checkbox to enable
kubeadm Real VMs or servers Learning how production clusters are bootstrapped

Installing kubectl and kind

bash
# macOS (Homebrew)
brew install kubectl kind

# Linux: download the binaries
curl -LO "https://dl.k8s.io/release/v1.34.0/bin/linux/amd64/kubectl"
chmod +x kubectl && sudo mv kubectl /usr/local/bin/
curl -Lo kind https://kind.sigs.k8s.io/dl/v0.30.0/kind-linux-amd64
chmod +x kind && sudo mv kind /usr/local/bin/

kubectl version --client

Creating a cluster with kind

A kind configuration file describes the nodes you want. This one asks for one control-plane node and two workers:

yaml
# kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: demo
nodes:
  - role: control-plane
  - role: worker
  - role: worker
bash
kind create cluster --config kind-config.yaml
kubectl get nodes

Sample output:

text
Creating cluster "demo" ...
 ✓ Ensuring node image (kindest/node:v1.34.0) 🖼
 ✓ Preparing nodes 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-demo"
NAME                 STATUS   ROLES           AGE   VERSION
demo-control-plane   Ready    control-plane   52s   v1.34.0
demo-worker          Ready    <none>          31s   v1.34.0
demo-worker2         Ready    <none>          31s   v1.34.0

Delete it again with kind delete cluster --name demo. Clusters are cheap: create a fresh one whenever an experiment goes wrong.

We can read the same config file with Python and summarise it, just as a tool would:

python · try it yourself
import yaml

config = yaml.safe_load("""
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: demo
nodes:
  - role: control-plane
  - role: worker
  - role: worker
""")

roles = [n["role"] for n in config["nodes"]]
print("cluster:", config["name"])
print("control-plane nodes:", roles.count("control-plane"))
print("workers:", roles.count("worker"))
names = []
for i, role in enumerate(roles):
    base = f"{config['name']}-{role}"
    same_before = roles[:i].count(role)
    names.append(base if same_before == 0 else f"{base}{same_before + 1}")
print("node names:", names)
cluster: demo
control-plane nodes: 1
workers: 2
node names: ['demo-control-plane', 'demo-worker', 'demo-worker2']

minikube in two commands

bash
minikube start --nodes 2 --cpus 2 --memory 4g
minikube addons enable ingress
minikube dashboard        # opens the web UI

kubeconfig and contexts

kubectl finds clusters through a kubeconfig file, by default ~/.kube/config. It lists clusters (API server address + CA certificate), users (credentials) and contexts (a cluster + user + default namespace). kind and minikube add an entry for you and switch to it.

yaml
apiVersion: v1
kind: Config
current-context: kind-demo
clusters:
  - name: kind-demo
    cluster:
      server: https://127.0.0.1:40213
  - name: minikube
    cluster:
      server: https://192.168.49.2:8443
contexts:
  - name: kind-demo
    context: {cluster: kind-demo, user: kind-demo}
  - name: minikube
    context: {cluster: minikube, user: minikube, namespace: dev}
users:
  - name: kind-demo
    user: {client-certificate-data: "..."}
  - name: minikube
    user: {client-certificate: /home/me/.minikube/profiles/minikube/client.crt}
bash
kubectl config get-contexts
kubectl config use-context minikube

This model resolves a context the way kubectl does: find the context, then its cluster and namespace (defaulting to default):

python · try it yourself
import yaml

kubeconfig = yaml.safe_load("""
current-context: kind-demo
clusters:
  - name: kind-demo
    cluster: {server: "https://127.0.0.1:40213"}
  - name: minikube
    cluster: {server: "https://192.168.49.2:8443"}
contexts:
  - name: kind-demo
    context: {cluster: kind-demo, user: kind-demo}
  - name: minikube
    context: {cluster: minikube, user: minikube, namespace: dev}
""")

def resolve(cfg, context_name=None):
    name = context_name or cfg["current-context"]
    ctx = next(c["context"] for c in cfg["contexts"] if c["name"] == name)
    cluster = next(c["cluster"] for c in cfg["clusters"] if c["name"] == ctx["cluster"])
    return name, cluster["server"], ctx.get("namespace", "default")

print(resolve(kubeconfig))
print(resolve(kubeconfig, "minikube"))
('kind-demo', 'https://127.0.0.1:40213', 'default')
('minikube', 'https://192.168.49.2:8443', 'dev')

Warning: Before running anything destructive, check where you are with kubectl config current-context. Deleting things in the wrong context is a classic mistake. Tools like kubectx/kubens and a shell prompt that shows the context help.

Test yourself

How does kind run Kubernetes nodes?

  1. As separate VMs
  2. As Docker containers
  3. As browser tabs
  4. On a cloud provider

In a kubeconfig, what is a context?

  1. A running Pod
  2. A combination of cluster, user and default namespace
  3. A Docker image
  4. A YAML template

Which command shows which cluster kubectl currently talks to?

  1. kubectl whoami
  2. kubectl config current-context
  3. kind status
  4. kubectl cluster delete