Kubernetes for Docker users: pods, deployments and services on a local kind cluster
Run shorty on a local kind cluster: a Deployment, a Service, a migration Job, port-forward, a rolling update and a rollback, explained for people who already know Docker.
The last time I wrote about Kubernetes on this blog was 2019, when I deployed WordPress on GKE behind a load balancer. Quite a lot has changed since then. You don't need a cloud account to learn it any more, a cluster runs inside Docker on your laptop.
In this blog we will run shorty, the URL shortener from this series (the design, the Go API), on a local kind cluster. We will create a Deployment, a Service and a one-off migration Job, reach the app with port-forward, then do a rolling update and a rollback. Level is beginner. You should know Docker already (images, containers, docker run -e, Compose). If not, start with Docker from zero and the Compose stack for shorty, because this post maps everything back to those.
Versions I used: kind v0.33.0, Kubernetes v1.37.0 (the kind default node image), kubectl v1.37.2, Docker on a Mac with Apple silicon, Go 1.27.3.
Installing kind and kubectl
kind means "Kubernetes in Docker". Each Kubernetes node is a Docker container, and inside it runs containerd, the kubelet and the control plane. kind is a Go program, so the simplest install is:
go install sigs.k8s.io/kind@v0.33.0
kind version
kubectl needs to be close to the cluster version (one minor version either side). The kubectl on my Mac was 1.33, four versions behind, so I got the current one from the official release:
curl -LO https://dl.k8s.io/release/v1.37.2/bin/darwin/arm64/kubectl
chmod +x kubectl && sudo mv kubectl /usr/local/bin/
kubectl version --client
Creating the cluster
kind create cluster --name shorty
kubectl get nodes
NAME STATUS ROLES AGE VERSION
shorty-control-plane Ready control-plane 25s v1.37.0
That's a one-node cluster. The first run is slower because it pulls the node image. kind also wrote a context called kind-shorty into your kubeconfig and switched to it, so kubectl talks to the new cluster.
Getting the image into the cluster
This is the first thing that surprises Docker users. Basically, the cluster does not see your local Docker images. The node has its own containerd with its own image store, so shorty:dev on your laptop means nothing to it. You either push to a registry or copy the image in:
cd shorty
docker build -t shorty:dev .
kind load docker-image shorty:dev --name shorty
I tried same for postgres:18-alpine and it failed with ctr: content digest sha256:... not found. That happens when your local image is a multi-platform index but Docker only holds the layers for your own platform. I didn't fight it. Postgres and Redis are public images, so the node can just pull them itself from Docker Hub. Only shorty needs kind load, as it exists nowhere else.
Docker words to Kubernetes words
Before the YAML, a quick map. A pod is one or more containers that share a network namespace, roughly one docker run. You almost never create pods directly. A Deployment says "keep N copies of this pod running" and replaces them when they die, which is like restart: unless-stopped and --scale in Compose rolled together. A Service gives those pods one stable name and IP, like the service name in a Compose network. A Job runs a pod to completion, like a one-shot Compose service.
Postgres and Redis
For this post, Postgres runs as a plain Deployment with an emptyDir volume. That means the data dies with the pod. It's fine for learning and terrible for anything else (I hit exactly that in the next post, by accident).
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:18-alpine
env:
- name: POSTGRES_USER
value: shorty
- name: POSTGRES_PASSWORD
value: shorty
- name: POSTGRES_DB
value: shorty
ports:
- containerPort: 5432
volumeMounts:
- name: data
mountPath: /var/lib/postgresql
volumes:
- name: data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
The selector is the glue. The Deployment owns pods labelled app: postgres, and the Service sends traffic to any ready pod with that label. Nothing refers to pods by name.
redis.yaml is the same shape with redis:8-alpine, the same redis-server --save "" --appendonly no --maxmemory 64mb args as the Compose file, and port 6379. It's in the code folder, I won't repeat it here.
kubectl apply -f postgres.yaml -f redis.yaml
kubectl rollout status deploy/postgres
Running the migrations as a Job
shorty's image has a migrate up subcommand, and the Dockerfile uses CMD with no ENTRYPOINT, so we can override it with args:
apiVersion: batch/v1
kind: Job
metadata:
name: shorty-migrate
spec:
backoffLimit: 4
template:
spec:
restartPolicy: OnFailure
containers:
- name: migrate
image: shorty:dev
args: ["/shorty", "migrate", "up"]
env:
- name: DATABASE_URL
value: postgres://shorty:shorty@postgres:5432/shorty?sslmode=disable
kubectl apply -f migrate-job.yaml
kubectl wait --for=condition=complete job/shorty-migrate --timeout=120s
kubectl logs job/shorty-migrate
OK up 00001_create_links.sql (2.87ms)
Note the hostname postgres in the URL. Cluster DNS resolves Service names, same as Compose does for service names (the Docker networking post covers the Docker side). The full name is postgres.default.svc.cluster.local, but inside the same namespace the short one works.
The shorty Deployment and Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: shorty
labels:
app: shorty
spec:
replicas: 2
selector:
matchLabels:
app: shorty
template:
metadata:
labels:
app: shorty
spec:
containers:
- name: shorty
image: shorty:dev
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
value: postgres://shorty:shorty@postgres:5432/shorty?sslmode=disable
- name: REDIS_URL
value: redis://redis:6379/0
- name: BASE_URL
value: http://localhost:8080
---
apiVersion: v1
kind: Service
metadata:
name: shorty
spec:
selector:
app: shorty
ports:
- port: 80
targetPort: 8080
Yes, the database password is sitting in plain text in the env. The next post moves it into a Secret. One thing at a time.
kubectl apply -f shorty.yaml
kubectl rollout status deploy/shorty
kubectl get deploy,rs,pods,svc
You'll see three layers: the Deployment, a ReplicaSet with a hash in its name (shorty-78f8b554c4), and two pods named after that ReplicaSet. The Deployment creates a new ReplicaSet for every change to the pod template. Keep that in mind for the rollout part.
Reaching the app with port-forward
There's no ports: "8080:8080" here. A ClusterIP Service is only reachable inside the cluster. For local testing, kubectl can tunnel a port:
kubectl port-forward svc/shorty 8080:80
And in another terminal:
curl -s -X POST localhost:8080/links -d '{"url":"https://kind.sigs.k8s.io/","code":"kind"}'
curl -si localhost:8080/kind | head -4
My first try used the code go and got {"error":"shortcode: code must be 3 to 32 characters"}. Fair enough, shorty's own validation. With kind it worked, and the second call returned 302 Found with Location: https://kind.sigs.k8s.io/.
Then I looked at the logs of both pods with kubectl logs -l app=shorty --prefix and every single request had gone to the same pod. port-forward picks one pod behind the Service and sticks to it, it does not load balance. To see the Service really spread traffic I ran a throwaway pod inside the cluster:
kubectl run tmp --rm -i --restart=Never --image=busybox:1.37 -- \
sh -c 'for i in 1 2 3 4 5 6 7 8 9 10; do wget -qO- http://shorty/links/kind >/dev/null; done'
for p in $(kubectl get pods -l app=shorty -o name); do
echo "$p $(kubectl logs $p | grep -c '/links/kind')"
done
That split 6 and 4 between the two pods, good enough for me.
Self-healing and scaling
Delete a pod and ReplicaSet makes a new one straight away. And scaling is one command:
kubectl delete pod shorty-78f8b554c4-qvvl8
kubectl get pods -l app=shorty
kubectl scale deploy/shorty --replicas=3
A rolling update
I built a second image with a different version stamp, loaded it and switched the Deployment to it:
docker build --build-arg VERSION=v2 -t shorty:v2 .
kind load docker-image shorty:v2 --name shorty
kubectl set image deploy/shorty shorty=shorty:v2
kubectl rollout status deploy/shorty
kubectl get rs -l app=shorty
NAME DESIRED CURRENT READY AGE
shorty-6cbff5f5c5 3 3 3 2s
shorty-78f8b554c4 0 0 0 67s
The new ReplicaSet scaled up while the old one scaled down, one pod at a time. The old ReplicaSet stays around at zero, and that's what makes a rollback cheap. One small catch: kubectl logs deploy/shorty right after the rollout printed "version":"dev", because it picked a pod that was still terminating. kubectl logs -l app=shorty showed all three new pods on v2.
A bad rollout and a rollback
Now the useful part. I pointed the Deployment at an image that doesn't exist:
kubectl set image deploy/shorty shorty=shorty:v3
kubectl get pods -l app=shorty
NAME READY STATUS RESTARTS AGE
shorty-6588f597b8-28vn4 0/1 ImagePullBackOff 0 21s
shorty-6cbff5f5c5-6cxbq 1/1 Running 0 28s
shorty-6cbff5f5c5-g47xc 1/1 Running 0 27s
shorty-6cbff5f5c5-x7jf7 1/1 Running 0 27s
The three v2 pods kept serving. The default strategy only takes an old pod away when a new one is available, so a broken image stalls the rollout rather than taking the app down. Then:
kubectl rollout undo deploy/shorty
kubectl rollout status deploy/shorty
kubectl rollout history deploy/shorty
Two things caught me here, both small only. Before the bad rollout I had set kubernetes.io/change-cause="shorty v2" on the Deployment, and that annotation was copied into the v3 revision too, so the history showed two revisions both saying "shorty v2". The annotation is only useful if you update it on every change. And rollout undo printed a warning that the object was managed with kubectl apply and the last-applied annotation won't be updated. In plain words: the cluster now runs v2 while shorty.yaml in git says shorty:dev. Undo is the emergency button, then fix the file and apply it again.
Cleaning up
kind delete cluster --name shorty
All the manifests are four small files: postgres.yaml, redis.yaml, migrate-job.yaml and shorty.yaml. Next, the hard-coded env vars move into ConfigMaps and Secrets, and the pods get probes and memory limits, because right now Kubernetes thinks a pod is ready the moment the process starts.