dns view

什么是DNS VIEW
以某种特殊的方式根据用户来源的不同而返回不同的查询结果,这个技术在CDN中应用相当多,解决目前贷款小,和延时大问题的一种方法
view “internal” { #internal 是区域名称,可以自定义,但必须是唯一
};

区分区域是通过match-clients 关键字来实现的
比如:
view “internal” {
match­clients { 192.168.0.0/24; };
};
上面这段表示只处理192.168.0.0/24网段的请求

match-clients 可能需要定义非常多的网段,bind引入acl关键字来定义变量替换,以使match-clients 中仅仅出现最少的符号,
而网段的增加可以在外部文件中进行
如:
acl "foo­subnet" { 192.168.1/24;192.168.2/24; };
view "internal" {
match­clients { "foo­subnet"; };
};


例子:简单的viewnamed.conf示例如下:
options {
directory "/var/named";
};
acl "foo­subnet" { 192.168.0/25; };
view "internal" {  // internal view of our zones
match­clients { "foo­subnet"; };
zone "zhoutao.com" {
type master;
file "zhoutao.com.0­127.zone";
};     
};
view "external" {  // view of our zones for the rest
of the world
match­clients { any; };  // implicit
recursion no;            // outside of our
subnet, they shouldn't be
// requesting recursion
zone "zhoutao.com" {
type master;
file "zhoutao.external.zone";  // external
zone data file
};   
};
范例解释:
任何来自192.186.0.1~192.168.0.127 之间ip地址用户的dns请求都会在internal视图中处理,如果请求不来自internal视图
规定的区域,那么请求会向下选取其他视图对比,所有,视图的对比是自上而下的,如果请求的区域在上一个视图中,就不会向下一个视图
请求,即便你在下一个视图中放入了这个区域

view的从属服务器配置问题
因为在主服务器中划分了视图,不同来源区域被分配到不同的视图中处理,
如果从属服务器只有一个 ip 地址,从属服务器就只能同步主服务器中的单个
视图中的域名的域文件。所以从属服务器需要与主服务器的视图数量相匹配的
ip 地址数量。

另外默认情况下从属服务器是使用本地第一网卡绑定的第一 ip 地址发送同
步请求的,如果不做调整,从属服务器视图中仅会同步本地第一网卡绑定的第
一 ip 地址在主服务器上所处的那个视图的信息。这个时候我们需要在从属服
务器的视图中设置 transfer­source 关键字。

示例:一个从属服务器视named.conf的例子

options {
directory "/var/named";
};
acl "foo­subnet" { 192.168.0/25; };
view "internal" {  // internal view of our zones
match­clients { "foo­subnet"; };
transfer­source 192.168.0.1;
zone "zhoutao.com" {
type slave ;
masters {192.168.0.254;};
file "zhoutao.com.0­127.zone";
};     
};
view "external" {  // view of our zones for the rest
of the world
match­clients { any; };  // implicit
transfer­source 192.168.0.201;
recursion no;            // outside of our
subnet, they shouldn't be
// requesting recursion
zone "zhoutao.com" {
type slave;\
masters { 192.168.0.254; };
file "zhoutao.external.zone";  // external
zone data file
};   
};
192.168.0.1 和 192.168.0.201 都是从属服务器本地的 ip 地址。同时
在视图中它们又被分到不同的视图。

bind view实验
模拟南北电信网通用户
比如www.zhoutao.com这个网站,为了提高电信网通2个网络上的客户体验,使用户体验最快速的访问速度,决定为电信和网通分别架设
2台服务器,其中一台接入电信,一台接入网通,让用户端透明的访问此网站,不需要用户进行人工的网站选择,采用dns服务器view功能
让不同的ip地址指向在不同的网络上的主机
如浏览www.zhoutao.com时,网通用户访问网通的主机比如192.168.3.128,电信用户访问电信的主机如192.168.3.129
而其他未知的网络用户访问192.168.3.130这台主机
实验环境
192.168.3.128(主DNS服务器)
192.168.3.129(电信)
192.168.3.130(网通)


安装bind
yum install bind bind-utils bind-chroot


