智能DNS(Bind dlz)在企業(yè)中的應(yīng)用
原創(chuàng)【51CTO獨(dú)家特稿】編者按:在大眾網(wǎng),智能DNS被用在絕大部分二級(jí)域名和一些非大眾網(wǎng)域名上,一年多來(lái)運(yùn)行的相當(dāng)穩(wěn)定。曉輝本次總結(jié)了過(guò)去一年來(lái)部署使用智能DNS過(guò)程中的操作步驟和一些經(jīng)驗(yàn)心得,跟大家分享。
去年因?yàn)槎?jí)域名大量增加、Bind下管理不便的關(guān)系,在公司部署了智能DNS(Bind dlz),當(dāng)時(shí)寫(xiě)過(guò)一篇部署文檔進(jìn)行了記錄。不過(guò)在過(guò)去一年的使用當(dāng)中,發(fā)現(xiàn)了其中不少的錯(cuò)誤,所以本次發(fā)布該文檔的第二版,對(duì)這些錯(cuò)誤進(jìn)行修正。更新內(nèi)容包括:
1、修正web界面中對(duì)新建域名ns 域名是自身的嚴(yán)重bug。
2、修正web界面中線路為 電信 網(wǎng)通 移動(dòng)。
3、取消A、CNAME、MX、AAAA記錄最多只能添加10條的限制。
4、修正IP庫(kù)到最新。
5、使用CentOS 5.7版本。
智能dns工作示意圖
一、Bind-dlz簡(jiǎn)介
全世界范圍內(nèi)標(biāo)準(zhǔn)DNS服務(wù)器是BIND。盡管被流傳了許多年,經(jīng)過(guò)多次修改,BIND的基本功能保持不變。遺憾的是,有一些不好的缺陷。
- BIND從文本文件中獲取數(shù)據(jù),這樣容易因?yàn)榫庉嬪e(cuò)誤出現(xiàn)問(wèn)題。
- BIND需要將數(shù)據(jù)加載到內(nèi)存中,如果域或者記錄較多,會(huì)消耗大量的內(nèi)存。
- BIND啟動(dòng)時(shí)解析Zone文件,對(duì)于一個(gè)記錄較多的DNS來(lái)說(shuō),會(huì)耽誤更多的時(shí)間。
- 如果最近修改一條記錄,那么要重新加載或者重啟BIND才能生效,可能會(huì)影響客戶端查詢(xún)。
bind-dlz主要解決上述缺陷而誕生,在mysql存儲(chǔ)zone的記錄,比在文本中好管理的多。
智能DNS的原理:
在用戶解析一個(gè)域名的時(shí)候,判斷一下用戶的IP,然后跟DNS服務(wù)器內(nèi)部的IP表匹配一下,看看用戶是電信還是網(wǎng)通用戶,然后給用戶返回對(duì)應(yīng)的IP地址。
適用范圍:
網(wǎng)站要有三線路接入或者在電信、聯(lián)通、移動(dòng)部署有服務(wù)器,這樣智能dns才能派上用場(chǎng)。
二、智能DNS系統(tǒng)服務(wù)規(guī)劃
1、NameServer服務(wù)器設(shè)置(到新網(wǎng)或者萬(wàn)網(wǎng)后臺(tái)添加)
ns1.zjyxh.com 192.19.13.15 ns2.zjyxh.com 192.19.11.3
NS1是master ,NS2是slave。兩者數(shù)據(jù)通過(guò)mysql來(lái)同步。
2、測(cè)試NS記錄是否生效
#dig ns www.zjyxh.com #dig www.zjyxh.com +trace
3、Bind-View規(guī)劃
www.zjyxh.com 網(wǎng)通 (CNC) 124.133.11.78 www.zjyxh.com 電信(TELECOM) 58.56.11.153 www.zjyxh.com 移動(dòng)(ANY) 120.192.11.13
三、在CentOS 5.7上安裝MySQL Replication
因?yàn)锽ind-dlz是使用MySQL作為存儲(chǔ)zone的載體,這樣就可以用php來(lái)操作MySQL。特別注意:智能dns最少部署兩臺(tái)NameServer,主從關(guān)系。主從同步利用mysql的復(fù)制來(lái)實(shí)現(xiàn)主從同步。
首先下載mysql的最新版并解壓:
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.60.tar.gz tar zxf mysql-5.1.60.tar.gz cd mysql-5.1.60 ./configure --prefix=/usr/local/mysql --enable-assembler --with-server-suffix=-DZWWW --enable-thread-safe-client --enable-local-infile --enable-thread-safe-client --with-big-tables --with-charset=utf8 --with-client-ldflags=-all-static -with-collation=utf8_general_ci --with-extra-charsets=all --with-mysqld-ldflags=-all-static --with-mysqld-ldflags=-ltcmalloc --with-mysqld-user=mysql -with-plugins=partition,myisammrg --with-pthread --with-unix-socket-path=/tmp/mysql.sock --without-ndb-debug make && make install
將my.cnf放到/etc下,并安裝系統(tǒng)數(shù)據(jù)庫(kù)。
wget http://autolemp.googlecode.com/files/my.cnf cp support-*/mysql.server /etc/init.d/mysqld cp my.cnf /etc/ chmod 744 /etc/init.d/mysqld cd /usr/local/mysql chown -R mysql:mysql . rm -rf sql-bench mysql-test mkdir -p /data0/mysql/relaylog/ mkdir -p /data0/mysql/binlog/ chown -R mysql.mysql /data0/mysql /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data0/mysql/data
默認(rèn)腳本會(huì)啟動(dòng)MySQL。如果報(bào)錯(cuò),會(huì)在數(shù)據(jù)庫(kù)目錄里有錯(cuò)誤日志。
MySQL replication配置
1、MySQL安全設(shè)置
* 用mysqladmin命令來(lái)改root用戶口令
# mysqladmin -uroot password 51cto.com
//設(shè)置MySQL管理操作用戶root的密碼為51cto.com
* 用setpassword修改口令:
mysql> set password for root@localhost=password('51cto.com);
* 直接修改user表的root用戶口令
mysql> use mysql; mysql> update user set password=password('51cto.com') where user='root'; mysql> flush privileges;
2、刪除默認(rèn)的數(shù)據(jù)庫(kù)和用戶。
我們的數(shù)據(jù)庫(kù)是在本地,并且也只需要本地的php腳本對(duì)mysql進(jìn)行讀取,所以很多用戶都不需要。mysql初始化后會(huì)自動(dòng)生成空用戶和test庫(kù),這會(huì)對(duì)數(shù)據(jù)庫(kù)構(gòu)成威脅,我們?nèi)縿h除。
mysql> drop database test; mysql> use mysql; mysql> delete from db;
mysql> delete from user where not(host="localhost" anduser="root");
mysql> flush privileges;
3、Master 機(jī)器設(shè)置權(quán)限,賦予Slave機(jī)器FILE及Replication Slave權(quán)利,并打包要同步的數(shù)據(jù)庫(kù)結(jié)構(gòu)。
Master# ./mysql -u root -p 51cto.com Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 to server version: 5.1.60 Type 'help;'or '\h' for help. Type '\c' to clear the buffer. mysql> GRANT FILE ON*.* TO slaverep@172.19.1.3 IDENTIFIEDBY ‘slaverep’; mysql> GRANT REPLICATION SLAVE ON *.* TO slaverep@172.19.1.3 IDENTIFIEDBY ‘51cto.com’; mysql> Flush privilege;
賦予192.19.11.3也就是Slave 機(jī)器有File權(quán)限, 這個(gè)5.1.60版對(duì)replication的權(quán)限好像做了調(diào)整,只賦予Slave機(jī)器有File權(quán)限還不行,還要給它REPLICATION SLAVE的權(quán)限才可以。
Master 導(dǎo)入CDN。Sql文件建立CDN數(shù)據(jù)結(jié)構(gòu)
# mysql -uroot -p 51cto.com mysql> create database cdn; mysql> \q # mysql cdn -uroot -p 51cto.com < cdn.sql #cdn.sql文件所在的位置
這樣master 就有了CDN庫(kù)的數(shù)據(jù)結(jié)構(gòu)。
Slave一樣。這樣不再重復(fù)。
然后,設(shè)置主服務(wù)器Master的my.cnf,啟動(dòng)Mysql服務(wù)
Master# vi /etc/my.cnf
在[mysqld]添加或修改以下的內(nèi)容:
[mysqld] log-bin=/home/data/mysql/data/binlog/binlog #打開(kāi)logbin選項(xiàng)以能寫(xiě)到slave的 I/O線程; server-id=1 #表示是本機(jī)的序號(hào)為1,一般來(lái)講就是master的意思. binlog-do-db=cdn #表示同步cdn數(shù)據(jù)庫(kù);
然后把Master主服務(wù)器的MySQL重啟。
Master# service mysqld restart
4、修改Slave服務(wù)器的my.cnf
Slave# vi /etc/my.cnf
在[mysqld]添加或修改以下的內(nèi)容:
[mysqld] master-host=192.19.13.15 master-user=slaverep master-password=51cto.com master-port=3306 server-id=10 master-connect-retry=60 replicate-do-db=cdn [要更新的數(shù)據(jù)庫(kù)] log-slave-updates
5、刪除Slave端數(shù)據(jù)庫(kù)目錄中的master.info
Slave# rm -f master.info
6、重啟動(dòng)Slave的MySQL服務(wù)。
Slave# service mysqld restart
7、測(cè)試
先檢測(cè)兩個(gè)MySQL數(shù)據(jù)庫(kù)中的cdn是否正常。正常情況應(yīng)該是Master和Slave 中的MySQL都有相同的cdn數(shù)據(jù)庫(kù),并且里面的數(shù)據(jù)都一樣。然后測(cè)試replication功能是否起用。在Master中的reptest數(shù)據(jù)庫(kù)添加一筆數(shù)據(jù):
Master# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 to server version: 5.1.60 Type 'help;'or '\h' for help. Type '\c' to clear the buffer. mysql> use cdn; Database changed mysql> INSERT INTO rep_table VALUES ('test1', '4321', 'T',24); Query OK, 1 row affected (0.00 sec) mysql>
然后查看Slave機(jī)器的reptest數(shù)據(jù)庫(kù):
Slave# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 to server version: 5.1.49 Type 'help;'or '\h' for help. Type '\c' to clear the buffer. mysql> use cdn; Database changed mysql>select * from reptable; +------+------+------+------+ | id | name| sex | age | +------+------+------+------+ | test1|4321 | T | 24 | +------+------+------+------+ 1 row in set(0.00 sec) mysql> source /root/etc/cdn.sql #導(dǎo)入壓縮包中的cdn.sql
這樣就ok了。接下來(lái)是一些性能方面的調(diào)優(yōu)。
為MySQL添加TCMalloc庫(kù)降低系統(tǒng)負(fù)載
TCMalloc(Thread-CachingMalloc)是google開(kāi)發(fā)的開(kāi)源工具──“google-perftools”中的成員。與標(biāo)準(zhǔn)的glibc庫(kù)的malloc相比,TCMalloc在內(nèi)存的分配上效率和速度要高得多,可以在很大程度上提高M(jìn)ySQL服務(wù)器在高并發(fā)情況下的性能,降低系統(tǒng)負(fù)載。
1、64位操作系統(tǒng)請(qǐng)先安裝libunwind庫(kù),32位操作系統(tǒng)不要安裝。libunwind庫(kù)為基于64位CPU和操作系統(tǒng)的程序提供了基本的堆棧輾轉(zhuǎn)開(kāi)解功能,其中包括用于輸出堆棧跟蹤的API、用于以編程方式輾轉(zhuǎn)開(kāi)解堆棧的API以及支持C++異常處理機(jī)制的API。
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz tar zxvf libunwind-0.99.tar.gz cd libunwind-0.99/ CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install
2、安裝google-perftools:
wget http://google-perftools.googlecode.com/files/google-perftools-1.6.tar.gz tar zxvf google-perftools-1.6.tar.gz cd google-perftools-1.6/ ./configure make && make install echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf /sbin/ldconfig
3、修改MySQL啟動(dòng)腳本(根據(jù)你的MySQL安裝位置而定):
vi /usr/local/mysql/bin/mysqld_safe
在# executingmysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
保存后退出,然后重啟MySQL服務(wù)。
4、使用lsof命令查看tcmalloc是否起效:
lsof -n | grep tcmalloc
如果出現(xiàn)以下信息,說(shuō)明tcmalloc已經(jīng)起效:
mysqld 10847 mysql mem REG 8,5 1203756 20484960/usr/local/lib/libtcmalloc.so.0.0.0
MySQL部分配置到此完畢,下面可以進(jìn)入正題了:安裝配置Bind-dlz。
四、安裝配置Bind-DLZ 及相關(guān)腳本
1、安裝bind
#mkdir /usr/local/src/bind-dlz #cd /usr/local/src/bind-dlz #wget http://ftp.isc.org/isc/bind9/9.6.0-P1/bind-9.6.0-P1.tar.gz #tar zxvf bind-9.6.0-P1.tar.gz #cd bind-9.6.0-P1 #./configure --with-dlz-mysql --enable-largefile --enable-threads=no--prefix=/usr/local/bind #make -j4 && make install
2、創(chuàng)建相關(guān)配置文件
cd /usr/local/bind/etc ../sbin/rndc-confgen >rndc.conf tail -n10 rndc.conf | head -n9 | sed -e s/#\//g >named.conf # vilocalhost.zone ttl 86400 @ IN SOA localhost. root.localhost. ( 1997022700 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum IN NS localhost. 1 IN PTR localhost. # dig >named.root
編輯named.conf:
#vi named.conf
最下面加入:
include"/usr/local/bind/etc/cnc_acl.conf"; //網(wǎng)通ACL include "/usr/local/bind/etc/telecom_acl.conf"; //電信ACL include "/usr/local/bind/etc/view.conf"; //DLZ相關(guān)的配
3、配置DNSTSIG
使用dnssec-keygenfunction 產(chǎn)生加密密鑰,一個(gè)為public key,另一個(gè)為private key,本文假設(shè)應(yīng)用服務(wù)器存在CNC,TELECOM,EDU,ANY
(1)產(chǎn)生加密金鑰
#cd /usr/local/bind/sbin #./dnssec-keygen -a hmac-md5 -b 128 -n HOST cnc #./dnssec-keygen -a hmac-md5 -b 128 -n HOST telecom #./dnssec-keygen -a hmac-md5 -b 128 -n HOST edu #./dnssec-keygen -a hmac-md5 -b 128 -n HOST any
(2)查看生成的密鑰文件
# cat Kcnc.+157+24406.private //以網(wǎng)通為例. Private-key-format: v1.2 Algorithm: 157 (HMAC_MD5) Key: YTjTOw00PzeEaasA16/Rvw== Bits: AAA=
將 YTjTOw00PzeEaasA16/Rvw== 加入到named.conf,其它同例。
詳細(xì)請(qǐng)參照named.conf配置文件
配置named.conf
#vi /usr/local/bind/etc/named.conf key "rndc-key" { algorithm hmac-md5; secret "5PubnjGuAWeH9F2dIUYd6g=="; }; controls { inet 127.0.0.1 port 953 allow { 127.0.0.1; } keys {"rndc-key"; }; }; options { directory "/usr/local/bind/etc"; pid-file "named.pid"; }; #TSIG-key key "cnc" { algorithm hmac-md5; secret "YTjTOw00PzeEaasA16/Rvw=="; }; key "telecom" { algorithm hmac-md5; secret"pUcQGLpSH2tQgVZ9ZHU6Yg=="; }; key "edu"{ algorithm hmac-md5; secret"Bzo6MTzrzbRFQbONYTS1Cw=="; }; key "any"{ algorithm hmac-md5; secret"DHpPfGJdMLv91OygBf9H6w=="; }; #acl acl"dns-ip-list"{ 172.19.3.15; #masterDNS IP 172.19.1.3; #slaveDNS IP }; #acl include include"/usr/local/bind/etc/cnc_acl.conf"; //網(wǎng)通ACL include"/usr/local/bind/etc/telecom_acl.conf"; //電信 ACL include"/usr/local/bind/etc/edu_acl.conf"; //教育網(wǎng)ACL include"/usr/local/bind/etc/view.conf"; //bind-view部分
3、Bind啟動(dòng)腳本
#!/bin/bash # chkconfig: 345 71 71 # description:bind daemondcase"$1" in start) if [ -x/usr/local/bind/sbin/named ]; then /usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -u bind echo "BIND9-named server started" fi ;; stop) kill `cat/usr/local/bind/etc/named.pid` && echo . && echo 'BIND9 serverstopped' ;; restart) echo . echo "Restart BIND9 server" $0 stop sleep 10 $0 start ;; reload) /usr/local/bind/sbin/rndcreload ;; status) /usr/local/bind/sbin/rndcstatus ;; *) echo"$0 start | stop | restart |reload |status" ;; esac chkconfig --add bind9 service bind9 start#啟動(dòng)bind service bind9 reload# 重載bind service bind9 restart# 重啟bind
剩余文檔見(jiàn)etc.tar.gz這個(gè)文件,里面很詳細(xì)。部署master時(shí)候用tar zxf etc.tar.gz -C /usr/local/bind/etc覆蓋即可,剩下的slave在部署bind還是將配置文件解壓到/usr/local/bind/etc/即可,將rndc-key和dnssec重新配置一遍就可以用了!
Web管理界面可以在這里下載(下載之后解壓縮,然后上傳到你的NameServer下即可。這個(gè)管理界面最初是LinuxTone的Dennis寫(xiě)的,我在這里修改了一些bug):
注意事項(xiàng)
部署DNS,防火墻和路由器要設(shè)置清楚,我部署的時(shí)候就是因?yàn)橛卜罌](méi)有對(duì)master和slave服務(wù)器開(kāi)放tcp和udp53端口,造成不能解析域名。需要大家切記!
作者簡(jiǎn)介:崔曉輝,網(wǎng)名coralzd,大眾網(wǎng)系統(tǒng)管理員,精通網(wǎng)站系統(tǒng)架構(gòu)、Unix技術(shù)。gtalk:coralzd@gmail.com
【編輯推薦】
- 網(wǎng)管員實(shí)戰(zhàn)手冊(cè):尋找最可靠DNS服務(wù)器
- 專(zhuān)題:DNS服務(wù)器安裝優(yōu)化全攻略
- 在RHEL 5下通過(guò)Bind實(shí)現(xiàn)DNS功能