thockinさんの「Alpineが(/etc/resolve.conf
のsearch
レコードに対応したよ!」というツイートを見て、「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)はあります。
- Alpine Linuxなら3.4から
- 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だと成功します。
- 3.3までは
- Alpineに関しては、
go-dnsmasq
を使って複数search
に対応したDNSサーバをコンテナ内に立てるか(上限なしになる)、もしくはmusl libc
ではなくglibc
を使ってスタブリゾルバ側で対応(上限6になる)しましょう。 -
glibc
を既に使っているなら、go-dnsmasq
を使ってスタブリゾルバではなくDNSサーバ側で対応しましょう(上限6から、上限なしに改善)。
-
-
nameserver
とsearch
の両方が上限に引っかかる場合はgo-dnsmasq
を利用しましょう
なぜ?
- kube-dnsは各アプリケーションコンテナの
/etc/resolv.conf
における1nameserver
レコードと3search
レコードを消費する - ところが、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
がある場合の名前解決が可能になった - でも
dnsmasq
はsearch
に対応していない(複数のsearch
ドメインそれぞれのDNSクエリを投げるのはリゾルバの責務 bydnsmasq
メンテナ) - したがって、Alpine 3.4からも、複数の
search
には対応できない - 対応したいときは、
glibc
をAlpineに入れるか、dnsmasq
ではなくgo-dnsmasq
を使うとよい
リゾルバ
- DNSサーバに名前解決を依頼する
- スタブリゾルバ=DNS Client。ライブラリに同梱されている
- libcのgetaddrinfo(3), getnameinfo(3)
- libcの実装の一つであるglibcには
search
レコードに対応したスタブリゾルバが同梱されている - 同様にlibcの実装の一つであるmusl libcには
search
レコードに対応していないスタブリゾルバが同梱されている
- libcの実装の一つであるglibcには
- golangはlibcのgetaddrinfo等を使う実装と、
/etc/resolv.conf
に書かれたネームサーバに問い合わせるだけの実装を選べる(On Unix systems, the resolver has two options for resolving names ...)net.LookupIP
- libcのgetaddrinfo(3), getnameinfo(3)
スタブリゾルバとは・名前解決の流れ
- An application invokes the stub resolver as function call, from a library.
- 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.
- 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.
- The caching name server (full resolver) sends an answer back to the stub resolver in the form of a DNS message.
- The stub resolver function returns a data structure to the application.
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
- DNSサーバ/フォワーダ、DHCPサーバ
- dnsmasq自体も
/etc/resolve.conf
を使ってレゾルバ経由で名前解決をする (参考: HowTo/dnsmasq - Debian Wiki) - /etc/resolv.conf について - Qiita
-
/etc/dnsmasq.conf
で/etc/resolve.conf
以外のリゾルバ設定ファイルを指定できる(参考: linux - How to overcome libc resolver limitation of maximum 3 nameservers? - Unix & Linux Stack Exchange) - 複数の
nameserver
に対応できる -
/etc/resolv.conf
のsearch
には対応できない(事例: dnsmasq with multiple search domains (Page 1) — General Discussion — OpenWrt)- というか、
dnsmasq
のメンテナによると、複数のsearch
に対応すべきはレゾルバであって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
を複数設定できる