golang解析域名的步驟全紀(jì)錄
最近遇到了一個(gè)問題。
我們的kube-apiserver配置了OIDC認(rèn)證,OIDC issuer是添加了dns server記錄的,但由于某些原因,我需要覆蓋掉dns server的解析,改用hostAlias的IP地址,但是實(shí)測發(fā)現(xiàn)總是走了DNS解析,雖然/etc/hosts文件已經(jīng)添加了自定義的hosts記錄。而那些沒有在dns server注冊的域名,還是可以通過 /etc/hosts 解析的。
原因是,kube-apiserver的基礎(chǔ)鏡像是 busybox ,和 centos 不同,這貨沒有 /etc/nsswitch.conf 文件,所以總是優(yōu)先使用DNS解析,忽略了 /etc/hosts 文件。
解決辦法很簡單,給鏡像添加 /etc/nsswitch.conf 文件指定解析順序即可,內(nèi)容如下。
hosts: files dns
即,files優(yōu)先dns。
順帶完整的理一下linux系統(tǒng)里golang的域名解析。
golang有兩種域名解析方法:內(nèi)置Go解析器;基于cgo的系統(tǒng)解析器。通過環(huán)境變量GODEBUG來配置。
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)級別的線程。
func init() { netGo = true }
讀取 resolv.conf 失敗則強(qiáng)制使用cgo。
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)先級的不同,還會(huì)細(xì)分為下面四種。
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)的問題。
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 可以指定解析順序。代碼挺簡單的。
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ì)的解析順序請參見 hostLookupOrder。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解GO語言中[]byte與string的兩種轉(zhuǎn)換方式和底層實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了GO語言中[]byte與string的兩種轉(zhuǎn)換方式和底層實(shí)現(xiàn)的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-03-03Go語言中slice作為參數(shù)傳遞時(shí)遇到的一些“坑”
這篇文章主要給大家介紹了關(guān)于Go語言中slice作為參數(shù)傳遞時(shí)遇到的一些“坑”,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Golang實(shí)現(xiàn)web文件共享服務(wù)的示例代碼
這篇文章主要介紹了Golang實(shí)現(xiàn)web文件共享服務(wù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10