Containers and Kubernetes are where most later labs run, and both can run entirely on your laptop at no cost. A container engine (Docker or Podman) runs containers; kind ("Kubernetes in Docker") runs a whole Kubernetes cluster inside containers; kubectl talks to it. This lesson sets all three up, runs a first container with limits, creates a named cluster, deploys to it, and deletes it again. It also shows the two mistakes that matter most: sending a command to the wrong cluster because of the current context, and a second cluster that will not start.
Difficulty: Beginner · Know this first: Lesson 06, Lesson F25
By the end of this lesson you can work at four levels:
This page teaches it with a plain-language analogy, an animated diagram with normal and failure scenarios, a simulator, a quiz, basic, intermediate, advanced, and master checks, the expert detail ("Go deeper"), a production incident, a hands-on lab with commands and their output, and a production design exercise.
This bar adds up minutes from a working shell to a deployment on a local cluster, cleaned up. It is a worked example, not a measurement; downloads depend on your connection. The create and context hops decide whether the cluster starts and whether commands reach the right one.
Pick how you target the cluster, whether another cluster is running, and the host's inotify limit.
Three questions. No score is saved anywhere — this is just you against the material.
Local containers and clusters grow in four levels: running containers, running a cluster, working safely with several clusters, and making local environments disposable.
At the basic level you can run and remove containers.
Where it breaks: unnamed containers piling up, and :latest changing under you.
Try it: Basic
docker run says "Cannot connect to the Docker daemon". What is wrong?
Now do it: Stop and start the engine and watch docker version.
At the intermediate level you have a cluster on your laptop.
Try it: Intermediate
Where does kind put the cluster's credentials?
Now do it: List your contexts.
At the advanced level you never act on the wrong cluster.
Try it: Advanced
What is the safest place for production cluster credentials?
Now do it: Remove contexts you do not need.
At the master level clusters are created from code and thrown away.
Trade-off: a local cluster is free and fast to rebuild and is not production: one node, no cloud load balancers, different storage. Test those differences somewhere real before you rely on them.
Try it: Master
Your CI uses kind for integration tests. What does it not tell you about production?
Now do it: List them for one service.
Installing pinned kind and kubectl on Linux x86-64 (replace the versions with current ones from the projects' release pages; use arm64 for ARM machines):
KIND=v0.29.0; KUBECTL=v1.33.1 curl -fsSLo kind "https://kind.sigs.k8s.io/dl/$KIND/kind-linux-amd64" curl -fsSLo kubectl "https://dl.k8s.io/release/$KUBECTL/bin/linux/amd64/kubectl" chmod +x kind kubectl && sudo mv kind kubectl /usr/local/bin/ kind version && kubectl version --client
kind can pin the Kubernetes version of the cluster too, with --image kindest/node:vX.Y.Z; each kind release lists the node images it was tested with. A cluster config file adds worker nodes and port mappings:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30080 # a NodePort service inside the cluster
hostPort: 8080 # reachable as localhost:8080
- role: worker
Known issue, for several clusters on one host: sudo sysctl -w fs.inotify.max_user_instances=512 and fs.inotify.max_user_watches=524288; put them in /etc/sysctl.d/ to keep them after a restart. Minikube and k3d are alternatives to kind with the same idea.
This is a composite of a failure pattern that keeps repeating; the details are illustrative. The practice cleanup that was not.
Shared-cluster credentials are issued per session and expire; tutorials in the team guide use --context everywhere; deleting a namespace in the shared cluster needs a separate role; and shell prompts show the current context.
Everything here runs on any Linux machine: your own laptop, WSL on Windows (run wsl --install in PowerShell once), or a small cloud VM. The Python needs only Python 3 and its standard library; check that you have it with python3 --version.
Before you start: Docker (Engine on Linux and WSL, or Docker Desktop) or Podman, plus kind and kubectl (install commands are in "Go deeper"), about 4 GB of free memory, and Python 3. The lab creates its own cluster named onboarding-lab and deletes it at the end; other clusters are not touched.
Type each one, read what it prints, and compare it with the note above it.
Check that both the Docker client and the engine answer, then run the smallest possible container.
docker version --format 'client {{.Client.Version}}, server {{.Server.Version}}'
docker run --rm hello-world | sed -n 2pRun nginx in the background with a port, a memory limit, and a CPU limit; check it, then remove it by name.
docker run -d --name onboarding-web -p 8089:80 --memory 128m --cpus 0.5 nginx:1.27-alpine
sleep 2; curl -sI http://localhost:8089 | head -1
docker inspect onboarding-web --format 'memory={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}'
docker rm -f onboarding-webCreate a named local cluster. kind adds a context called kind-onboarding-lab and switches to it; note your previous context first so you can switch back.
kubectl config current-context 2>/dev/null || echo "(no current context)" kind create cluster --name onboarding-lab --wait 180s kind get clusters
Deploy two nginx pods, naming the context on every command so nothing can reach another cluster.
Changes a real system: On any machine with more than one cluster, name the context on every command that changes something; the current context is just whatever was used last.
CTX=kind-onboarding-lab kubectl --context $CTX get nodes kubectl --context $CTX create deployment hello --image=nginx:1.27-alpine --replicas=2 kubectl --context $CTX rollout status deployment/hello --timeout=120s kubectl --context $CTX get pods -l app=hello
Delete the cluster and check what is left.
kind delete cluster --name onboarding-lab kind get clusters kubectl config current-context 2>/dev/null || echo "(no current context: switch back with kubectl config use-context NAME)" docker system df
context_guard.pyThis program is a guard that a script can run before any kubectl command that changes something: it reads a kubeconfig, finds the current context, and refuses unless the context is the one the script expected and is not marked as production, the check that would have stopped the wrong-cluster accident in this lesson. Save it as context_guard.py, then run it.
# context_guard.py
KUBECONFIG = {
"current-context": "team-dev-shared",
"contexts": [
{"name": "kind-onboarding-lab", "cluster": "kind-onboarding-lab"},
{"name": "team-dev-shared", "cluster": "dev-eu-1"},
{"name": "prod-eu-1", "cluster": "prod-eu-1"},
],
}
PROTECTED = ("prod",) # name fragments that must never be the target
def guard(expected, action, context=None):
target = context or KUBECONFIG["current-context"]
names = [c["name"] for c in KUBECONFIG["contexts"]]
if target not in names:
return f"REFUSE {action}: context {target} does not exist"
if any(p in target for p in PROTECTED):
return f"REFUSE {action}: {target} is a protected context"
if target != expected:
return f"REFUSE {action}: would run against {target}, expected {expected}; pass --context {expected}"
return f"ok {action} on {target}"
print(guard("kind-onboarding-lab", "kubectl delete namespace demo"))
print(guard("kind-onboarding-lab", "kubectl delete namespace demo", context="kind-onboarding-lab"))
print(guard("prod-eu-1", "kubectl scale deploy/api --replicas=0", context="prod-eu-1"))
print(guard("kind-onboarding-lab", "kubectl apply -f app.yaml", context="kind-onboardinglab"))
Run it, and you should see this output:
$ python3 context_guard.py REFUSE kubectl delete namespace demo: would run against team-dev-shared, expected kind-onboarding-lab; pass --context kind-onboarding-lab ok kubectl delete namespace demo on kind-onboarding-lab REFUSE kubectl scale deploy/api --replicas=0: prod-eu-1 is a protected context REFUSE kubectl apply -f app.yaml: context kind-onboardinglab does not exist
The same Linux steps, made into a real script you can keep and run again. It uses the Bash features a production script needs: functions (one per step), an array of step names, arguments to choose steps, a case for options, exit codes recorded for each step, and a trap that prints a summary even when you stop it with Ctrl+C. Make a folder for this lesson (mkdir -p ~/labs/containers-and-local-kubernetes-docker-podman-kind-and-kubectl && cd ~/labs/containers-and-local-kubernetes-docker-podman-kind-and-kubectl) and save this as lab.sh.
#!/usr/bin/env bash
# The Linux steps of "Containers and a Local Kubernetes Cluster: Docker or Podman, kind, and kubectl", as one script (save it as lab.sh).
#
# ./lab.sh run every step, in order
# ./lab.sh 2 4 run only steps 2 and 4
# ./lab.sh --list list the steps
#
# The steps share one shell, so a later step can use what an earlier one set up (a folder,
# a variable, a container): run them all in order the first time. Your own scripts should
# start with "set -euo pipefail"; this one runs teaching commands, and some of them fail on
# purpose, so it records each step's exit code instead of stopping at the first failure.
STEPS=(
'Check that both the Docker client and the engine answer, then run the smallest possible container.'
'Run nginx in the background with a port, a memory limit, and a CPU limit; check it, then remove it by name.'
'Create a named local cluster. kind adds a context called kind-onboarding-lab and switches to it; note your previous context first so you can switch back.'
'Deploy two nginx pods, naming the context on every command so nothing can reach another cluster.'
'Delete the cluster and check what is left.'
)
step_1() {
docker version --format 'client {{.Client.Version}}, server {{.Server.Version}}'
docker run --rm hello-world | sed -n 2p
}
step_2() {
docker run -d --name onboarding-web -p 8089:80 --memory 128m --cpus 0.5 nginx:1.27-alpine
sleep 2; curl -sI http://localhost:8089 | head -1
docker inspect onboarding-web --format 'memory={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}'
docker rm -f onboarding-web
}
step_3() {
kubectl config current-context 2>/dev/null || echo "(no current context)"
kind create cluster --name onboarding-lab --wait 180s
kind get clusters
}
step_4() {
CTX=kind-onboarding-lab
kubectl --context $CTX get nodes
kubectl --context $CTX create deployment hello --image=nginx:1.27-alpine --replicas=2
kubectl --context $CTX rollout status deployment/hello --timeout=120s
kubectl --context $CTX get pods -l app=hello
}
step_5() {
kind delete cluster --name onboarding-lab
kind get clusters
kubectl config current-context 2>/dev/null || echo "(no current context: switch back with kubectl config use-context NAME)"
docker system df
}
# print a step that has to be run by hand, instead of running it
by_hand() {
echo " (run this one yourself: $1)"
sed 's/^/ /'
}
RESULTS=()
run_step() {
local n=$1 start=$SECONDS code
printf '\n== step %d of %d: %s\n' "$n" "${#STEPS[@]}" "${STEPS[n-1]}"
"step_$n"
code=$?
RESULTS+=("step $n: exit code $code, $((SECONDS - start)) s")
}
summary() {
echo
echo "== summary"
local r
for r in "${RESULTS[@]}"; do echo " $r"; done
}
case "${1:-}" in
-h|--help) sed -n '2,6p' "$0"; exit 0 ;;
--list) for i in "${!STEPS[@]}"; do echo "$((i + 1)). ${STEPS[i]}"; done; exit 0 ;;
esac
[ $# -eq 0 ] && set -- $(seq 1 "${#STEPS[@]}")
for n in "$@"; do
if ! [[ $n =~ ^[0-9]+$ ]] || (( n < 1 || n > ${#STEPS[@]} )); then
echo "there is no step '$n'; try --list" >&2
exit 2
fi
done
trap summary EXIT
trap 'echo; echo "stopped with Ctrl+C"; exit 130' INT
for n in "$@"; do run_step "$n"; done
Then make it executable and run it:
chmod +x lab.sh ./lab.sh --list # the steps ./lab.sh # every step, then a summary of exit codes and times ./lab.sh 2 3 # only steps 2 and 3
New to a feature used above? These W3Schools pages explain each one from zero, with examples you can run in the browser.
sed, head, curl, df, rm, echo, Pipes, Redirection, If...Else, Variables.Your team wants every engineer to run the platform locally on kind for development and to run the same setup in CI. Design it.
Which engine and versions? How is the cluster defined and created? How are images built and loaded? How do engineers avoid acting on shared clusters by mistake? What host settings are needed? How is it reset?
Say 15 engineers on Windows (WSL), macOS, and Linux, laptops with 16 GB of memory, and 8 services.
Docker Engine in WSL and Linux, Docker Desktop or Podman Desktop on macOS; pinned kind and kubectl installed by a script; a kind config in the repository; a make target that creates the cluster, loads locally built images (kind load docker-image), installs add-ons, and verifies them; --context everywhere in scripts; a separate KUBECONFIG for shared clusters with short-lived credentials; documented inotify settings; make reset deletes the cluster by name.
A full local platform needs a lot of memory; trimming it to the services a person works on is faster but tests less.
Each row is one way this system went wrong in the diagram's scenarios. The hop number is where to look first.
| Scenario | Where | Effect | What goes wrong |
|---|---|---|---|
| Wrong context | Hop 7: Context | Breaks | The cluster create failed earlier and was retried later, so the current context was still the team's shared development cluster, added yesterday for a demo. The lab commands do not name a context. |
| Wrong context | Hop 8: Deploy | Breaks | The deployment lands in the shared cluster, and the lab's cleanup, "kubectl delete namespace demo", deletes the team's demo namespace there, with its database and secrets. Restoring it from manifests and a backup takes 45 minutes. |
| Second cluster will not start | Hop 6: Create cluster | Breaks | kind stops at "Starting control-plane": kubeadm init fails because the API server never answers. With another cluster running, the host has used up its 128 inotify instances. |
The official documentation and standards behind this lesson. Versions and defaults change; when a command or field here differs from them, trust the official page.
Last updated . Next review due by November 2027. Found something out of date? Tell me.