linux中,如何在/etc/hosts中将一个域名解析为多个IP地址?工作原理是什么?
可以在/etc/hosts文件中,将一个域名配置多个IP地址
比如:
[root@nccztsjb-node-23 yamls]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.20.58.83 nccztsjb-node-23 www.website.com 172.20.58.18 www.website.com 12.20.58.65 www.website.com [root@nccztsjb-node-23 yamls]#
这个时候进行ping测试,发现每次ping都是解析第一个地址
[root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (172.20.58.83) 56(84) bytes of data. 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=1 ttl=64 time=0.048 ms 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=2 ttl=64 time=0.045 ms ^C --- nccztsjb-node-23 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.045/0.046/0.048/0.007 ms [root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (172.20.58.83) 56(84) bytes of data. 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=1 ttl=64 time=0.047 ms 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=2 ttl=64 time=0.087 ms ^C --- nccztsjb-node-23 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.047/0.067/0.087/0.020 ms [root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (172.20.58.83) 56(84) bytes of data. 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=1 ttl=64 time=0.060 ms 64 bytes from nccztsjb-node-23 (172.20.58.83): icmp_seq=2 ttl=64 time=0.057 ms ^C --- nccztsjb-node-23 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.057/0.058/0.060/0.007 ms [root@nccztsjb-node-23 yamls]#
这就说到这个工作原理了:
只有当第一个无法解析的时候,才会解析第2个IP
比如:将第一个IP地址,写一个无法解析的地址,那么就解析到第2个IP了
[root@nccztsjb-node-23 yamls]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 12.20.58.83 nccztsjb-node-23 www.website.com 172.20.58.18 www.website.com 12.20.58.65 www.website.com [root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (172.20.58.18) 56(84) bytes of data. 64 bytes from www.website.com (172.20.58.18): icmp_seq=1 ttl=64 time=0.838 ms 64 bytes from www.website.com (172.20.58.18): icmp_seq=2 ttl=64 time=0.350 ms 64 bytes from www.website.com (172.20.58.18): icmp_seq=3 ttl=64 time=0.289 ms ^C --- nccztsjb-node-23 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.289/0.492/0.838/0.246 ms [root@nccztsjb-node-23 yamls]#
将第2个IP地址也修改为一个无法解析的IP地址,就解析到第3个了
[root@nccztsjb-node-23 yamls]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 12.20.58.83 nccztsjb-node-23 www.website.com 12.20.58.18 www.website.com 172.20.58.65 www.website.com [root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (172.20.58.65) 56(84) bytes of data. 64 bytes from www.website.com (172.20.58.65): icmp_seq=1 ttl=64 time=0.379 ms 64 bytes from www.website.com (172.20.58.65): icmp_seq=2 ttl=64 time=0.315 ms ^C --- nccztsjb-node-23 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.315/0.347/0.379/0.032 ms [root@nccztsjb-node-23 yamls]#
将3个IP地址,都换成无法ping通的地址,这个时候,就是还是解析第一个地址,只不过是丢包的
[root@nccztsjb-node-23 yamls]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 12.20.58.83 nccztsjb-node-23 www.website.com 12.20.58.18 www.website.com 12.20.58.65 www.website.com [root@nccztsjb-node-23 yamls]# ping www.website.com PING nccztsjb-node-23 (12.20.58.83) 56(84) bytes of data. ^C --- nccztsjb-node-23 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 999ms [root@nccztsjb-node-23 yamls]#
所以,这里的解析的逻辑,本质上是,第一个能ping通,就返回第一个,第一个ping不通,就下一个,依次类推,间接的实现了高可用了,但是,不是负载均衡
如果都不通,解析的还是第一个地址