1.簡介
DNS服務(wù)是域名系統(tǒng)的縮寫, 英文全稱:Domain Name System,將域名和IP地址相互映射。在容器環(huán)境中,DNS至關(guān)重要,
Docker link
Docker link是一個遺留的特性,在新版本的Docker中,一般不推薦使用。簡單來說Docker link就是把兩個容器連接起來,容器可以使用容器名進(jìn)行通信,而不需要依賴ip地址(其實就是在容器的/etc/hosts文件添加了host記錄,原本容器之間的IP就是通的,只是我們增加了host記錄,可以不用IP去訪問)
創(chuàng)建容器centos-1:
[root@host1?~]#?docker?run?-itd?--name?centos-1??registry.cn-shanghai.aliyuncs.com/public-namespace/cr7-centos7-tool:v2
創(chuàng)建容器centos-2,使用--link name:alias,name就是要訪問的目標(biāo)機(jī)器,alias就是自定義的別名。
[root@host1?~]#?docker?run?-itd?--name?centos-2??--link?centos-1:centos-1-alias??registry.cn-shanghai.aliyuncs.com/public-namespace/cr7-centos7-tool:v2
查看容器centos-2的/etc/hosts文件:
[root@host1?~]#?docker?exec?centos-2?cat?/etc/hosts
127.0.0.1???????localhost
::1?????localhost?ip6-localhost?ip6-loopback
fe00::0?ip6-localnet
ff00::0?ip6-mcastprefix
ff02::1?ip6-allnodes
ff02::2?ip6-allrouters
172.18.0.2??????centos-1-alias?9dde6339057a?centos-1??#容器centos-1的host記錄
172.18.0.3??????f1a7e5fa3d96??#容器centos-2自身的host記錄
意味著centos-2可以用centos-1-alias,9dde6339057a,centos-1來訪問原先創(chuàng)建的容器。centos-1是不可以通過hostname訪問centos-2的。
[root@host1 ~]# docker exec centos-2 ping centos-1-alias
PING centos-1-alias (172.18.0.2) 56(84) bytes of data.
64 bytes from centos-1-alias (172.18.0.2): icmp_seq=1 ttl=64 time=0.174 ms
^C
[root@host1 ~]# docker exec centos-2 ping centos-1
PING centos-1-alias (172.18.0.2) 56(84) bytes of data.
64 bytes from centos-1-alias (172.18.0.2): icmp_seq=1 ttl=64 time=1.37 ms
64 bytes from centos-1-alias (172.18.0.2): icmp_seq=2 ttl=64 time=0.523 ms
^C
[root@host1 ~]# docker exec centos-2 ping 9dde6339057a
PING centos-1-alias (172.18.0.2) 56(84) bytes of data.
64 bytes from centos-1-alias (172.18.0.2): icmp_seq=1 ttl=64 time=2.59 ms
64 bytes from centos-1-alias (172.18.0.2): icmp_seq=2 ttl=64 time=3.75 ms
Embedded DNS
從Docker 1.10開始,Docker提供了一個內(nèi)置的DNS服務(wù)器,當(dāng)創(chuàng)建的容器屬于自定義網(wǎng)絡(luò)時,容器的/etc/resolv.conf會使用內(nèi)置的DNS服務(wù)器(地址永遠(yuǎn)是127.0.0.11)來解析相同自定義網(wǎng)絡(luò)內(nèi)的其他容器。
為了向后兼容,default bridge網(wǎng)絡(luò)的DNS配置沒有改變,默認(rèn)的docker網(wǎng)絡(luò)使用的是宿主機(jī)的/etc/resolv.conf的配置。
創(chuàng)建一個自定義網(wǎng)絡(luò):
[root@host1?~]#?docker?network?create?my-network
#bridge,host,none為docker默認(rèn)創(chuàng)建的網(wǎng)絡(luò)
[root@host1?~]#?docker?network?ls
NETWORK?ID??????????NAME????????????????DRIVER??????????????SCOPE
2115f17cd9d0????????bridge??????????????bridge??????????????local
19accfa096cf????????host????????????????host????????????????local
a23c8b371c7f????????my-network??????????bridge??????????????local
0a33edc20fae????????none????????????????null????????????????local
分別創(chuàng)建兩個容器屬于自定義網(wǎng)絡(luò)my-network中:
[root@host1?~]#?docker?run?-itd?--name?centos-3?--net?my-network??registry.cn-shanghai.aliyuncs.com/public-namespace/cr7-centos7-tool:v2?
[root@host1?~]#?docker?run?-itd?--name?centos-4?--net?my-network??registry.cn-shanghai.aliyuncs.com/public-namespace/cr7-centos7-tool:v2??
查看容器centos-4的/etc/hosts和/etc/resolv.conf,可以看到nameserver添加的IP為127.0.0.11的Embedded DNS:
#/etc/hosts中沒有配置對方的host記錄
[root@host1?~]#?docker?exec?centos-4?cat?/etc/hosts
127.0.0.1???????localhost
::1?????localhost?ip6-localhost?ip6-loopback
fe00::0?ip6-localnet
ff00::0?ip6-mcastprefix
ff02::1?ip6-allnodes
ff02::2?ip6-allrouters
172.19.0.3??????555281f37ea3
#/etc/resolv.conf配置了dns服務(wù)器127.0.0.11
[root@host1?~]#?docker?exec?centos-4?cat?/etc/resolv.conf
nameserver?127.0.0.11
options?ndots:0
此時centos-3和centos-4可以互相解析:
[root@host1 ~]# docker exec centos-4 ping centos-3
PING centos-3 (172.19.0.2) 56(84) bytes of data.
64 bytes from centos-3.my-network (172.19.0.2): icmp_seq=1 ttl=64 time=0.128 ms
64 bytes from centos-3.my-network (172.19.0.2): icmp_seq=2 ttl=64 time=0.078 ms
64 bytes from centos-3.my-network (172.19.0.2): icmp_seq=3 ttl=64 time=0.103 ms
^C
[root@host1 ~]# docker exec centos-3 ping centos-4
PING centos-4 (172.19.0.3) 56(84) bytes of data.
64 bytes from centos-4.my-network (172.19.0.3): icmp_seq=1 ttl=64 time=0.087 ms
64 bytes from centos-4.my-network (172.19.0.3): icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from centos-4.my-network (172.19.0.3): icmp_seq=3 ttl=64 time=0.076 ms
^C
Docker DNS配置
方式一:docker run (針對單個容器)
Flag | Description |
---|---|
--dns | 指定DNS服務(wù)器地址,如果容器不能訪問指定的所有ip地址,則會使用8.8.8.8作為DNS服務(wù)器地址(Docker默認(rèn)定義的) |
--dns-search | 當(dāng)容器訪問一個不包括完全域名的主機(jī)名時,在該主機(jī)名后面添加dns-search指定的域名后綴,例如容器訪問centos-1,dns-search配置的是example.com,則會解析成centos-1.example.com |
--dns-opt | options ndots:5的含義是當(dāng)查詢的域名字符串內(nèi)的點字符數(shù)量大于等于ndots值(5)時,則認(rèn)為是完整域名,直接解析,不會走 search 域 |
--hostname | 指定容器hostname |
方式二:daemon.json
nameserver只針對docker默認(rèn)網(wǎng)絡(luò)所有容器,dns-search和dns-opts針對所有網(wǎng)絡(luò)容器。
{
?????"dns":?["114.114.114.114","223.5.5.5"],
?????"dns-opts":["ndots:5"],
?????"dns-search":["example.com"]
}?
Kubernetes DNS
文章來源:http://www.zghlxwxcb.cn/news/detail-791018.html
在kubernetes中,有以下4中DNS策略,可以通過dnsPolicy指定:文章來源地址http://www.zghlxwxcb.cn/news/detail-791018.html
到了這里,關(guān)于Docker 容器的DNS的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!