LoginSignup
13
11

More than 5 years have passed since last update.

kube-dnsとlibc、スタブリゾルバ、またはいつdnsmasqやgo-dnsmasqが必要になるのか

Last updated at Posted at 2016-08-18

thockinさんの「Alpineが(/etc/resolve.confsearchレコードに対応したよ!」というツイートを見て、「Alpineでgo-dnsmasqいらなくなったのかな?」と思ったら、まだ必要なケースはありそうだったのでメモ。

TL;DR;

  • Kubernetesがアプリケーションコンテナ内/etc/resolv.confの1つのnameserverを消費するので、それでnameserverの上限を超えるようならdnsmasqをDockerイメージにインストールしておきましょう。
    • Alpine Linuxなら3.4からapk add dnsmasqできます。musl libcを使うAlpine Linuxは上限が低いですが、glibcを使う場合でも上限(=3)はあります。
  • Kubernetesが3つのsearchレコードを消費するので、それで上限を超えるようならgo-dnsmasqをDockerイメージにインストールしておきましょう。
    • musl libcを使うAlpine Linuxに関しては
      • 3.3まではsearchに対応してなかったので、上限0です。
      • 3.4からはsearchに対応しています(上限がいくつかは不明)
      • docker run -it --rm --dns-search google.com alpine:3.3 nslookup wwwなどでテストできます。alpine:3.3だとエラー、alpine:3.4だと成功します。
    • Alpineに関しては、go-dnsmasqを使って複数searchに対応したDNSサーバをコンテナ内に立てるか(上限なしになる)、もしくはmusl libcではなくglibcを使ってスタブリゾルバ側で対応(上限6になる)しましょう。
    • glibcを既に使っているなら、go-dnsmasqを使ってスタブリゾルバではなくDNSサーバ側で対応しましょう(上限6から、上限なしに改善)。
  • nameserversearchの両方が上限に引っかかる場合はgo-dnsmasqを利用しましょう

なぜ?

  • kube-dnsは各アプリケーションコンテナの/etc/resolv.confにおける1 nameserverレコードと3 searchレコードを消費する
  • ところが、glibcのスタブリゾルバは最大3つのnameserverレコードと6つのsearchレコードにまで対応
  • なので、システム要件的にkube-dns以外に3つ以上のnameserverまたは4つ以上のsearchレコードが必要な場合、glibcのスタブリゾルバで対応できなくなる
  • また、Alpine Linuxに使われていることで有名なmusl libcのスタブリゾルバは最大1つ(要出典)のnameserverレコードに対応、searchには非対応。nameserverに関してはkube-dns以外を使うともう上限を超えてしまうし、searchは対応してないのでdigやnslookupがエラーになっていた
  • Alpine Linux 3.4からdnsmasqがインストールできるようになった
  • dnsmasqは複数nameserverに対応しているので、スタブリゾルバ→dnsmasq経由で複数nameserverがある場合の名前解決が可能になった
  • でもdnsmasqsearchに対応していない(複数のsearchドメインそれぞれのDNSクエリを投げるのはリゾルバの責務 by dnsmasqメンテナ)
  • したがって、Alpine 3.4からも、複数のsearchには対応できない
  • 対応したいときは、glibcをAlpineに入れるか、dnsmasqではなくgo-dnsmasqを使うとよい

リゾルバ

  • DNSサーバに名前解決を依頼する
  • スタブリゾルバ=DNS Client。ライブラリに同梱されている
    • libcのgetaddrinfo(3), getnameinfo(3)
      • libcの実装の一つであるglibcにはsearchレコードに対応したスタブリゾルバが同梱されている
      • 同様にlibcの実装の一つであるmusl libcにはsearchレコードに対応していないスタブリゾルバが同梱されている
    • golangはlibcのgetaddrinfo等を使う実装と、/etc/resolv.confに書かれたネームサーバに問い合わせるだけの実装を選べる(On Unix systems, the resolver has two options for resolving names ...)
      • net.LookupIP

参考: DNS System Components

スタブリゾルバとは・名前解決の流れ

  1. An application invokes the stub resolver as function call, from a library.
  2. The stub resolver, possibly after consulting /etc/hosts, ldap, nis+, etc., sends a recursive DNS query to a DNS server via the network. If necessary, the stub resolver will retransmit the query, query another DNS server, etc., until either it gets an answer or gives up.
  3. The DNS server, acting as a full resolver (a caching name server), consults its cache and then, if necessary, performs recursion (asks other name servers, traversing the DNS name hierarchy) in order to find the answer.
  4. The caching name server (full resolver) sends an answer back to the stub resolver in the form of a DNS message.
  5. The stub resolver function returns a data structure to the application.

引用元: confused wiht the full resolver and stub resolver

glibcとは何なのか

The GNU Library is not just a single library, but is in fact a collection of libraries that include the C library, math library, threading libraries, DNS stub resolver library, name service libraries. All of these libraries together constitute "the implementation."
FAQ - glibc wiki

dnsmasq

the search-domain function is implemented by the libc resolver code,
which should generate separate DNS queries for host2.domain1.local,
host2.domain2.local, etc.
Comment #4 : Bug #781557 : Bugs : dnsmasq package : Ubuntu in Bug #781557 “multiple .local search domains not honoured” : Bugs : dnsmasq package : Ubuntu

go-dnsmasq

GitHub: https://github.com/janeczku/go-dnsmasq

  • 軽量なキャシュありのDNSサーバ/フォワーダ
  • dnsmasqの代替として機能
  • dnsmasq同様、nameserverを複数設定できる
  • dnsmasqと違ってsearchを複数設定できる
13
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11