最原始的负载平衡,可以在 DNS 里使用多个 A 纪录来达到效果。假设,如果你有三台 WWW 服务器,它的 IP 地址分别为 10.0.0.1,10.0.0.2 以及 10.0.0.3,使用以下的设定方式,每台服务器都会被分到 1/3 的联机。
Name TTL CLASS TYPE Resource Record (RR) Data WWW 600 IN A 192.168.1.1 600 IN A 192.168.1.96 600 IN A 192.168.1.99
当有客户端寻问这三笔纪录时,BIND 会使用不同的顺序轮流回复这三个 IP 地址。如上面这个例子,客户端将会接到任意的顺序,如 1,2,3 ,2,3,1 以及 3,1,2。大部份的客户端会使用第一个顺序,并且忽略其余的。
2.rrset-order 功能
rrset-order {
[ class class_name ]
[ type type_name ]
[ name "domain_name"]
order ordering }; //ordering 可為 fixed / random / cyclic
范例 :
rrset-order {class IN type A name "sip.com" order random;};
rrset-order 多笔 FQDN 时响应的方法,也就是当您有 Round Robin 的数据(一个名称对应到多个 RDATA)时,其响应的方式:
random: 随机选取,原来之系统默认值
cyclic: 循环式回答,即有三笔资料的话,即依 1->2->3->1->2…回答
3.Bind配置
这里以sip.com为范例:
/etc/named.conf
...
options {
listen-on port 53 { 127.0.0.1; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
rrset-order {
class IN type A name "sip.com" order random;
};}
...
zone "sip.com" IN {
type master;
file "named.sip.zone";
};
/var/named/named.sip.zone
; BIND db file for sip.com
$TTL 86400
@ IN SOA primary.sip.com. admin.sip.com. (
2008090301 ; serial number YYMMDDNN
28800 ; Refresh
7200 ; Retry
864000 ; Expire
86400 ; Min TTL
)NS primary.sip.com.
NS secondary.sip.com.MX 10 mail.sip.com.
MX 20 mail-spool.sip.com.
_sip._udp SRV 0 1 9 primary.sip.com.
SRV 0 3 9 secondary.sip.com.
primary IN A 10.30.32.30
secondary IN A 10.30.32.31
mail IN A 10.30.32.32
mail-spool IN A 10.30.32.34
www IN A 192.168.1.96
www IN A 192.168.1.99
www IN A 192.168.1.1
$ORIGIN sip.com.这里www.sip.com配置了3个ip地址,分别为192.168.1.1 , 192.168.1.96, 192.168.1.99.
4.Test
重新加载配置:
/etc/init.d/named reload
#第一次查询
dig www.sip.com
; <<>> DiG 9.5.0-P1 <<>> www.sip.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32738
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 2, ADDITIONAL: 2;; QUESTION SECTION:
;www.sip.com. IN A;; ANSWER SECTION:
www.sip.com. 86400 IN A 192.168.1.1
www.sip.com. 86400 IN A 192.168.1.96
www.sip.com. 86400 IN A 192.168.1.99;; AUTHORITY SECTION:
sip.com. 86400 IN NS primary.sip.com.
sip.com. 86400 IN NS secondary.sip.com.;; ADDITIONAL SECTION:
primary.sip.com. 86400 IN A 10.30.32.30
secondary.sip.com. 86400 IN A 10.30.32.31;; Query time: 9 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Sep 4 00:01:39 2008
;; MSG SIZE rcvd: 155# 第二次查询
dig www.sip.com
; <<>> DiG 9.5.0-P1 <<>> www.sip.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28609
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 2, ADDITIONAL: 2;; QUESTION SECTION:
;www.sip.com. IN A;; ANSWER SECTION:
www.sip.com. 86400 IN A 192.168.1.99
www.sip.com. 86400 IN A 192.168.1.1
www.sip.com. 86400 IN A 192.168.1.96;; AUTHORITY SECTION:
sip.com. 86400 IN NS secondary.sip.com.
sip.com. 86400 IN NS primary.sip.com.;; ADDITIONAL SECTION:
primary.sip.com. 86400 IN A 10.30.32.30
secondary.sip.com. 86400 IN A 10.30.32.31;; Query time: 5 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Sep 4 00:01:41 2008
;; MSG SIZE rcvd: 155
注意红色部分,www.sip.com返回的地址顺序已经改变了!