DNS 포워딩 활성화

DNS 포워딩 활성화

Consul 서버로 DNS 포워딩을 활성화하는 과정을 설명하는 페이지예요. Consul 에이전트가 실행되는 모든 노드에 이 작업을 적용할 수 있어요.

출처: 문서

본문

이 페이지에서는 Consul 서버로 DNS 포워딩을 활성화하는 과정을 설명합니다.

Consul 에이전트가 실행되는 모든 노드에 이러한 작업을 적용할 수 있습니다.

요구 사항 (Requirements)

DNS 포워딩을 활성화하려면 배포에 다음이 있어야 합니다.

  • 실행 중인 Consul 서버 인스턴스
  • Consul 카탈로그에 등록된 서비스가 있는 하나 이상의 Consul 클라이언트 노드
  • iptables 명령 사용 가능, 또는 다음 로컬 DNS 서버 중 하나: systemd-resolved, BIND, Dnsmasq, Unbound, macOS 시스템 리졸버

네트워크 주소 구성 (Network address configuration)

이 페이지의 예시 구성은 Consul의 DNS 서버가 로컬 DNS 서버와 같은 노드의 루프백 인터페이스에서 수신 대기한다고 가정합니다.

Consul이 루프백 IP에서 수신 대기하지 않는다면 구성과 명령에서 localhost와 120.0.0.1 참조를 환경에 맞는 적절한 IP 주소로 바꾸세요.

systemd-resolved

systemd-resolved는 로컬 애플리케이션에 네트워크 이름 해석을 제공하는 시스템 서비스입니다. 많은 Linux 배포판의 기본 로컬 DNS 서버입니다.

systemd-resolved 서비스가 .consul 도메인 쿼리를 Consul로 보내도록 구성하려면 /etc/systemd/resolved.conf.d/ 디렉터리에 있는 consul.conf 파일을 만드세요.

resolved 구성에 [Resolve] 섹션을 추가하세요.

/etc/systemd/resolved.conf.d/consul.conf[Resolve]
DNS=127.0.0.1
DNSSEC=false
Domains=~consul

Consul DNS 서버 포트 정의 (Define port for Consul DNS server)

systemd 245 이하를 사용할 때는 DNS 구성 필드에서 포트 번호를 지정할 수 없습니다. systemd-resolved는 권한 있는 포트인 포트 53만 사용합니다.

시스템 구성에서 포트를 지정할 수 없을 때는 두 가지 해결 방법이 있습니다.

  • Consul DNS 서비스가 8600 대신 포트 53에서 수신 대기하도록 구성합니다.
  • iptables로 포트 53을 8600에 매핑합니다.

포트 53에 바인딩하는 것은 일반적으로 Consul을 권한 있는 사용자로 실행하거나 CAP_NET_BIND_SERVICE 기능으로 Linux를 실행해야 합니다.

Consul Docker 이미지를 사용할 때는 환경에 다음을 추가해 Consul이 해당 포트를 사용할 수 있게 하세요. CONSUL_ALLOW_PRIVILEGED_PORTS=yes.

Consul을 권한 있는 사용자로 실행하지 않으려면 다음 iptables 명령으로 포트 53을 8600에 매핑하고 DNS 쿼리를 Consul로 리디렉션하는 것으로 충분합니다.

# iptables --table nat --append OUTPUT --destination localhost --protocol udp --match udp --dport 53 --jump REDIRECT --to-ports 8600 && \
  iptables --table nat --append OUTPUT --destination localhost --protocol tcp --match tcp --dport 53 --jump REDIRECT --to-ports 8600

Systemd 246 이상에서는 systemd-resolved 구성 파일에서 DNS 포트를 직접 지정할 수 있습니다.

이전 버전의 systemd는 DNS 트래픽을 Consul로 보내려면 iptables 규칙이 필요했습니다.

resolved 구성에 [Resolve] 섹션을 추가하세요.

/etc/systemd/resolved.conf.d/consul.conf[Resolve]
DNS=127.0.0.1:8600
DNSSEC=false
Domains=~consul

PTR 레코드 쿼리는 Consul 외에도 다른 구성된 리졸버로 계속 전송됩니다.