修改named.conf 配置文件
 

 
  
  1. // 
  2. // Sample named.conf BIND DNS server 'named' configuration file 
  3. // for the Red Hat BIND distribution. 
  4. // 
  5. // See the BIND Administrator's Reference Manual (ARM) for details, in: 
  6. //   file:///usr/share/doc/bind-*/arm/Bv9ARM.html 
  7. // Also see the BIND Configuration GUI : /usr/bin/system-config-bind and 
  8. // its manual. 
  9. // 
  10. include                 "/etc/dx.cfg"; 
  11. include                 "/etc/wt.cfg"; 
  12. options 
  13.         // Those options should be used carefully because they disable port 
  14.         // randomization 
  15.         // query-source    port 53; 
  16.         // query-source-v6 port 53; 
  17.  
  18.         // Put files that named is allowed to write in the data/ directory: 
  19.         directory "/var/named"; // the default 
  20.         dump-file               "data/cache_dump.db"; 
  21.         statistics-file         "data/named_stats.txt"; 
  22.         memstatistics-file      "data/named_mem_stats.txt"; 
  23. }; 
  24. logging 
  25. /*      If you want to enable debugging, eg. using the 'rndc trace' command, 
  26.  *      named will try to write the 'named.run' file in the $directory (/var/named). 
  27.  *      By default, SELinux policy does not allow named to modify the /var/named directory, 
  28.  *      so put the default debug log file in data/ : 
  29.  */ 
  30.         channel default_debug { 
  31.                 file "data/named.run"; 
  32.                 severity dynamic; 
  33.         }; 
  34. }; 
  35. // 
  36. // All BIND 9 zones are in a "view", which allow different zones to be served 
  37. // to different types of client addresses, and for options to be set for groups 
  38. // of zones. 
  39. // 
  40. // By default, if named.conf contains no "view" clauses, all zones are in the 
  41. // "default" view, which matches all clients. 
  42. // 
  43. // If named.conf contains any "view" clause, then all zones MUST be in a view; 
  44. // so it is recommended to start off using views to avoid having to restructure 
  45. // your configuration files in the future. 
  46. // 
  47. #view "localhost_resolver" 
  48. #{ 
  49. /* This view sets up named to be a localhost resolver ( caching only nameserver ). 
  50.  * If all you want is a caching-only nameserver, then you need only define this view: 
  51.  */ 
  52. #       match-clients           { localhost; }; 
  53. #       match-destinations      { localhost; }; 
  54. #       recursion yes; 
  55.         # all views must contain the root hints zone: 
  56. #       include "/etc/named.root.hints"; 
  57.  
  58.         /* these are zones that contain definitions for all the localhost 
  59.          * names and addresses, as recommended in RFC1912 - these names should 
  60.          * ONLY be served to localhost clients: 
  61.          */ 
  62.         #include "/etc/named.rfc1912.zones"; 
  63. #}; 
  64.  
  65. view "dxzone" { 
  66. match-clients { dx; }; 
  67. recursion yes; 
  68. zone "zhoutao.com"{ 
  69. type master; 
  70. file "dx/zhoutao.com.zone"; 
  71. }; 
  72. }; 
  73.  
  74. view "wtzone" { 
  75. match-clients { wt; }; 
  76. recursion yes; 
  77. zone "zhoutao.com"{ 
  78. type master; 
  79. file "wt/zhoutao.com.zone"; 
  80. }; 
  81. }; 
  82.  
  83. view "otherzone" { 
  84. match-clients { any; }; 
  85. recursion yes; 
  86. zone"zhoutao.com"{ 
  87. type master; 
  88. file "others/zhoutao.com.zone"; 
  89. }; 
  90. }; 



在etc下建立dx.cfg 和wt.cfg配置文件

[root@localhost etc]# cat dx.cfg
acl dx {192.168.3.129;};
[root@localhost etc]# cat wt.cfg
acl wt {192.168.3.130;};


在/var/named/chroot/var/named 下创建3个区域文件夹
mkdir dx wt others
[root@localhost named]# ls
data  dx  others  slaves  wt
vimdiff dx/zhoutao.com.zone wt/zhoutao.com.zone others/zhoutao.com.zone
 

 
  
  1. [root@localhost named]# cat dx/zhoutao.com.zone  
  2. $TTL    86400 
  3. @               IN SOA  @       root ( 
  4.                                         42              ; serial (d. adams) 
  5.                                         3H              ; refresh 
  6.                                         15M             ; retry 
  7.                                         1W              ; expiry 
  8.                                         1D )            ; minimum 
  9.  
  10.                 IN NS           @ 
  11. www             IN A            192.168.3.129 
  12.                 IN AAAA         ::1 
  13.  
  14. [root@localhost named]# cat wt/zhoutao.com.zone  
  15. $TTL    86400 
  16. @               IN SOA  @       root ( 
  17.                                         42              ; serial (d. adams) 
  18.                                         3H              ; refresh 
  19.                                         15M             ; retry 
  20.                                         1W              ; expiry 
  21.                                         1D )            ; minimum 
  22.  
  23.                 IN NS           @ 
  24. www             IN A            192.168.3.130 
  25.                 IN AAAA         ::1 
  26.  
  27. [root@localhost named]# cat others/zhoutao.com.zone  
  28. $TTL    86400 
  29. @               IN SOA  @       root ( 
  30.                                         42              ; serial (d. adams) 
  31.                                         3H              ; refresh 
  32.                                         15M             ; retry 
  33.                                         1W              ; expiry 
  34.                                         1D )            ; minimum 
  35.  
  36.                 IN NS           @ 
  37. www             IN A            192.168.3.128 
  38.                 IN AAAA         ::1 


/etc/init.d/named start


修改你的resolv.conf 用dig 或者host命令测试
192.168.3.129上
[root@localhost etc]# host www.zhoutao.com
www.zhoutao.com has address 192.168.3.129

192.168.3.130上
[root@localhost ~]# host www.zhoutao.com
www.zhoutao.com has address 192.168.3.130
www.zhoutao.com has IPv6 address ::1


本机上 192.168.1.7
[zhoutao@WorkSpace ~]$ host www.zhoutao.com
www.zhoutao.com has address 192.168.3.128
www.zhoutao.com has IPv6 address ::1