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
# 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 --clientCreating a cluster with kind
A kind configuration file describes the nodes you want. This one asks for one control-plane node and two workers:
# kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: demo
nodes:
- role: control-plane
- role: worker
- role: workerkind create cluster --config kind-config.yaml
kubectl get nodesSample output:
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.0Delete 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:
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
minikube start --nodes 2 --cpus 2 --memory 4g
minikube addons enable ingress
minikube dashboard # opens the web UIkubeconfig 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.
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}kubectl config get-contexts
kubectl config use-context minikubeThis model resolves a context the way kubectl does: find the context, then its cluster and namespace (defaulting to default):
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 likekubectx/kubensand a shell prompt that shows the context help.
Test yourself
How does kind run Kubernetes nodes?
- As separate VMs
- As Docker containers
- As browser tabs
- On a cloud provider
In a kubeconfig, what is a context?
- A running Pod
- A combination of cluster, user and default namespace
- A Docker image
- A YAML template
Which command shows which cluster kubectl currently talks to?
- kubectl whoami
- kubectl config current-context
- kind status
- kubectl cluster delete