dns view
什么是DNS VIEW
以某种特殊的方式根据用户来源的不同而返回不同的查询结果,这个技术在CDN中应用相当多,解决目前贷款小,和延时大问题的一种方法
view “internal” { #internal 是区域名称,可以自定义,但必须是唯一
};
区分区域是通过match-clients 关键字来实现的
比如:
view “internal” {
matchclients { 192.168.0.0/24; };
};
上面这段表示只处理192.168.0.0/24网段的请求
match-clients 可能需要定义非常多的网段,bind引入acl关键字来定义变量替换,以使match-clients 中仅仅出现最少的符号,
而网段的增加可以在外部文件中进行
如:
acl "foosubnet" { 192.168.1/24;192.168.2/24; };
view "internal" {
matchclients { "foosubnet"; };
};
例子:简单的viewnamed.conf示例如下:
options {
directory "/var/named";
};
acl "foosubnet" { 192.168.0/25; };
view "internal" { // internal view of our zones
matchclients { "foosubnet"; };
zone "zhoutao.com" {
type master;
file "zhoutao.com.0127.zone";
};
};
view "external" { // view of our zones for the rest
of the world
matchclients { 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 地址在主服务器上所处的那个视图的信息。这个时候我们需要在从属服
务器的视图中设置 transfersource 关键字。
示例:一个从属服务器视named.conf的例子
options {
directory "/var/named";
};
acl "foosubnet" { 192.168.0/25; };
view "internal" { // internal view of our zones
matchclients { "foosubnet"; };
transfersource 192.168.0.1;
zone "zhoutao.com" {
type slave ;
masters {192.168.0.254;};
file "zhoutao.com.0127.zone";
};
};
view "external" { // view of our zones for the rest
of the world
matchclients { any; }; // implicit
transfersource 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 配置文件
- //
- // Sample named.conf BIND DNS server 'named' configuration file
- // for the Red Hat BIND distribution.
- //
- // See the BIND Administrator's Reference Manual (ARM) for details, in:
- // file:///usr/share/doc/bind-*/arm/Bv9ARM.html
- // Also see the BIND Configuration GUI : /usr/bin/system-config-bind and
- // its manual.
- //
- include "/etc/dx.cfg";
- include "/etc/wt.cfg";
- options
- {
- // Those options should be used carefully because they disable port
- // randomization
- // query-source port 53;
- // query-source-v6 port 53;
- // Put files that named is allowed to write in the data/ directory:
- directory "/var/named"; // the default
- dump-file "data/cache_dump.db";
- statistics-file "data/named_stats.txt";
- memstatistics-file "data/named_mem_stats.txt";
- };
- logging
- {
- /* If you want to enable debugging, eg. using the 'rndc trace' command,
- * named will try to write the 'named.run' file in the $directory (/var/named).
- * By default, SELinux policy does not allow named to modify the /var/named directory,
- * so put the default debug log file in data/ :
- */
- channel default_debug {
- file "data/named.run";
- severity dynamic;
- };
- };
- //
- // All BIND 9 zones are in a "view", which allow different zones to be served
- // to different types of client addresses, and for options to be set for groups
- // of zones.
- //
- // By default, if named.conf contains no "view" clauses, all zones are in the
- // "default" view, which matches all clients.
- //
- // If named.conf contains any "view" clause, then all zones MUST be in a view;
- // so it is recommended to start off using views to avoid having to restructure
- // your configuration files in the future.
- //
- #view "localhost_resolver"
- #{
- /* This view sets up named to be a localhost resolver ( caching only nameserver ).
- * If all you want is a caching-only nameserver, then you need only define this view:
- */
- # match-clients { localhost; };
- # match-destinations { localhost; };
- # recursion yes;
- # all views must contain the root hints zone:
- # include "/etc/named.root.hints";
- /* these are zones that contain definitions for all the localhost
- * names and addresses, as recommended in RFC1912 - these names should
- * ONLY be served to localhost clients:
- */
- #include "/etc/named.rfc1912.zones";
- #};
- view "dxzone" {
- match-clients { dx; };
- recursion yes;
- zone "zhoutao.com"{
- type master;
- file "dx/zhoutao.com.zone";
- };
- };
- view "wtzone" {
- match-clients { wt; };
- recursion yes;
- zone "zhoutao.com"{
- type master;
- file "wt/zhoutao.com.zone";
- };
- };
- view "otherzone" {
- match-clients { any; };
- recursion yes;
- zone"zhoutao.com"{
- type master;
- file "others/zhoutao.com.zone";
- };
- };
在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
- [root@localhost named]# cat dx/zhoutao.com.zone
- $TTL 86400
- @ IN SOA @ root (
- 42 ; serial (d. adams)
- 3H ; refresh
- 15M ; retry
- 1W ; expiry
- 1D ) ; minimum
- IN NS @
- www IN A 192.168.3.129
- IN AAAA ::1
- [root@localhost named]# cat wt/zhoutao.com.zone
- $TTL 86400
- @ IN SOA @ root (
- 42 ; serial (d. adams)
- 3H ; refresh
- 15M ; retry
- 1W ; expiry
- 1D ) ; minimum
- IN NS @
- www IN A 192.168.3.130
- IN AAAA ::1
- [root@localhost named]# cat others/zhoutao.com.zone
- $TTL 86400
- @ IN SOA @ root (
- 42 ; serial (d. adams)
- 3H ; refresh
- 15M ; retry
- 1W ; expiry
- 1D ) ; minimum
- IN NS @
- www IN A 192.168.3.128
- 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