golang解析域名的步驟全紀(jì)錄
最近遇到了一個(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 文件。
解決辦法很簡單,給鏡像添加 /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)級(jí)別的線程。
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)先級(jí)的不同,還會(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ì)的解析順序請(qǐng)參見 hostLookupOrder。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
go語言調(diào)用c語言的so動(dòng)態(tài)庫的實(shí)現(xiàn)
在Go語言開發(fā)過程中,有時(shí)需要調(diào)用C或C++編寫的so動(dòng)態(tài)庫,本文介紹了如何在Go語言中調(diào)用so庫的步驟和注意事項(xiàng),包括環(huán)境準(zhǔn)備、編譯生成.so文件、Go文件編寫、以及可能遇到的問題和解決方法,感興趣的可以了解一下2024-10-10
自己動(dòng)手用Golang實(shí)現(xiàn)約瑟夫環(huán)算法的示例
這篇文章主要介紹了自己動(dòng)手用Golang實(shí)現(xiàn)約瑟夫環(huán)算法的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Golang defer延遲語句的實(shí)現(xiàn)
defer擁有注冊(cè)延遲調(diào)用的機(jī)制,本文主要介紹了Golang defer延遲語句的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Go語言實(shí)現(xiàn)服務(wù)端消息接收和發(fā)送
這篇文章主要為大家詳細(xì)介紹了Go語言實(shí)現(xiàn)服務(wù)端消息接收和發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

