Ubuntu 18.04にdnsmasqでローカルDNSをたてた

自宅のOpenStack用に、Ubuntu 18.04上にdnsmasqでローカルDNSをたてました。

自宅のOpenStackはコミュニティのインストールガイドにほぼ従っているので、コントローラとコンピュートの名前解決は/etc/hostsに書いています。インスタンスをたてて使っている分には問題なかったのですが、インスタンスの上でKubernetesクラスタを動かすとよろしくありません。KubernetesのPodはdnsPolicyがClusterFirstである場合、kube-dnsで解決出来ない名前解決は動いているノードに設定されているDNSサーバで名前解決します。そのため/etc/hostsではPodが名前解決できませんのでDNSサーバを建てることにしました。

dnsmasqはホストにある/etc/hostsをそのまま名前解決に使い、/etc/hostsにないものはホストのDNSサーバに問い合わせます。すでにControllerには/etc/hostsを書いているので、これを使います。

Controllerにdnsmasqをインストールします。

sudo apt install dnsmasq

dnsmasqの設定ファイルを修正します。

# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/hidekazuna.localdomain/
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts
# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=hidekazuna.localdomain

/etc/hostsを修正します。

# controller
10.0.0.11  controller.localdomain controller
# compute1
10.0.0.31  compute1.localdomain compute1
# compute2
10.0.0.32  compute2.localdomain compute2

dnsmasqを再起動します。

sudo systemctl restart dnsmasq

systemd-resolvedを再起動します。

sudo systemctl restart systemd-resolved

上記でうまくいかないときは、/etc/netplan/下に作成したネットワークの設定ファイルであるYAML形式のファイルから以下2つのファイルが生成されていることを確認します。1つ目のファイル名はインタフェース名により異なります。

  • /run/systemd/network/10-netplan-eno1.network
  • /run/systemd/resolve/resolv.conf

よくわからず自信がない場合は生成して適用後(systemd-networkdは再起動不要のようですが)systemd-networkd、systemd-resolved、dnsmasqを再起動します。

sudo netplan generate
sudo netplan apply
sudo systemctl restart systemd-networkd
sudo systemctl restart systemd-resolved
sudo systemctl restart dnsmasq

最後に名前解決できることを確認します。

$ dig controller

; <<>> DiG 9.11.3-1ubuntu1-Ubuntu <<>> controller
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39671
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;controller.			IN	A

;; ANSWER SECTION:
controller.		0	IN	A	10.0.0.11

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Jan 27 21:25:33 JST 2019
;; MSG SIZE  rcvd: 55
$ dig www.google.com

; <<>> DiG 9.11.3-1ubuntu1-Ubuntu <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27197
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.google.com.			IN	A

;; ANSWER SECTION:
www.google.com.		219	IN	A	172.217.161.36

;; Query time: 8 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Jan 27 21:27:13 JST 2019
;; MSG SIZE  rcvd: 59

ControllerにDNSサーバをたてたので、Computeから使うように設定変更します。Computeの設定は/etc/netplan/下にあるネットワーク設定ファイルのDNSサーバの値を変更したあと、2つのファイルを生成、適用してsystemd-resolvedを再起動します。

sudo netplan generate
sudo netplan apply
sudo systemctl restart systemd-resolved

名前解決できることを確認します。(下記はcompute2での実施例)

$ dig compute2

; <<>> DiG 9.11.3-1ubuntu1-Ubuntu <<>> compute2
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24121
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;compute2.			IN	A

;; ANSWER SECTION:
compute2.		0	IN	A	10.0.0.32

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Jan 27 21:43:08 JST 2019
;; MSG SIZE  rcvd: 53

$ dig www.google.com

; <<>> DiG 9.11.3-1ubuntu1-Ubuntu <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 914
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.google.com.			IN	A

;; ANSWER SECTION:
www.google.com.		219	IN	A	216.58.197.4

;; Query time: 6 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Jan 27 21:43:10 JST 2019
;; MSG SIZE  rcvd: 59

hidekazu@compute2:~$

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください