You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
13 KiB

  1. # K8s DNS stack by Kubespray
  2. For K8s cluster nodes, Kubespray configures a [Kubernetes DNS](https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/)
  3. [cluster add-on](https://releases.k8s.io/master/cluster/addons/README.md)
  4. to serve as an authoritative DNS server for a given ``dns_domain`` and its
  5. ``svc, default.svc`` default subdomains (a total of ``ndots: 5`` max levels).
  6. Other nodes in the inventory, like external storage nodes or a separate etcd cluster
  7. node group, considered non-cluster and left up to the user to configure DNS resolve.
  8. ## DNS variables
  9. There are several global variables which can be used to modify DNS settings:
  10. ### ndots
  11. ndots value to be used in ``/etc/resolv.conf``
  12. It is important to note that multiple search domains combined with high ``ndots``
  13. values lead to poor performance of DNS stack, so please choose it wisely.
  14. ## dns_timeout
  15. timeout value to be used in ``/etc/resolv.conf``
  16. ## dns_attempts
  17. attempts value to be used in ``/etc/resolv.conf``
  18. ### searchdomains
  19. Custom search domains to be added in addition to the cluster search domains (``default.svc.{{ dns_domain }}, svc.{{ dns_domain }}``).
  20. Most Linux systems limit the total number of search domains to 6 and the total length of all search domains
  21. to 256 characters. Depending on the length of ``dns_domain``, you're limited to less than the total limit.
  22. `remove_default_searchdomains: true` will remove the default cluster search domains.
  23. Please note that ``resolvconf_mode: docker_dns`` will automatically add your systems search domains as
  24. additional search domains. Please take this into the accounts for the limits.
  25. ### nameservers
  26. This variable is only used by ``resolvconf_mode: host_resolvconf``. These nameservers are added to the hosts
  27. ``/etc/resolv.conf`` *after* ``upstream_dns_servers`` and thus serve as backup nameservers. If this variable
  28. is not set, a default resolver is chosen (depending on cloud provider or 8.8.8.8 when no cloud provider is specified).
  29. ### upstream_dns_servers
  30. DNS servers to be added *after* the cluster DNS. Used by all ``resolvconf_mode`` modes. These serve as backup
  31. DNS servers in early cluster deployment when no cluster DNS is available yet.
  32. ### dns_upstream_forward_extra_opts
  33. Whether or not upstream DNS servers come from `upstream_dns_servers` variable or /etc/resolv.conf, related forward block in coredns (and nodelocaldns) configuration can take options (see <https://coredns.io/plugins/forward/> for details).
  34. These are configurable in inventory in as a dictionary in the `dns_upstream_forward_extra_opts` variable.
  35. By default, no other option than the ones hardcoded (see `roles/kubernetes-apps/ansible/templates/coredns-config.yml.j2` and `roles/kubernetes-apps/ansible/templates/nodelocaldns-config.yml.j2`).
  36. ### coredns_kubernetes_extra_opts
  37. Custom options to be added to the kubernetes coredns plugin.
  38. ### coredns_kubernetes_extra_domains
  39. Extra domains to be forwarded to the kubernetes coredns plugin.
  40. ### coredns_rewrite_block
  41. [Rewrite](https://coredns.io/plugins/rewrite/) plugin block to perform internal message rewriting.
  42. ### coredns_external_zones
  43. Array of optional external zones to coredns forward queries to. It's injected into
  44. `coredns`' config file before default kubernetes zone. Use it as an optimization for well-known zones and/or internal-only
  45. domains, i.e. VPN for internal networks (default is unset)
  46. Example:
  47. ```yaml
  48. coredns_external_zones:
  49. - zones:
  50. - example.com
  51. - example.io:1053
  52. nameservers:
  53. - 1.1.1.1
  54. - 2.2.2.2
  55. cache: 5
  56. - zones:
  57. - https://mycompany.local:4453
  58. nameservers:
  59. - 192.168.0.53
  60. cache: 0
  61. - zones:
  62. - mydomain.tld
  63. nameservers:
  64. - 10.233.0.3
  65. cache: 5
  66. rewrite:
  67. - name stop website.tld website.namespace.svc.cluster.local
  68. ```
  69. or as INI
  70. ```ini
  71. coredns_external_zones='[{"cache": 30,"zones":["example.com","example.io:453"],"nameservers":["1.1.1.1","2.2.2.2"]}]'
  72. ```
  73. ### dns_etchosts (coredns)
  74. Optional hosts file content to coredns use as /etc/hosts file. This will also be used by nodelocaldns, if enabled.
  75. Example:
  76. ```yaml
  77. dns_etchosts: |
  78. 192.168.0.100 api.example.com
  79. 192.168.0.200 ingress.example.com
  80. ```
  81. ### enable_coredns_reverse_dns_lookups
  82. Whether reverse DNS lookups are enabled in the coredns config. Defaults to `true`.
  83. ### CoreDNS default zone cache plugin
  84. If you wish to configure the caching behaviour of CoreDNS on the default zone, you can do so using the `coredns_default_zone_cache_block` string block.
  85. An example value (more information on the [plugin's documentation](https://coredns.io/plugins/cache/)) to:
  86. * raise the max cache TTL to 3600 seconds
  87. * raise the max amount of success responses to cache to 3000
  88. * disable caching of denial responses altogether
  89. * enable pre-fetching of lookups with at least 10 lookups per minute before they expire
  90. Would be as follows:
  91. ```yaml
  92. coredns_default_zone_cache_block: |
  93. cache 3600 {
  94. success 3000
  95. denial 0
  96. prefetch 10 1m
  97. }
  98. ```
  99. ### Handle old/extra dns_domains
  100. If you need to change the dns_domain of your cluster for whatever reason (switching to or from `cluster.local` for example),
  101. and you have workloads that embed it in their configuration you can use the variable `old_dns_domains`.
  102. This will add some configuration to coredns and nodelocaldns to ensure the DNS requests using the old domain are handled correctly.
  103. Example:
  104. ```yaml
  105. old_dns_domains:
  106. - example1.com
  107. - example2.com
  108. dns_domain: cluster.local
  109. ```
  110. will make `my-svc.my-ns.svc.example1.com`, `my-svc.my-ns.svc.example2.com` and `my-svc.my-ns.svc.cluster.local` have the same DNS answer.
  111. ### systemd_resolved_disable_stub_listener
  112. Whether or not to set `DNSStubListener=no` when using systemd-resolved. Defaults to `true` on Flatcar.
  113. You might need to set it to `true` if CoreDNS fails to start with `address already in use` errors.
  114. ## DNS modes supported by Kubespray
  115. You can modify how Kubespray sets up DNS for your cluster with the variables ``dns_mode`` and ``resolvconf_mode``.
  116. ### dns_mode
  117. ``dns_mode`` configures how Kubespray will setup cluster DNS. There are four modes available:
  118. #### dns_mode: coredns (default)
  119. This installs CoreDNS as the default cluster DNS for all queries.
  120. #### dns_mode: coredns_dual
  121. This installs CoreDNS as the default cluster DNS for all queries, plus a secondary CoreDNS stack.
  122. #### dns_mode: manual
  123. This does not install coredns, but allows you to specify
  124. `manual_dns_server`, which will be configured on nodes for handling Pod DNS.
  125. Use this method if you plan to install your own DNS server in the cluster after
  126. initial deployment.
  127. #### dns_mode: none
  128. This does not install any of DNS solution at all. This basically disables cluster DNS completely and
  129. leaves you with a non functional cluster.
  130. ## resolvconf_mode
  131. ``resolvconf_mode`` configures how Kubespray will setup DNS for ``hostNetwork: true`` PODs and non-k8s containers.
  132. There are three modes available:
  133. ### resolvconf_mode: host_resolvconf (default)
  134. This activates the classic Kubespray behavior that modifies the hosts ``/etc/resolv.conf`` file and dhclient
  135. configuration to point to the cluster dns server (either coredns or coredns_dual, depending on dns_mode).
  136. As cluster DNS is not available on early deployment stage, this mode is split into 2 stages. In the first
  137. stage (``dns_early: true``), ``/etc/resolv.conf`` is configured to use the DNS servers found in ``upstream_dns_servers``
  138. and ``nameservers``. Later, ``/etc/resolv.conf`` is reconfigured to use the cluster DNS server first, leaving
  139. the other nameservers as backups.
  140. Also note, existing records will be purged from the `/etc/resolv.conf`,
  141. including resolvconf's base/head/cloud-init config files and those that come from dhclient.
  142. ### resolvconf_mode: docker_dns
  143. This sets up the docker daemon with additional --dns/--dns-search/--dns-opt flags.
  144. The following nameservers are added to the docker daemon (in the same order as listed here):
  145. * cluster nameserver (depends on dns_mode)
  146. * content of optional upstream_dns_servers variable
  147. * host system nameservers (read from hosts /etc/resolv.conf)
  148. The following search domains are added to the docker daemon (in the same order as listed here):
  149. * cluster domains (``default.svc.{{ dns_domain }}``, ``svc.{{ dns_domain }}``)
  150. * content of optional searchdomains variable
  151. * host system search domains (read from hosts /etc/resolv.conf)
  152. The following dns options are added to the docker daemon
  153. * ndots:{{ ndots }}
  154. * timeout:2
  155. * attempts:2
  156. These dns options can be overridden by setting a different list:
  157. ```yaml
  158. docker_dns_options:
  159. - ndots:{{ ndots }}
  160. - timeout:2
  161. - attempts:2
  162. - rotate
  163. ```
  164. For normal PODs, k8s will ignore these options and setup its own DNS settings for the PODs, taking
  165. the --cluster_dns (either coredns or coredns_dual, depending on dns_mode) kubelet option into account.
  166. For ``hostNetwork: true`` PODs however, k8s will let docker setup DNS settings. Docker containers which
  167. are not started/managed by k8s will also use these docker options.
  168. The host system name servers are added to ensure name resolution is also working while cluster DNS is not
  169. running yet. This is especially important in early stages of cluster deployment. In this early stage,
  170. DNS queries to the cluster DNS will timeout after a few seconds, resulting in the system nameserver being
  171. used as a backup nameserver. After cluster DNS is running, all queries will be answered by the cluster DNS
  172. servers, which in turn will forward queries to the system nameserver if required.
  173. ### resolvconf_mode: none
  174. Does nothing regarding ``/etc/resolv.conf``. This leaves you with a cluster that works as expected in most cases.
  175. The only exception is that ``hostNetwork: true`` PODs and non-k8s managed containers will not be able to resolve
  176. cluster service names.
  177. ## Nodelocal DNS cache
  178. Setting ``enable_nodelocaldns`` to ``true`` will make pods reach out to the dns (core-dns) caching agent running on the same node, thereby avoiding iptables DNAT rules and connection tracking. The local caching agent will query core-dns (depending on what main DNS plugin is configured in your cluster) for cache misses of cluster hostnames(cluster.local suffix by default).
  179. More information on the rationale behind this implementation can be found [here](https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/1024-nodelocal-cache-dns/README.md).
  180. **As per the 2.10 release, Nodelocal DNS cache is enabled by default.**
  181. ### External zones
  182. It's possible to extent the `nodelocaldns`' configuration by adding an array of external zones. For example:
  183. ```yaml
  184. nodelocaldns_external_zones:
  185. - zones:
  186. - example.com
  187. - example.io:1053
  188. nameservers:
  189. - 1.1.1.1
  190. - 2.2.2.2
  191. cache: 5
  192. - zones:
  193. - https://mycompany.local:4453
  194. nameservers:
  195. - 192.168.0.53
  196. ```
  197. ### dns_etchosts (nodelocaldns)
  198. See [dns_etchosts](#dns_etchosts-coredns) above.
  199. ### Nodelocal DNS HA
  200. Under some circumstances the single POD nodelocaldns implementation may not be able to be replaced soon enough and a cluster upgrade or a nodelocaldns upgrade can cause DNS requests to time out for short intervals. If for any reason your applications cannot tolerate this behavior you can enable a redundant nodelocal DNS pod on each node:
  201. ```yaml
  202. enable_nodelocaldns_secondary: true
  203. ```
  204. **Note:** when the nodelocaldns secondary is enabled, the primary is instructed to no longer tear down the iptables rules it sets up to direct traffic to itself. In case both daemonsets have failing pods on the same node, this can cause a DNS blackout with traffic no longer being forwarded to the coredns central service as a fallback. Please ensure you account for this also if you decide to disable the nodelocaldns cache.
  205. There is a time delta (in seconds) allowed for the secondary nodelocaldns to survive in case both primary and secondary daemonsets are updated at the same time. It is advised to tune this variable after you have performed some tests in your own environment.
  206. ```yaml
  207. nodelocaldns_secondary_skew_seconds: 5
  208. ```
  209. ## Limitations
  210. * Kubespray has yet ways to configure Kubedns addon to forward requests SkyDns can
  211. not answer with authority to arbitrary recursive resolvers. This task is left
  212. for future. See [official SkyDns docs](https://github.com/skynetservices/skydns)
  213. for details.
  214. * There is
  215. [no way to specify a custom value](https://github.com/kubernetes/kubernetes/issues/33554)
  216. for the SkyDNS ``ndots`` param.
  217. * the ``searchdomains`` have a limitation of a 6 names and 256 chars
  218. length. Due to default ``svc, default.svc`` subdomains, the actual
  219. limits are a 4 names and 239 chars respectively. If `remove_default_searchdomains: true`
  220. added you are back to 6 names.
  221. * the ``nameservers`` have a limitation of a 3 servers, although there
  222. is a way to mitigate that with the ``upstream_dns_servers``,
  223. see below. Anyway, the ``nameservers`` can take no more than a two
  224. custom DNS servers because of one slot is reserved for a Kubernetes
  225. cluster needs.