resolved 구성을 만든 후 systemd-resolved를 다시 시작하세요.

# systemctl restart systemd-resolved

이 명령은 출력을 생성하지 않습니다.

systemd-resolved 구성 검증 (Validate the systemd-resolved configuration)

systemd-resolved가 활성 상태인지 검증하세요.

# systemctl is-active systemd-resolved
active

systemd-resolved가 consul 도메인에 대한 쿼리를 Consul로 포워딩하도록 구성되었는지 확인하세요.

# resolvectl domain
Global: ~consul
Link 2 (eth0):

systemd-resolved가 Consul 서버 주소를 해석할 수 있는지 확인하세요.

# resolvectl query consul.service.consul
consul.service.consul: 127.0.0.1

-- Information acquired via protocol DNS in 6.6ms.
-- Data is authenticated: no

/etc/resolv.conf가 systemd-resolved가 관리하는 stub-resolv.conf 파일을 가리키는지 확인하세요.

$ ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 37 Jul 14 10:10 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf

systemd-resolved의 스텁 리졸버의 IP 주소가 구성된 nameserver인지 확인하세요.

$ cat /etc/resolv.conf
## This file is managed by man:systemd-resolved(8). Do not edit.
##
## This is a dynamic resolv.conf file for connecting local clients to the
## internal DNS stub resolver of systemd-resolved. This file lists all
## configured search domains.
##
## Run "resolvectl status" to see details about the uplink DNS servers
## currently in use.
##
## Third party programs must not access this file directly, but only through the
## symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
## replace this symlink by a static file or a different symlink.
##
## See man:systemd-resolved.service(8) for details about the supported modes of
## operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0

운영 체제가 .consul 도메인에 대한 DNS 쿼리를 해석할 수 있는지 확인하세요.

$ host consul.service.consul
consul.service.consul has address 127.0.0.1

systemd와 함께 로컬 리졸버 사용 (Using any local resolver with systemd)

기본적으로 resolved.conf 파일의 로컬 리졸버 스텁은 127.0.0.53:53에서 UDP와 TCP 요청을 수신 대기하도록 구성되어 있습니다. 그러나 DNSStubListener 옵션을 false로 설정하면 시스템이 resolved보다 먼저 로드되는 한 모든 DNS 구성을 사용할 수 있습니다.

/etc/systemd/resolved.confDNSStubListener=false

Docker용 systemd-resolved 구성 (Configuring systemd-resolved for Docker)

기본적으로 Docker는 localhost 주소를 사용하는 호스트 파일을 자체 DNS 설정으로 대체합니다. 이 동작은 systemd-resolved와 Docker 컨테이너에서 문제를 일으킬 수 있습니다.

Docker가 호스트 DNS 설정을 대체하지 못하게 하려면 systemd-resolved에서 스텁 리졸버 주소를 구성하고 /etc/docker/daemon.json 파일의 dns 옵션을 해당 주소로 설정하세요.

/etc/systemd/resolved.conf.d/consul.conf[Resolve]
DNS=127.0.0.1:8600
DNSSEC=false
Domains=~consul
DNSStubListener=yes
DNSStubListenerExtra=172.17.0.1
/etc/docker/daemon.json{
  "dns": ["172.17.0.1"]
}

이러한 변경 후 systemd-resolved와 docker 서비스를 다시 시작하세요.

Dnsmasq

네트워크가 작고 가벼운 DNS 솔루션이 필요하다면 dnsmasq를 사용하세요.

참고 배포판이 systemd를 사용한다면 다음 단계를 따르기 전에 systemd-resolved를 비활성화하세요.

/etc/dnsmasq.d 디렉터리에 dnsmasq.conf 파일 또는 일련의 파일을 구성하세요. consul 도메인에 대한 요청이 Consul DNS로 포워딩되도록 구성 파일에 서버 설정을 추가하세요.

/etc/dnsmasq.d/10-consul# Enable forward lookup of the 'consul' domain:
server=/consul/127.0.0.1#8600

