Six months of Kubernetes in production: what I wish I'd known
At the scale-up we moved our services to Kubernetes over the past six months. The lessons, the YAML, the outages we caused ourselves and the things that turned out to be really good.
When I started at the scale-up in December, the Kubernetes migration was “almost done”. In practice that meant one test cluster, three services on it, and a wiki page titled “TODO”. Six months later all 23 services run on Kubernetes, our old VMs with Ansible are gone, and I’ve written more YAML than in the rest of my life combined.
Here’s what I learned. Not a tutorial, there are enough of those. More a list of things I’d have liked someone to tell me in December.
The setup#
For context: we run on AWS, with clusters created by kops. One cluster for staging, one for production, currently on Kubernetes 1.6. In front of everything is the nginx ingress controller, and for certificates we use kube-lego, which automatically requests Let’s Encrypt certificates for every Ingress with the right annotation. Since Let’s Encrypt left beta last year I haven’t paid for a single certificate, and I don’t intend to start again.
Most of our services are Go, a few are Node, and there’s one old Python service that nobody wants to touch.
Lesson 1: resource requests and limits are not optional#
Our first outage was entirely my fault. I had deployed a new service without resources. Kubernetes happily scheduled it on a node that was already busy, the service had a memory leak under load, and it took the whole node down with it, along with four other services.
Since then every Deployment has requests and limits, and we have a check in CI that refuses manifests without them. A typical Deployment looks like this now:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: orders-api
labels:
app: orders-api
spec:
replicas: 3
template:
metadata:
labels:
app: orders-api
spec:
containers:
- name: orders-api
image: registry.example.com/orders-api:1.14.2
ports:
- containerPort: 8080
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: orders-config
key: db-host
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
periodSeconds: 10
A container that exceeds its memory limit gets killed with OOMKilled. That sounds harsh, but it’s much better than a node that slowly grinds to a halt.
Lesson 2: readiness and liveness are different things#
A readiness probe says: “send me traffic, I’m ready”. A liveness probe says: “I’m still alive, don’t restart me”. We had both pointing at the same endpoint, and that endpoint also checked the database connection. Result: the database hiccuped for thirty seconds, every liveness probe failed, Kubernetes restarted every pod at the same time, and the thirty-second hiccup became a five-minute outage.
Now liveness only checks whether the process itself responds. Dependencies only go in readiness.
Lesson 3: handle SIGTERM#
When Kubernetes stops a pod, it first sends SIGTERM, then waits (30 seconds by default), and only then SIGKILL. If your app ignores SIGTERM and simply dies, you drop requests on every deploy. In Go this has been nice to solve since 1.8, thanks to Server.Shutdown:
srv := &http.Server{Addr: ":8080", Handler: router}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %v", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, os.Interrupt)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("shutdown: %v", err)
}
After we rolled this out across all Go services, the error spikes during deploys disappeared from our dashboards.
Lesson 4: YAML is a programming language, and a bad one#
Indentation errors that only show up on kubectl apply. A list where you meant a map. And my personal favourite: a ConfigMap with a list of country codes, where NO (Norway) was read as the boolean false. Because YAML 1.1 thinks yes, no, on and off are booleans. Quote your strings. All of them.
We now run every manifest through kubectl apply --dry-run in CI, and we’re looking at Helm for the services that differ per environment. I’m not entirely happy about templating YAML with Go templates, but copy-pasting 23 almost-identical files is worse.
Lesson 5: Secrets aren’t secret#
A Kubernetes Secret is base64-encoded, not encrypted. Anyone who can read Secrets in the namespace can read your database password. Since 1.6, RBAC is in beta and we use it to strictly limit who can do what. Our developers can view and restart pods in their own namespace, and that’s about it. That caused some grumbling, but it’s the right call.
Lesson 6: learn kubectl by heart#
These are the commands I type a hundred times a day:
kubectl get pods -o wide
kubectl describe pod orders-api-3891723545-x7k2p
kubectl logs -f orders-api-3891723545-x7k2p
kubectl logs -p orders-api-3891723545-x7k2p # logs of the previous, crashed container
kubectl rollout status deployment/orders-api
kubectl rollout undo deployment/orders-api
describe is the most underrated one. Nine times out of ten the answer to “why won’t my pod start?” is in the Events at the bottom.
Was it worth it?#
Yes. Deploys went from “Ansible run of fifteen minutes that sometimes fails halfway” to a rolling update that we can roll back with one command. A dead node gets noticed and its pods get rescheduled elsewhere, without anyone being woken up. And new services get a standard shape: Dockerfile, Deployment, Service, Ingress, done.
But it’s not magic. Kubernetes doesn’t take complexity away, it moves it. Earlier the complexity was in our scripts; now it’s in a system with a lot of moving parts that you have to understand when something goes wrong at night. Anyone who tells you otherwise has never debugged a broken overlay network.
Jef would hate it, by the way. You can deploy on a Friday now without breaking a sweat. That doesn’t mean you should.