archive / 2020

Post-mortem: it was DNS. Of course it was DNS.

A (lightly edited) version of our internal post-mortem of a 70-minute outage at the scale-up. A new worker, ndots:5, an undersized CoreDNS and a five-second mystery.

There’s a haiku every ops person knows: It’s not DNS. There’s no way it’s DNS. It was DNS. On Thursday we added a few verses of our own.

We write a post-mortem for every incident with customer impact. They’re blameless: we’re looking for what in the system made the mistake possible, not for who pressed the button. This is a slightly cleaned-up version of the one from this week, with internal names replaced. I’m sharing it because I learned a lot from other people’s post-mortems, and because the root cause is so common that I’m fairly sure some of you have the same time bomb in your cluster.

Summary#

On Thursday 24 September, between 14:02 and 15:12 CEST, roughly 30% of API requests failed or took more than five seconds. The cause was overloaded DNS inside our Kubernetes cluster. A new background worker generated an enormous number of DNS lookups for an external domain. Through the default ndots:5 setting, every lookup turned into ten queries, eight of which were pointless. CoreDNS hit its memory limit, got OOM-killed, and from then on every service in the cluster had trouble resolving names, including the names of our own databases.

Timeline#

Time (CEST)What happened
13:40Deploy of the new address-enrichment worker. Everything green.
13:55Queue fills up after a batch import, the HPA scales the worker from 3 to 40 pods.
14:02First alerts: error rate on the public API above 5%.
14:06On-call starts investigating. Database looks healthy, no deploys of the API.
14:15Pattern spotted: many requests take exactly five seconds longer.
14:24CoreDNS pods turn out to have restarted 11 times. OOMKilled.
14:31CoreDNS manually scaled from 2 to 6 replicas. Brief improvement, then worse again.
14:48Link to the new worker made via CoreDNS query logs.
14:52Worker scaled down to 0.
15:05CoreDNS stable, error rate drops.
15:12Everything back to normal. Incident closed.

I was incident commander. The 33 minutes between the first alert and spotting the pattern in CoreDNS are the part I’m least proud of, more on that below.

What exactly happened#

ndots:5, the default nobody reads#

Every pod in Kubernetes gets a /etc/resolv.conf that looks roughly like this:

nameserver 10.96.0.10
search orders.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

ndots:5 means: if a name contains fewer than five dots, first try it with every search domain appended, and only then as-is. That’s handy for internal names: postgres becomes postgres.orders.svc.cluster.local on the first try.

But our new worker called an external API at api.addresscheck.example.com. Three dots, so fewer than five. The resolver therefore first tries:

  1. api.addresscheck.example.com.orders.svc.cluster.local
  2. api.addresscheck.example.com.svc.cluster.local
  3. api.addresscheck.example.com.cluster.local
  4. our cloud provider’s internal search domain, inherited from the node
  5. and only then api.addresscheck.example.com

All of that for both an A and an AAAA record. Ten queries, eight of which come back NXDOMAIN. Per lookup.

No connection reuse#

On its own, that’s not dramatic. But the worker is written in Python and called requests.get() for every address. No session, so no connection reuse, so a new TCP connection and a new DNS lookup for every single call. Forty pods, each doing a few dozen calls per second: thousands of DNS queries per second, on a cluster where CoreDNS normally does a few hundred.

CoreDNS on a diet#

Our CoreDNS was still running with the standard manifest: two replicas and a memory limit of 170Mi. That’s fine for a quiet cluster, but under this load the cache and the number of open requests grew until the pods got OOM-killed. With two replicas, every restart means half your DNS capacity is gone. And the remaining replica then gets twice the load. You can see where this is going.

The five-second mystery#

What threw us off the scent for a long time: lots of requests weren’t failing, they were exactly five seconds slower. That’s the default timeout of the glibc resolver. It’s a known issue in Kubernetes: glibc sends the A and AAAA queries in parallel from the same socket, and under load a race condition in the kernel’s conntrack can drop one of the two packets. The resolver then waits five seconds before retrying. That’s been happening at a low level in our cluster for a long time, but nobody had noticed. Under this load it suddenly became very visible.

Our Go services use Go’s own resolver and were less affected by that race. But they were very much affected when CoreDNS was simply down, because they connect to Postgres and Redis by service name.

What went well#

  • The alerts fired quickly and were correct.
  • Once we knew it was DNS, the fix (worker to zero) was simple and immediately effective.
  • Communication to customer support was on time, thanks to the status page template from the previous incident.

What went less well#

  • We didn’t look at DNS until late. Twenty minutes went into the database and the API, because “that’s where the errors are”. The five-second pattern was the clue, and we should have recognised it sooner. Lesson for me: if latency piles up in exact round numbers, think timeouts first.
  • We had no dashboard for CoreDNS. CoreDNS exposes good Prometheus metrics out of the box. We just weren’t scraping them.
  • The worker went live without a load test against real volumes. Not the developer’s fault: our checklist for new services doesn’t ask for it.
  • Scaling CoreDNS up didn’t help as long as the source of the load kept running. Fighting symptoms costs time.

Action items#

  1. Lower ndots for workloads that mainly talk to external services. You can do that per pod:

    spec:
      dnsConfig:
        options:
          - name: ndots
            value: "2"
    

    Internal names like postgres or postgres.orders keep working through the search path; external names with two or more dots are resolved directly. (Done.)

  2. Reuse HTTP connections. In Python that means a requests.Session() per process; in Go a single shared http.Client instead of creating a new one per request. We’ve added a lint rule for the latter. (Done.)

  3. Roll out NodeLocal DNSCache. A DNS cache as a DaemonSet on every node. Pods talk to a local cache instead of going through conntrack to CoreDNS. It’s been GA since Kubernetes 1.18, which conveniently is what we’re upgrading to next month. (Planned.)

  4. Scale CoreDNS with the cluster via the cluster-proportional-autoscaler, with a minimum of four replicas and a more realistic memory limit. (Done.)

  5. Dashboard and alerts for CoreDNS: request rate, error rate (SERVFAIL), latency and restarts. (Done.)

  6. Add “expected external calls and volume” to the checklist for new services. (Done.)

Final thought#

Nobody did anything stupid here. A developer wrote a perfectly normal Python worker. The autoscaler did exactly what it was supposed to. CoreDNS ran with defaults that thousands of clusters use. It was the combination that bit us, and that’s almost always the case with incidents like this.

And yes, the haiku is now pinned in our incident channel.

    Tip: / opens search, ↑↓ to select, ↵ to open.