# Uncomment and modify as appropriate to enable reverse DNS lookups for
# common netblocks found in RFC 1918, 5735, and 6598:
#rev-server=0.0.0.0/8,127.0.0.1#8600
#rev-server=10.0.0.0/8,127.0.0.1#8600
#rev-server=100.64.0.0/10,127.0.0.1#8600
#rev-server=127.0.0.1/8,127.0.0.1#8600
#rev-server=169.254.0.0/16,127.0.0.1#8600
#rev-server=172.16.0.0/12,127.0.0.1#8600
#rev-server=192.168.0.0/16,127.0.0.1#8600
#rev-server=224.0.0.0/4,127.0.0.1#8600
#rev-server=240.0.0.0/4,127.0.0.1#8600
# Accept DNS queries only from hosts whose address is on a local subnet.
#local-service
# Don't poll /etc/resolv.conf for changes.
#no-poll
# Don't read /etc/resolv.conf. Get upstream servers only from the command
# line or the dnsmasq configuration file (see the "server" directive below).
#no-resolv
# Specify IP address(es) of other DNS servers for queries not handled
# directly by consul. There is normally one 'server' entry set for every
# 'nameserver' parameter found in '/etc/resolv.conf'. See dnsmasq(8)'s
# 'server' configuration option for details.
#server=1.2.3.4
#server=208.67.222.222
#server=8.8.8.8
# Set the size of dnsmasq's cache. The default is 150 names. Setting the
# cache size to zero disables caching.
#cache-size=65536

구성을 만든 후 dnsmasq 서비스를 다시 시작하세요.

Consul이 직접 처리하지 않는 쿼리에 대한 IP 주소 지정과 같은 추가 구성 설정은 dnsmasq(8)를 참조하세요.

BIND

BIND는 견고한 DNS 시스템입니다. 가장 두드러진 구성 요소인 named는 두 가지 주요 DNS 서버 역할을 모두 수행하며, DNS 존의 권한 있는(authoritative) 네임 서버 역할과 네트워크의 재귀 리졸버 역할을 합니다.

참고 배포판이 systemd를 사용한다면 다음 단계를 따르기 전에 systemd-resolved를 비활성화하세요.

BIND 서비스가 .consul 도메인 쿼리를 Consul로 보내도록 구성하려면:

  • DNSSEC가 비활성화된 named 구성 파일을 만듭니다.
  • .consul 도메인을 관리하는 존(zone) 구성 파일을 만듭니다.

Named 구성 파일 (Named configuration file)

/etc/named.conf를 편집해 BIND 인스턴스를 구성하세요. Consul과 BIND가 통신할 수 있도록 DNSSEC를 비활성화하는 것을 잊지 마세요. 다음 단계에서 만드는 존 파일을 포함하도록 include 섹션을 추가하세요.

다음 예시는 DNSSEC가 비활성화된 BIND 구성을 보여줍니다.

/etc/named.confoptions {
  listen-on port 53 { 127.0.0.1; };
  listen-on-v6 port 53 { ::1; };
  directory       "/var/named";
  dump-file       "/var/named/data/cache_dump.db";
  statistics-file "/var/named/data/named_stats.txt";
  memstatistics-file "/var/named/data/named_mem_stats.txt";
  allow-query     { localhost; };
  recursion yes;

  dnssec-enable no;
  dnssec-validation no;

  /* Path to ISC DLV key */
  bindkeys-file "/etc/named.iscdlv.key";

  managed-keys-directory "/var/named/dynamic";
};

include "/etc/named/consul.conf";

존 구성 파일 (Zone configuration file)

consul.conf에서 Consul이 관리하는 레코드에 대한 존을 설정하세요.

/etc/named/consul.confzone "consul" IN {
  type forward;
  forward only;
  forwarders { 127.0.0.1 port 8600; };
};

Unbound

Linux와 macOS용으로 빠르고 간결한 DNS 리졸버가 필요할 때 Unbound를 사용하세요.

참고 배포판이 systemd를 사용한다면 다음 단계를 따르기 전에 systemd-resolved를 비활성화하세요.

다음 예시는 /etc/unbound/unbound.conf.d 디렉터리의 consul.conf 파일에 대한 구성을 보여줍니다.

Unbound 구성 파일에 server와 stub-zone 설정을 추가하세요.

