Your first cluster, hands-on
Reading about Pods and Services only goes so far. In this chapter you build a real Kubernetes cluster on your own laptop, deploy an app to it, reach it, scale it, update it, break it on purpose and clean up. Everything runs inside Docker, so it costs nothing and leaves nothing behind.
kind or minikube?
Both give you a working cluster on one machine. kind ("Kubernetes in Docker") runs each Kubernetes node as a Docker container: it starts in under a minute, is very light, and is what many CI pipelines use to test against a real cluster. minikube runs a single node in a container or VM and comes with handy add-ons (dashboard, ingress, metrics-server). Either is fine for learning; this chapter uses kind, and the minikube equivalents are at the end. You need Docker running (Linux chapter 11 sets it up).
Install kubectl and kind
On a Mac, brew install kubectl kind does both. On Linux or WSL (Intel/AMD machines), download the binaries:
# kubectl: the client you'll use for every cluster, local or cloud
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
$ sudo install -m 0755 kubectl /usr/local/bin/kubectl
# kind: check kind.sigs.k8s.io for the newest version number
$ KIND_VERSION=v0.24.0
$ curl -Lo ./kind https://kind.sigs.k8s.io/dl/${KIND_VERSION}/kind-linux-amd64
$ chmod +x ./kind && sudo mv ./kind /usr/local/bin/kind
$ kubectl version --client
$ kind version
Create the cluster
$ kind create cluster --name dev
Creating cluster "dev" ...
✓ Ensuring node image
✓ Preparing nodes
✓ Starting control-plane
Set kubectl context to "kind-dev"
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
dev-control-plane Ready control-plane 45s v1.31.0 # yours may be newer
kind also wrote the connection details into ~/.kube/config, the kubeconfig file every kubectl command reads. That's why kubectl "just knows" where the cluster is.
Contexts and namespaces
A context in the kubeconfig is a saved combination of a cluster, a user and a default namespace. When you also work with a cloud cluster, switching context is how you choose which cluster a command hits, so always check before running anything destructive.
$ kubectl config get-contexts # all saved contexts, * marks the current one
$ kubectl config current-context
kind-dev
$ kubectl config use-context kind-dev
A namespace is a folder inside a cluster. It groups related objects, lets two teams use the same names without clashing, and is the unit that access rules and resource quotas apply to. Every cluster starts with default, kube-system (Kubernetes' own parts) and a few others.
$ kubectl create namespace shop
$ kubectl config set-context --current --namespace=shop # make it the default
$ kubectl get pods -A # -A: every namespace
Deploy an app
Save this as web.yaml. It's a Deployment that keeps two copies of nginx running, and a Service that gives them one stable address:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: shop
spec:
replicas: 2
selector:
matchLabels: {app: web}
template:
metadata:
labels: {app: web}
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests: {cpu: 50m, memory: 32Mi}
limits: {memory: 128Mi}
---
apiVersion: v1
kind: Service
metadata:
name: web
namespace: shop
spec:
selector: {app: web}
ports:
- port: 80
targetPort: 80
$ kubectl apply -f web.yaml
deployment.apps/web created
service/web created
$ kubectl rollout status deployment/web
deployment "web" successfully rolled out
$ kubectl get deploy,rs,pods -o wide
Reach it
The Service is type ClusterIP, so it's only reachable from inside the cluster. For testing from your laptop, port-forward opens a tunnel through kubectl:
$ kubectl port-forward svc/web 8080:80
Forwarding from 127.0.0.1:8080 -> 80
# in a second terminal:
$ curl -I localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.27.x
Port-forward is for you, not for users. Real traffic comes in through an Ingress or a LoadBalancer Service (Kubernetes chapter 4).
Scale, update, roll back
$ kubectl scale deployment/web --replicas=4
$ kubectl set image deployment/web web=nginx:1.27-alpine # container name = new image
$ kubectl rollout status deployment/web # watch pods being replaced one by one
$ kubectl rollout history deployment/web
$ kubectl rollout undo deployment/web # back to the previous version
In real projects you'd change the image in web.yaml and kubectl apply it again, so the file in Git always matches what's running. The commands above are just the quickest way to see it happen.
Break it on purpose
Point the Deployment at an image that doesn't exist and watch how Kubernetes reports it:
$ kubectl set image deployment/web web=nginx:does-not-exist
$ kubectl get pods
web-7c9d... 0/1 ImagePullBackOff 0 30s
$ kubectl describe pod web-7c9d... # read the Events at the bottom
Failed to pull image "nginx:does-not-exist": ... not found
$ kubectl rollout undo deployment/web # fix it
Notice the old pods kept serving while the broken ones failed: a rolling update only removes old pods when new ones become ready. Now delete a pod by hand with kubectl delete pod <name> and run kubectl get pods again. A new one appears within seconds, because the ReplicaSet always works back to the number you asked for.
Run your own image
The cluster's nodes are containers, so they can't see images you've only built on your laptop. Load them in first:
$ docker build -t myapp:dev .
$ kind load docker-image myapp:dev --name dev
# then use image: myapp:dev in the Deployment
Use a real tag like dev, not latest: with latest, Kubernetes always tries to pull from a registry and fails with ImagePullBackOff.
Clean up
$ kubectl delete namespace shop # deletes everything inside it
$ kind delete cluster --name dev # removes the whole cluster
The same steps on minikube
$ minikube start # instead of kind create cluster
$ minikube image load myapp:dev # instead of kind load docker-image
$ minikube service web -n shop --url # prints a URL that reaches the Service
$ minikube addons enable ingress # a ready-made Ingress controller
$ minikube delete
Common problems
The connection to the server localhost:8080 was refused: kubectl has no cluster to talk to. Checkkubectl config get-contexts, and that Docker (and the kind containers) are running.- Pods stuck in
Pending:kubectl describe podusually saysInsufficient cpuormemory. Give Docker Desktop more resources, or lower the requests. - ImagePullBackOff for your own image: you forgot
kind load, or used thelatesttag. - Changes "don't apply": you're in a different namespace or context than you think.
kubectl config get-contextsshows both.