1. 智能DNS原理
根据客户端发起请求端的IP,匹配到相应访问的zone,返回指定可以实现流量分流,提高带宽利用率。智能DNS可与上层负载均衡作连动,动态实用高可用。
打个比喻,智能DNS就相当于收费站的路标,可以让货车走一个入口,轿车走另一个入口,ETC收费走ETC口。
2. 智能DNS 配置
2.1 编辑master节点/var/named/chroot/etc/named.conf文件,在include上面添加
#把外来访问的客户端流量分成两个入口group1,group2,可以想像成IDC1,IDC2 acl group1 { #IDC1,192.168.56.13发来的请求分配到IDC1机房去 192.168.56.13; #客户端IP,可以写多个IP,也可以写IP段 }; acl group2 { #ID2,14发来的请求匹配到group2里 192.168.56.14; }; #如果请求的客户端不在group1,group2配置的网段里,则走之前默认的配置
2.2 编辑master节点/var/named/chroot/etc/view.conf为,之前的配置清空
view "GROUP1" { match-clients { group1; }; #匹配group1时,去下边group1.viewdns.com.zone文件下边解析 zone "viewdns.com" { type master; file "group1.viewdns.com.zone"; }; }; view "GROUP2" { match-clients { group2; }; #匹配group2时,去下边group2.viewdns.com.zone文件下边解析 zone "viewdns.com" { type master; file "group2.viewdns.com.zone"; }; };
2.3 创建zone文件
#编辑group1.viewdns.com.zone [root@master etc]# vim group1.viewdns.com.zone $ORIGIN . $TTL 3600 ; 1 hour viewdns.com IN SOA op.viewdns.com. dns.viewdns.com. ( 2005 ; serial 900 ; refresh (15 minutes) 600 ; retry (10 minutes) 86400 ; expire (1 day) 3600 ; minimum (1 hour) ) NS op.viewdns.com. $ORIGIN viewdns.com. op A 192.168.122.1 view A 192.168.122.1 #域名中的一段字符串,没有实际意义 #编辑group2.viewdns.com.zone [root@master etc]# vim group2.viewdns.com.zone $ORIGIN . $TTL 3600 ; 1 hour viewdns.com IN SOA op.viewdns.com. dns.viewdns.com. ( 2005 ; serial 900 ; refresh (15 minutes) 600 ; retry (10 minutes) 86400 ; expire (1 day) 3600 ; minimum (1 hour) ) NS op.viewdns.com. $ORIGIN viewdns.com. op A 192.168.122.2 view A 192.168.122.2
2.4 更改属主,reload
[root@master etc]# cp example.com.zone viewdns.com.zone [root@master etc]# chown named.named /var/named/chroot/etc/group*.zone [root@master etc]# rndc reload
2.5 分别是在两台主机中测试解析结果
#在192.168.56.13这台机器执行,返回解析结果为192.168.122.1 [root@master etc]# host view.viewdns.com 192.168.56.13 Using domain server: Name: 192.168.56.13 Address: 192.168.56.13#53 Aliases: view.viewdns.com has address 192.168.122.1 #在192.168.56.14这台机器执行,返回解析结果为192.168.122.2 [root@slave ~]# host view.viewdns.com 192.168.56.13 Using domain server: Name: 192.168.56.13 Address: 192.168.56.13#53 Aliases: view.viewdns.com has address 192.168.122.2
3. 智能DNS解析流程分析
以192.168.56.13上执行为例
3.1 在13上发起一条view.viewdns.com的域名解析请求
[root@master ~]# host view.viewdns.com 192.168.56.13 Using domain server: Name: 192.168.56.13 Address: 192.168.56.13#53 Aliases: view.viewdns.com has address 192.168.122.1
3.2 首先会到/etc/named.conf,把配置都加载一遍
#里面定义了两个acl组,192.168.56.13属于group1组,当从192.168.56.13发来请求时,走group1组, acl group1 { 192.168.56.13; }; acl group2 { 192.168.56.14; }; #接着加载view.conf文件 include "/var/named/chroot/etc/view.conf";
3.3 加载view.conf文件,找到group1组的配置
[root@master ~]# vim /var/named/chroot/etc/view.conf view "GROUP1" { match-clients { group1; }; #192.168.56.13客户端匹配到group1,则去找下边group1.viewdns.com.zone文件去解析 zone "viewdns.com" { type master; file "group1.viewdns.com.zone"; }; }; view "GROUP2" { match-clients { group2; }; zone "viewdns.com" { type master; file "group2.viewdns.com.zone"; }; };
3.4 zone文件解析
[root@master ~]# cat /var/named/chroot/etc/group1.viewdns.com.zone $ORIGIN . $TTL 3600 ; 1 hour viewdns.com IN SOA op.viewdns.com. dns.viewdns.com. ( 2005 ; serial 900 ; refresh (15 minutes) 600 ; retry (10 minutes) 86400 ; expire (1 day) 3600 ; minimum (1 hour) ) NS op.viewdns.com. $ORIGIN viewdns.com. op A 192.168.122.1 view A 192.168.122.1 #dns发现192.168.56.13客户端请的view.viewdns.com记录zone文件里边有,把解析返回给192.168.56.13
3.5 返回解析结果
[root@master ~]# host view.viewdns.com 192.168.56.13 Using domain server: Name: 192.168.56.13 Address: 192.168.56.13#53 Aliases: view.viewdns.com has address 192.168.122.1
3.6 192.168.56.14请求view.viewdns.com则匹配到group2组,其余流程与上面解析过程一致
[root@slave ~]# host view.viewdns.com 192.168.56.13 Using domain server: Name: 192.168.56.13 Address: 192.168.56.13#53 Aliases: view.viewdns.com has address 192.168.122.2