/etc/unbound/unbound.conf.d/consul.conf#Allow insecure queries to local resolvers
server:
  do-not-query-localhost: no
  domain-insecure: "consul"

#Add consul as a stub-zone
stub-zone:
  name: "consul"
  stub-addr: 127.0.0.1@8600

새 구성이 포함되도록 /etc/unbound/unbound.conf 파일 하단에 다음 줄을 추가해야 할 수 있습니다.

/etc/unbound/unbound.confinclude: "/etc/unbound/unbound.conf.d/*.conf"

iptables

iptables는 시스템에 대한 트래픽 규칙을 정의할 수 있는 일반적인 방화벽 소프트웨어입니다.

Consul 에이전트 노드에 로컬 DNS 서버가 없다면 iptables를 사용해 포트 53의 DNS 요청을 보조 서비스 없이 같은 머신에서 실행되는 Consul 에이전트로 포워딩할 수 있습니다.

이 구성은 전체 DNS 포워딩을 구현하므로 호스트에 대한 모든 DNS 쿼리가 .consul 최상위 도메인에 대한 것뿐만 아니라 Consul로 포워딩됩니다. Consul의 기본 구성은 .consul 최상위 도메인만 해석하므로 iptables 구성을 사용할 때 노드가 다른 도메인도 해석할 수 있게 하려면 recursors 플래그를 설정해야 합니다.

네트워크에서 DNS 릴레이 호스트를 사용한다면 이를 Consul과 같은 호스트에 두지 마세요. 리디렉션이 트래픽을 가로챌 수 있습니다.

Consul recursors 구성 (Configure Consul recursors)

Consul 구성에 recursors를 추가하세요.

/etc/consul.d/consul.hcl# DNS recursors
recursors = [ "1.1.1.1" ]

iptables 리디렉션이 요청을 가로챌 수 있으므로 recursors에 localhost 주소를 포함하면 안 됩니다.

예시의 1.1.1.1 주소를 다른 DNS 서버 주소로 바꿀 수 있습니다. 이는 외부 DNS 서비스가 이미 인프라에서 실행 중이고 recursor로 사용되는 상황에 적합합니다.

iptables 규칙 생성 (Create iptables rules)

Consul이 유효한 recursor를 사용하도록 구성한 후 iptables에 규칙을 추가해 포트 53에서 포트 8600으로 트래픽을 리디렉션하세요.

# iptables -t nat -A PREROUTING -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600 \
  iptables -t nat -A PREROUTING -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600 \
  iptables -t nat -A OUTPUT -d localhost -p udp -m udp --dport 53 -j REDIRECT --to-ports 8600 \
  iptables -t nat -A OUTPUT -d localhost -p tcp -m tcp --dport 53 -j REDIRECT --to-ports 8600

macOS

macOS 시스템에서는 macOS 시스템 리졸버를 사용해 모든 .consul 요청을 Consul로 지정하세요.

man 5 resolver 명령이 이 기능을 자세히 설명합니다.

다음 지침은 sudo 또는 root 액세스 권한을 요구합니다.

macOS 시스템 리졸버가 DNS 쿼리를 Consul로 포워딩하도록 구성하려면 Consul 에이전트를 가리키는 /etc/resolver/ 디렉터리에 리졸버 항목을 추가하세요.

이 폴더가 없으면 만드세요.

# mkdir -p /etc/resolver

nameserver와 port 항목이 있는 새 파일 /etc/resolver/consul을 만드세요.

/etc/resolver/consulnameserver 127.0.0.1
port 8600

이 구성은 macOS 리졸버 데몬에 모든 .consul TLD 요청을 포트 8600의 127.0.0.1로 포워딩하도록 알립니다.

다음 단계 (Next steps)

이 페이지의 지침은 노드가 DNS 요청을 Consul로 포워딩하도록 구성하는 데 도움이 되었습니다.

포워딩이 활성화된 후 Consul DNS를 쿼리하는 방법에 대해 자세히 알아보려면 DNS 포워딩 워크플로를 참조하세요.

Consul에서 제공하는 다른 DNS 기능과 구성에 대한 자세한 내용은 DNS 사용 개요를 참조하세요.

더 알아보기 (Learn more)