国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Golang > 正文

golang解析域名的步驟全紀(jì)錄

2020-04-01 18:50:41
字體:
供稿:網(wǎng)友

最近遇到了一個問題。

我們的kube-apiserver配置了OIDC認證,OIDC issuer是添加了dns server記錄的,但由于某些原因,我需要覆蓋掉dns server的解析,改用hostAlias的IP地址,但是實測發(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 resolverexport GODEBUG=netdns=cgo # force cgo resolver

默認采用的是內(nèi)置Go解析器,因為當(dāng)DNS解析阻塞時,內(nèi)置Go解析器只是阻塞了一個goroutine,而cgo的解析器則是阻塞了一個操作系統(tǒng)級別的線程。

func init() { netGo = true }

讀取 resolv.conf 失敗則強制使用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解析器時,根據(jù)解析優(yōu)先級的不同,還會細分為下面四種。

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 字段時,linux下使用的是 hostLookupDNSFiles ,也就是說,dns解析優(yōu)先hosts解析,所以就會出現(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 。

詳細的解析順序請參見 hostLookupOrder

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 游戏| 手游| 丰都县| 霍州市| 略阳县| 毕节市| 乐昌市| 仁寿县| 赣州市| 洱源县| 丰都县| 清水县| 北海市| 靖西县| 金湖县| 沂源县| 武威市| 密山市| 三穗县| 藁城市| 长葛市| 正安县| 明星| 稷山县| 县级市| 太和县| 西林县| 康马县| 四川省| 茶陵县| 中江县| 郧西县| 晋城| 苏尼特左旗| 和田县| 滨州市| 鄂托克旗| 萍乡市| 襄樊市| 永定县| 巨野县|