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.

204 lines
8.1 KiB

  1. # K8s DNS stack by Kubespray
  2. For K8s cluster nodes, Kubespray configures a [Kubernetes DNS](https://kubernetes.io/docs/admin/dns/)
  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. ### searchdomains
  15. Custom search domains to be added in addition to the cluster search domains (``default.svc.{{ dns_domain }}, svc.{{ dns_domain }}``).
  16. Most Linux systems limit the total number of search domains to 6 and the total length of all search domains
  17. to 256 characters. Depending on the length of ``dns_domain``, you're limited to less than the total limit.
  18. Please note that ``resolvconf_mode: docker_dns`` will automatically add your systems search domains as
  19. additional search domains. Please take this into the accounts for the limits.
  20. ### nameservers
  21. This variable is only used by ``resolvconf_mode: host_resolvconf``. These nameservers are added to the hosts
  22. ``/etc/resolv.conf`` *after* ``upstream_dns_servers`` and thus serve as backup nameservers. If this variable
  23. is not set, a default resolver is chosen (depending on cloud provider or 8.8.8.8 when no cloud provider is specified).
  24. ### upstream_dns_servers
  25. DNS servers to be added *after* the cluster DNS. Used by all ``resolvconf_mode`` modes. These serve as backup
  26. DNS servers in early cluster deployment when no cluster DNS is available yet.
  27. ## DNS modes supported by Kubespray
  28. ### coredns_external_zones
  29. Array of optional external zones to coredns forward queries to. It's injected into
  30. `coredns`' config file before default kubernetes zone. Use it as an optimization for well-known zones and/or internal-only
  31. domains, i.e. VPN for internal networks (default is unset)
  32. Example:
  33. ```yaml
  34. coredns_external_zones:
  35. - zones:
  36. - example.com
  37. - example.io:1053
  38. nameservers:
  39. - 1.1.1.1
  40. - 2.2.2.2
  41. cache: 5
  42. - zones:
  43. - https://mycompany.local:4453
  44. nameservers:
  45. - 192.168.0.53
  46. cache: 0
  47. ```
  48. or as INI
  49. ```ini
  50. coredns_external_zones=[{"cache": 30,"zones":["example.com","example.io:453"],"nameservers":["1.1.1.1","2.2.2.2"]}]'
  51. ```
  52. You can modify how Kubespray sets up DNS for your cluster with the variables ``dns_mode`` and ``resolvconf_mode``.
  53. ### dns_mode
  54. ``dns_mode`` configures how Kubespray will setup cluster DNS. There are four modes available:
  55. #### dns_mode: coredns (default)
  56. This installs CoreDNS as the default cluster DNS for all queries.
  57. #### dns_mode: coredns_dual
  58. This installs CoreDNS as the default cluster DNS for all queries, plus a secondary CoreDNS stack.
  59. #### dns_mode: manual
  60. This does not install coredns, but allows you to specify
  61. `manual_dns_server`, which will be configured on nodes for handling Pod DNS.
  62. Use this method if you plan to install your own DNS server in the cluster after
  63. initial deployment.
  64. #### dns_mode: none
  65. This does not install any of DNS solution at all. This basically disables cluster DNS completely and
  66. leaves you with a non functional cluster.
  67. ## resolvconf_mode
  68. ``resolvconf_mode`` configures how Kubespray will setup DNS for ``hostNetwork: true`` PODs and non-k8s containers.
  69. There are three modes available:
  70. ### resolvconf_mode: docker_dns (default)
  71. This sets up the docker daemon with additional --dns/--dns-search/--dns-opt flags.
  72. The following nameservers are added to the docker daemon (in the same order as listed here):
  73. * cluster nameserver (depends on dns_mode)
  74. * content of optional upstream_dns_servers variable
  75. * host system nameservers (read from hosts /etc/resolv.conf)
  76. The following search domains are added to the docker daemon (in the same order as listed here):
  77. * cluster domains (``default.svc.{{ dns_domain }}``, ``svc.{{ dns_domain }}``)
  78. * content of optional searchdomains variable
  79. * host system search domains (read from hosts /etc/resolv.conf)
  80. The following dns options are added to the docker daemon
  81. * ndots:{{ ndots }}
  82. * timeout:2
  83. * attempts:2
  84. For normal PODs, k8s will ignore these options and setup its own DNS settings for the PODs, taking
  85. the --cluster_dns (either coredns or coredns_dual, depending on dns_mode) kubelet option into account.
  86. For ``hostNetwork: true`` PODs however, k8s will let docker setup DNS settings. Docker containers which
  87. are not started/managed by k8s will also use these docker options.
  88. The host system name servers are added to ensure name resolution is also working while cluster DNS is not
  89. running yet. This is especially important in early stages of cluster deployment. In this early stage,
  90. DNS queries to the cluster DNS will timeout after a few seconds, resulting in the system nameserver being
  91. used as a backup nameserver. After cluster DNS is running, all queries will be answered by the cluster DNS
  92. servers, which in turn will forward queries to the system nameserver if required.
  93. #### resolvconf_mode: host_resolvconf
  94. This activates the classic Kubespray behavior that modifies the hosts ``/etc/resolv.conf`` file and dhclient
  95. configuration to point to the cluster dns server (either coredns or coredns_dual, depending on dns_mode).
  96. As cluster DNS is not available on early deployment stage, this mode is split into 2 stages. In the first
  97. stage (``dns_early: true``), ``/etc/resolv.conf`` is configured to use the DNS servers found in ``upstream_dns_servers``
  98. and ``nameservers``. Later, ``/etc/resolv.conf`` is reconfigured to use the cluster DNS server first, leaving
  99. the other nameservers as backups.
  100. Also note, existing records will be purged from the `/etc/resolv.conf`,
  101. including resolvconf's base/head/cloud-init config files and those that come from dhclient.
  102. #### resolvconf_mode: none
  103. Does nothing regarding ``/etc/resolv.conf``. This leaves you with a cluster that works as expected in most cases.
  104. The only exception is that ``hostNetwork: true`` PODs and non-k8s managed containers will not be able to resolve
  105. cluster service names.
  106. ## Nodelocal DNS cache
  107. 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).
  108. More information on the rationale behind this implementation can be found [here](https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/0030-nodelocal-dns-cache.md).
  109. **As per the 2.10 release, Nodelocal DNS cache is enabled by default.**
  110. ### External zones
  111. It's possible to extent the `nodelocaldns`' configuration by adding an array of external zones. For example:
  112. ```yaml
  113. nodelocaldns_external_zones:
  114. - zones:
  115. - example.com
  116. - example.io:1053
  117. nameservers:
  118. - 1.1.1.1
  119. - 2.2.2.2
  120. cache: 5
  121. - zones:
  122. - https://mycompany.local:4453
  123. nameservers:
  124. - 192.168.0.53
  125. ```
  126. ## Limitations
  127. * Kubespray has yet ways to configure Kubedns addon to forward requests SkyDns can
  128. not answer with authority to arbitrary recursive resolvers. This task is left
  129. for future. See [official SkyDns docs](https://github.com/skynetservices/skydns)
  130. for details.
  131. * There is
  132. [no way to specify a custom value](https://github.com/kubernetes/kubernetes/issues/33554)
  133. for the SkyDNS ``ndots`` param.
  134. * the ``searchdomains`` have a limitation of a 6 names and 256 chars
  135. length. Due to default ``svc, default.svc`` subdomains, the actual
  136. limits are a 4 names and 239 chars respectively.
  137. * the ``nameservers`` have a limitation of a 3 servers, although there
  138. is a way to mitigate that with the ``upstream_dns_servers``,
  139. see below. Anyway, the ``nameservers`` can take no more than a two
  140. custom DNS servers because of one slot is reserved for a Kubernetes
  141. cluster needs.