[Network] DNS (Domain Name System) 原理

Outline

DNS Wiki

Root Name Servers


Theory

Space

RFC 1034 – Iterative 查詢標準:
Resolver

DNS resolution 順序:
DNS resolution sequence


Resolution Process

Resolution Process

(圖片來源:Web Service Principles – DNS)


Round-robin DNS

Round-robin DNS Wiki (同個 A Record 設定多個 IP Answers)

舉例來說:

$ nslookup -type=a a-record-with-2ip.local 8.8.8.8
Server:      8.8.8.8
Address:   8.8.8.8#53

Non-authoritative answer:
Name:   a-record-with-2ip.local
Address: 139.162.80.101
Name:   a-record-with-2ip.local
Address: 2.2.2.2

Client retry 機制

完整實測結果參考:Round-robin DNS – Client retry mechanism

CURL實測有兩個IP ANSWER的A Record網域,其中第一次訪問的IP失效:

$ curl --connect-timeout 5 -m 25 "a-record-with-2ip.local" -vvv
* STATE: INIT => CONNECT handle 0x600057990; line 1410 (connection #-5000)
* Rebuilt URL to: a-record-with-2ip.local/
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x600057990; line 1446 (connection #0)
*   Trying 8.8.8.8...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x600057990; line 1527 (connection #0)
* After 2450ms connect time, move on!
* connect to 8.8.8.8 port 80 failed: Connection timed out
*   Trying 139.162.80.137...
* TCP_NODELAY set
* Connected to a-record-with-2ip.local (139.162.80.137) port 80 (#0)
...(Omitted)  
* Expire cleared

另外經CURL實測有三個 IP ANSWER 的 A Record 網域,其中前兩次訪問的IP失效。
實測結果為機制上會保留目前所剩一半的 Connection Timeout 給每一次 IP 連線嘗試:

$ curl --connect-timeout 20 "a-record-with-3ip.local" -vvv                                         
* STATE: INIT => CONNECT handle 0x6000579a0; line 1410 (connection #-5000)            
* Rebuilt URL to: a-record-with-3ip.local/                                                      
* Added connection 0. The cache now contains 1 members                                
* STATE: CONNECT => WAITRESOLVE handle 0x6000579a0; line 1446 (connection #0)         
*   Trying 2.2.2.2...                                                                 
* TCP_NODELAY set                                                                     
* STATE: WAITRESOLVE => WAITCONNECT handle 0x6000579a0; line 1527 (connection #0)     
* After 9968ms connect time, move on!                                                 
* connect to 2.2.2.2 port 80 failed: Connection timed out                             
*   Trying 8.8.8.8...                                                                 
* TCP_NODELAY set                                                                     
* After 4977ms connect time, move on!                                                 
* connect to 8.8.8.8 port 80 failed: Connection timed out                             
*   Trying 139.162.80.137...                                                          
* TCP_NODELAY set                                                                     
* Connected to a-record-with-3ip.local (139.162.80.137) port 80 (#0)                            
...(Omitted)                                  
* Expire cleared

使用Apache HttpClientsetConnectTimeout10 實測結果為Timeout會隨著每一次retry遞增:

// logback in console
18:25:06.282 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connecting to a-record-with-3ip.local/8.8.8.8:80
18:25:16.286 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connect to a-record-with-3ip.local/8.8.8.8:80 timed out. Connection will be retried using another IP address
18:25:16.286 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connecting to a-record-with-3ip.local/2.2.2.2:80
18:25:26.291 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connect to a-record-with-3ip.local/2.2.2.2:80 timed out. Connection will be retried using another IP address
18:25:26.291 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connecting to a-record-with-3ip.local/139.162.80.137:80
18:25:26.327 [main] DEBUG org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator - http-outgoing-0 connection established 10.30.20.151:64386<->139.162.80.137:80

Stackoverflow – Ignore DNS Round-Robin for a Reliable Timeout with Spring-ws and 4.5.x Http Client


查看 Client DSN Resolver

Linux:

$ cat /etc/resolv.conf
nameserver 10.162.72.5
nameserver 10.162.74.5

Windows:

$ ipconfig /all
DNS Servers . . . . . . . . . . . : 192.168.1.1
                                    192.168.1.1

Mac OS:

$ scutil --dns | grep 'nameserver\[[0-9]*\]'

Glossary

Top Level Domain (TLD) 頂級域名分類
Generic Top Level Domain (gTLD) 通用頂級域名:.com / .edu / .gov / .mil / .net / .org
Country code top-level domain (ccTLD) 國家頂級域名:.tw / .hk / .us
New gTLD 新頂級域名: .taipei / .coffee

Leave a Reply

Your email address will not be published. Required fields are marked *