最近遇到了一個(gè)問題。
我們的kube-apiserver配置了OIDC認(rèn)證,OIDC issuer是添加了dns server記錄的,但由于某些原因,我需要覆蓋掉dns server的解析,改用hostAlias的IP地址,但是實(shí)測(cè)發(fā)現(xiàn)總是走了DNS解析,雖然/etc/hosts文件已經(jīng)添加了自定義的hosts記錄。而那些沒有在dns server注冊(cè)的域名,還是可以通過 /etc/hosts 解析的。
原因是,kube-apiserver的基礎(chǔ)鏡像是 busybox ,和 centos 不同,這貨沒有 /etc/nsswitch.conf 文件,所以總是優(yōu)先使用DNS解析,忽略了 /etc/hosts 文件。
解決辦法很簡(jiǎn)單,給鏡像添加 /etc/nsswitch.conf 文件指定解析順序即可,內(nèi)容如下。
1
|
hosts: files dns |
即,files優(yōu)先dns。
順帶完整的理一下linux系統(tǒng)里golang的域名解析。
golang有兩種域名解析方法:內(nèi)置Go解析器;基于cgo的系統(tǒng)解析器。通過環(huán)境變量GODEBUG來配置。
1
2
|
export GODEBUG=netdns=go # force pure Go resolver export GODEBUG=netdns=cgo # force cgo resolver |
默認(rèn)采用的是內(nèi)置Go解析器,因?yàn)楫?dāng)DNS解析阻塞時(shí),內(nèi)置Go解析器只是阻塞了一個(gè)goroutine,而cgo的解析器則是阻塞了一個(gè)操作系統(tǒng)級(jí)別的線程。
1
|
func init() { netGo = true } |
讀取 resolv.conf 失敗則強(qiáng)制使用cgo。
1
2
3
4
5
6
7
8
9
|
confVal.resolv = dnsReadConfig( "/etc/resolv.conf" ) if confVal.resolv.err != nil && !os.IsNotExist(confVal.resolv.err) && !os.IsPermission(confVal.resolv.err) { // If we can't read the resolv.conf file, assume it // had something important in it and defer to cgo. // libc's resolver might then fail too, but at least // it wasn't our fault. confVal.forceCgoLookupHost = true } |
當(dāng)使用內(nèi)置Go解析器時(shí),根據(jù)解析優(yōu)先級(jí)的不同,還會(huì)細(xì)分為下面四種。
1
2
3
4
5
6
7
8
|
const ( // hostLookupCgo means defer to cgo. hostLookupCgo hostLookupOrder = iota hostLookupFilesDNS // files first hostLookupDNSFiles // dns first hostLookupFiles // only files hostLookupDNS // only DNS ) |
當(dāng) /etc/nsswitch.conf 文件不存在或者文件存在但是沒有指定 hosts 字段時(shí),linux下使用的是 hostLookupDNSFiles ,也就是說,dns解析優(yōu)先hosts解析,所以就會(huì)出現(xiàn)開頭出現(xiàn)的問題。
1
2
3
4
5
6
7
8
9
10
11
12
|
nss := c.nss srcs := nss.sources[ "hosts" ] // If /etc/nsswitch.conf doesn't exist or doesn't specify any // sources for "hosts", assume Go's DNS will work fine. if os.IsNotExist(nss.err) || (nss.err == nil && len(srcs) == 0) { if c.goos == "linux" { // glibc says the default is "dns [!UNAVAIL=return] files" // http://www.gnu.org/software/libc/manual/html_node/Notes-on-NSS-Configuration-File.html. return hostLookupDNSFiles } return hostLookupFilesDNS } |
通過 nsswitch.conf 可以指定解析順序。代碼挺簡(jiǎn)單的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
var mdnsSource, filesSource, dnsSource bool var first string for _, src := range srcs { if src.source == "files" || src.source == "dns" { if !src.standardCriteria() { return fallbackOrder // non-standard; let libc deal with it. } if src.source == "files" { filesSource = true } else if src.source == "dns" { dnsSource = true } if first == "" { first = src.source } continue } // Some source we don't know how to deal with. return fallbackOrder } // Cases where Go can handle it without cgo and C thread // overhead. switch { case filesSource && dnsSource: if first == "files" { return hostLookupFilesDNS } else { return hostLookupDNSFiles } case filesSource: return hostLookupFiles case dnsSource: return hostLookupDNS } |
所以指定 hosts: files dns,解析策略就是 hostLookupFilesDNS,即優(yōu)先使用 /etc/hosts 。
詳細(xì)的解析順序請(qǐng)參見 hostLookupOrder。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://ieevee.com/tech/2018/12/16/go-name-resolution.html