- systemd-nspawnコンテナで動いているサービスに対してIPアドレスではなくコンテナ名でアクセスしたかったので、名前解決の方法する方法がないか調べてみたところ、
nss-mymachines
というのがあることを知ったので、manを読んでみた。 -
nss-mymachines
はNSSのプラグインなので、使い方としては/etc/nsswitch.conf
からこのモジュールを使用するよう設定すればいいっぽい
manの訳
原文: https://www.freedesktop.org/software/systemd/man/nss-mymachines.html
Name
nss-mymachines, libnss_mymachines.so.2 — Provide hostname resolution for local container instances.
nss-mymachines, libnss_mymachines.so.2 — ローカルコンテナインスタンス向けにホスト名解決を提供します.
Synopsis
libnss_mymachines.so.2
Description
nss-mymachines is a plug-in module for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc), providing hostname resolution for the names of containers running locally that are registered with systemd-machined.service(8). The container names are resolved to the IP addresses of the specific container, ordered by their scope. This functionality only applies to containers using network namespacing (see the description of --private-network in systemd-nspawn(1)). Note that the name that is resolved is the one registered with systemd-machined, which may be different than the hostname configured inside of the container.
nss-mymachinesは、GNU Cライブラリ(glibc)のGNUネームサービススイッチ(NSS)機能用のプラグインモジュールで、systemd-machined.service(8)に登録されている、ローカルで実行されているコンテナの名前に対するホスト名解決を提供します。 。
コンテナー名は特定のコンテナーのIPアドレスに解決され、それらの範囲によって順序付けされます。
この機能はネットワークネームスペースを使用するコンテナにのみ適用されます(systemd-nspawn(1)の--private-networkの説明を参照)。
解決される名前はsystemd-machinedに登録されている名前であることに注意してください。これはコンテナーの内部で構成されたホスト名とは異なる場合があります。
The module also provides name resolution for user and group identifiers mapped to containers. All names from the range allocated to a given container container are exposed on the host as "vu-container-uid" and "vg-container-gid" (see example below). This functionality only applies to containers using user namespacing (see the description of --private-users in systemd-nspawn(1)).
このモジュールは、コンテナーにマップされたユーザーIDとグループIDの名前解決も行います。
特定のコンテナーコンテナーに割り当てられた範囲のすべての名前は、 "vu-container-uid"および "vg-container-gid"としてホストに公開されます(以下の例を参照)。
この機能はユーザネームスペースを使用するコンテナにのみ適用されます(systemd-nspawn(1)の--private-usersの説明を参照)。
To activate the NSS module, add "mymachines" to the lines starting with "hosts:", "passwd:" and "group:" in /etc/nsswitch.conf.
NSSモジュールを有効にするには、/etc/nsswitch.conf の "hosts:"、"passwd:" および "group:" で始まる行に "mymachines" を追加します。
It is recommended to place "mymachines" after the "files" or "compat" entry of the /etc/nsswitch.conf lines to make sure that its mappings are preferred over other resolvers such as DNS, but so that /etc/hosts, /etc/passwd and /etc/group based mappings take precedence.
/etc/nsswitch.conf 行の "files" または "compat" エントリの後に "mymachines" を配置して、DNSなどの他のリゾルバよりもそのマッピングが優先されるようにすることをお勧めします。その場合、/etc/passwd および /etc/group ベースのマッピングが優先されます。
メモ
- manに載っているサンプル↓を見るに、コンテナ名がそのままホスト名として解決される模様。(mdnsのように.localがついたりはしない)
$ ping -c1 rawhide
PING rawhide(fe80::94aa:3aff:fe7b:d4b9%ve-rawhide (fe80::94aa:3aff:fe7b:d4b9%ve-rawhide)) 56 data bytes
64 bytes from fe80::94aa:3aff:fe7b:d4b9%ve-rawhide (fe80::94aa:3aff:fe7b:d4b9%ve-rawhide): icmp_seq=1 ttl=64 time=0.045 ms
...
- Debian 10 buster でsystemd-container, systemd-networkd, systemd-resolved あたりをインストールしたら、
/etc/nsswitch.conf
に勝手に設定されていたので、何もする必要がなかった
vagrant@buster:~$ cat /etc/nsswitch.conf
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.
passwd: files systemd
group: files systemd
shadow: files
gshadow: files
hosts: files dns mymachines
networks: files
protocols: db files
services: db files
ethers: db files
rpc: db files
netgroup: nis
-
systemd-container
を入れるとlibnss-mymachines
が勝手に入る模様
vagrant@buster:~$ dpkg -l |grep libnss
ii libnss-mymachines:amd64 241-5 amd64 nss module to resolve hostnames for local container instances
ii libnss-systemd:amd64 241-5 amd64 nss module providing dynamic user and group name resolution
vagrant@buster:~$ apt-cache rdepends libnss-mymachines
libnss-mymachines
Reverse Depends:
systemd-container