DNS Hell in k8s
Tags: kubernetes, dns, k8s, eks
Published: 2019-11-03 00:10:42

---
title: DNS Hell in k8s
published: true
tags: kubernetes, dns, k8s, eks
canonical_url: https://www.codacy.com/blog/dns-hell-in-kubernetes/
---

Here at Codacy, everyone's been working really hard in the last few months to move all of our services to kubernetes. And it has been a bumpy road... From having to run a nfs-server provisioner, to be able to share files between pods, to launching our own scheduler to avoid scaling issues, we can say that we're hitting some interesting problems along the way, giving us the opportunity to do and learn new things everyday - which is pretty cool.

In this blog post I will talk about one of the most common issues that almost everyone, including us, seems to struggle with when moving to k8s: **DNS intermittent delays**

One of our components, we simply call it worker, runs as a short-living pod and is responsible to spawn other pods that run the analysis tools (eslint, pylint, etc.), gather their data and save it. 
Because of some pitfalls on our architecture, we agreed that this was one of the toughest parts of the system to put on k8s and therefore, we decided to tackle it first. 
During one of the first attempts to put these workers running on k8s in production, after some minutes we noticed a problem that we didn't get during our tests on the development and staging environments. 
Some workers were throwing `UnknownHostExceptions` while trying to access graylog and the databases, that were running outside the cluster. This seemed to increase when the number of running workers increased on the nodes. After some research, we found lots of users complaining about DNS intermittent delays of ~5s in [this github issue][1]. This was a problem, since the [default timeout for the DNS is 5 seconds][2]. We went through almost all of the solutions referred in the issue thread:

1. ["*arp table overflow on the nodes (arp -n showing more than 1000 entries). Increasing the limits solved the problem*"][3]  
We checked this on our nodes and this was not a problem for us, since we had around 50 entries in all of the nodes.

2. ["*dnsPolicy: Default works without delays*"][4]  
Well, actually, this was a bit confusing for us because [despite this dnsPolicy being called Default, it is not the default policy in k8s][5]. The default DNS policy is "ClusterFirst", i.e., "any DNS query that does match the configured cluster domain suffix, is forwarded to the upstream nameserver inherited from the node", while the "Default" DNS policy just "inherits the name resolution configuration from the node that the pods run on". We tested this, by running some test pods trying to resolve www.google.com and this configuration decreased from an average of ~5s to ~2s... It is still a lot of time to resolve the name but we then decided to try it on our workers. However, after some minutes we still got the `UnknownHostExceptions` while trying to access the external services on startup.

3. [*"Use fully-qualified names when possible"*][6]  
As explained by [Marco Pracucci](https://twitter.com/pracucci) in [his blog][7], in k8s if you're trying to resolve a name of something outside of the cluster, with the default configuration on `/etc/resolv.conf`, any request for resolution that contains fewer than 5 dots will cycle through all of the search domains as well in an attempt to resolve. For example, to resolve `codacy.com`, `codacy.com.kube-system.svc.cluster.local.`, `codacy.com.svc.cluster.local.`, `codacy.com.cluster.local.`, `codacy.com.ec2.internal.` and finally `codacy.com.` must be looked up, for both `A` and `AAAA` records. While this didn't solve our issue, it was something good to be aware to tweak our apps and get a better performance. 

4. ["*using the option single-request-reopen on /etc/resolv.conf fixed the problem*"][8]  
Fortunately, the guys from [weaveworks][9] and also from [XING][10] investigated this problem in depth and explain in detail why this can be a solution. Basically, the root cause of these delays is the Linux connection tracking mechanism, aka `conntrack`, which is implemented as a kernel module and is inherently racy. According to the [man page for resolv.conf][2], the `single-request-reopen` option enables sequential lookups using different sockets for the `A` and `AAAA` requests, reducing the race conditions. We also tried this but, after some minutes, we continued to see workers failing and while running `conntrack -S` on the nodes, the `insert_failed` counter was still increasing.

5. ["*use tcp for DNS requests*"][11]  
Since UDP is lossy, one of the UDP packets might get dropped by the kernel due to the races, so the client will try to re-send it after a timeout (5 seconds). Therefore, we decided to try using TCP for DNS as a workaround for this issue, as it was also one of the workarounds suggested by [Pavithra Ramesh and Blake Barnett in their recent talk at Europe's Kubecon][12]. Despite this being a bit slower because we are now using TCP, it actually solved the problem for us and the pods stopped failing.

Some good news is that these DNS problems caused by race conditions in the connection tracking mechanism (fun fact: this is [briefly "documented" in Linux's source code][18]) already have two patches to fix it (if you're brave enough, you can take a look at them [here][19] and [here][20]). 
However, the most recent patch is only available since version 5 of the linux kernel and it's not always possible to control the kernel version of the nodes where your pods will run. In our case, since we are running on EKS, we are using Amazon Linux 2 and the most [recent update (7/18/2019) only supports the 4.19 kernel][17], which only contains one of the patches. 

Meanwhile, the most proper solution seems to be the usage of a [NodeLocal DNScache (beta in k8s v1.15)][16], already detailed in the [k8s official documentation][13]. This solution aims to improve the overall DNS performance on the cluster by running DNS caching agents on every node as a [DaemonSet][21] so pods can reach out to these agents running on the same node, thereby reducing the number of upstream trips which would still use `conntrack` and increase the probability of being impacted by the race conditions previously referred.

Other solutions, such as using a sidecar on every pod running [tc][15] to [delay DNS packages][14], we actually didn't try since they seemed more complex and required more configuration. We also discarded every solution that required any modification on the nodes configuration since we can also deploy our application on-premises and, in this case, the cluster nodes are managed by our customers.

In the end, after some research and the typical "trial and error", we were able to find a workaround for this issue until we get to a proper solution in the future, enabling us to proceed and hit the next problem. 
:smile:

_See the original article [here][22]_ 

[1]: https://github.com/kubernetes/kubernetes/issues/56903
[2]: http://man7.org/linux/man-pages/man5/resolv.conf.5.html
[3]: https://github.com/kubernetes/kubernetes/issues/56903#issuecomment-359035254
[4]: https://github.com/kubernetes/kubernetes/issues/56903#issuecomment-359085202
[5]: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
[6]: https://github.com/kubernetes/kubernetes/issues/56903#issuecomment-397404229
[7]: https://pracucci.com/kubernetes-dns-resolution-ndots-options-and-why-it-may-affect-application-performances.html
[8]: https://github.com/kubernetes/kubernetes/issues/56903#issuecomment-359897058
[9]: https://www.weave.works/blog/racy-conntrack-and-dns-lookup-timeouts
[10]: https://tech.xing.com/a-reason-for-unexplained-connection-timeouts-on-kubernetes-docker-abd041cf7e02
[11]: https://github.com/kubernetes/kubernetes/issues/56903#issuecomment-424498508
[12]: https://www.youtube.com/watch?v=XbkViBUuScE&feature=youtu.be
[13]: https://kubernetes.io/docs/tasks/administer-cluster/nodelocaldns/#motivation
[14]: https://blog.quentin-machu.fr/2018/06/24/5-15s-dns-lookups-on-kubernetes/
[15]: https://linux.die.net/man/8/tc
[16]: https://sysdig.com/blog/whats-new-kubernetes-1-15/
[17]: https://aws.amazon.com/amazon-linux-2/release-notes/
[18]: https://github.com/torvalds/linux/blob/24de3d377539e384621c5b8f8f8d8d01852dddc8/net/netfilter/nf_nat_core.c#L290-L291
[19]: https://github.com/torvalds/linux/commit/ed07d9a021df6da53456663a76999189badc432a
[20]: https://github.com/torvalds/linux/commit/4e35c1cb9460240e983a01745b5f29fe3a4d8e39
[21]: https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
[22]: https://www.codacy.com/blog/dns-hell-in-kubernetes/