智能DNS配置詳解
智能DNS 配置詳解XX 網(wǎng)NS 服務(wù)器為FreeBSD 下的Bind master/slave ,起初因?yàn)樵L問(wèn)量不太大,這個(gè)架構(gòu)完全可以勝任,但是隨著域名的增加,維護(hù)zone 文件變得越來(lái)越復(fù)雜,一
智能DNS 配置詳解
XX 網(wǎng)NS 服務(wù)器為FreeBSD 下的Bind master/slave ,起初因?yàn)樵L問(wèn)量不太大,這個(gè)架構(gòu)完全可以勝任,但是隨著域名的增加,維護(hù)zone 文件變得越來(lái)越復(fù)雜,一旦書(shū)寫(xiě)有誤,會(huì)造成Bind 無(wú)法解析域名。所以,特此部署了Bind DLZ Mysql ,經(jīng)過(guò)一個(gè)月的上線(xiàn)測(cè)試,發(fā)現(xiàn)其負(fù)載很小,每秒5qps 。不存在mysql time out 問(wèn)題,因?yàn)閙ysql 每時(shí)每刻都在查詢(xún). 在這里感謝老曹的鼎力相助!
目錄
一、相關(guān)術(shù)語(yǔ)簡(jiǎn)介
二、智能DNS 系統(tǒng)服務(wù)規(guī)劃
三、MYSQLReplication 及添加GoogleTCMALLOC 庫(kù)降低系統(tǒng)負(fù)載
四、安裝配置Bind-DLZ 及相關(guān)腳本
五、測(cè)試Bind-DLZ 相關(guān)總結(jié)
一相關(guān)術(shù)語(yǔ)簡(jiǎn)介:
1、智能DNS (Bind-view ):
智能DNS 的原理很簡(jiǎn)單:在用戶(hù)解析一個(gè)域名的時(shí)候,判斷一下用戶(hù)的IP ,然后跟DNS 服務(wù)器內(nèi)部的IP 表匹配一下,看看用戶(hù)是電信還是網(wǎng)通用戶(hù),然后給用戶(hù)返回對(duì)應(yīng)的IP 地址。目前的域名服務(wù)運(yùn)營(yíng)商不提供智能DNS 服務(wù),所以必須自行架設(shè)DNS 服務(wù)或者使用網(wǎng)上免費(fèi)的智能DNS 服務(wù),如DNSPOD.
2.Bind-DLZ
Bind-DLZ 主頁(yè):http://bind-dlz.sourceforge.net/
DLZ(Dynamically Loadable Zones)與傳統(tǒng)的BIND9不同,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 才能生效,那么需要時(shí)間,可能會(huì)影響客戶(hù)端查詢(xún)。 而B(niǎo)ind-dlz 即將幫你解決這些問(wèn)題, 對(duì)Zone 文件操作也更方便了,直接對(duì)數(shù)據(jù)庫(kù)操作, 可以很方 便擴(kuò)充及開(kāi)發(fā)管理程序。
二、智能DNS 系統(tǒng)服務(wù)規(guī)劃
,1、NameServer 服務(wù)器注冊(cè)(到新網(wǎng)或者萬(wàn)網(wǎng)后臺(tái)添加)
ns1.soshandong.com 192.19.13.15
ns2.soshandong.com 192.19.11.3
NS1是master ,NS2是slave 。兩者數(shù)據(jù)通過(guò)mysql 來(lái)同步。
2、測(cè)試NS 記錄是否生效
#digns www.soshandong.com
#digwww.soshandong.com trace
3、Bind-View 規(guī)劃
www.soshandong.com 網(wǎng)通 (CNC) 124.133.11.78 www.soshandong.com 電信(TELECOM) 58.56.11.153
www.soshandong.com 移動(dòng)(ANY) 120.192.11.13
三、MYSQLReplication 在Ubuntu 上的安裝
Ubuntu 上安裝Mysql 5.1.51
因新安裝的Ubuntu 沒(méi)有zlib-dev 庫(kù)和ncurses ,需要執(zhí)行apt-get 來(lái)安裝他們
#apt-get install zlib1g-dev ncurses
首先下載mysql-5.1.51.tar.gz ,并解壓.
#cd mysql-*
#sh mysql.sh
腳本執(zhí)行完畢后,將my.cnf 放到/etc下
# mv my.cnf /etc/
默認(rèn)腳本會(huì)啟動(dòng)mysql ,如果報(bào)錯(cuò),會(huì)在數(shù)據(jù)庫(kù)目錄里有錯(cuò)誤日志。
Mysql replication配置
1、Mysql 安全設(shè)置
* 用mysqladmin 命令來(lái)改root 用戶(hù)口令# mysqladmin -uroot password linuxtone.org//設(shè)置MySQL 管理操作用戶(hù)root 的密碼為52netseek
* 用setpassword 修改口令:
,mysql> set password for root@localhost=password('linuxtone.org);
* 直接修改user 表的root 用戶(hù)口令
mysql> use mysql;
mysql> update user set password=password('linuxtone.org') where user='root';
mysql> flush privileges;
2. 刪除默認(rèn)的數(shù)據(jù)庫(kù)和用戶(hù)我們的數(shù)據(jù)庫(kù)是在本地,并且也只需要本地的php 腳本對(duì)mysql 進(jìn)行讀取,所以很多用戶(hù)不需要。m
ysql 初始化后會(huì)自動(dòng)生成空用戶(hù)和test 庫(kù),這會(huì)對(duì)數(shù)據(jù)庫(kù)構(gòu)成威脅,我們?nèi)縿h除。我們使用mysql 客戶(hù)端程序連接到本地的mysql 服務(wù)器后出現(xiàn)如下提示:
mysql> drop database test;
mysql> use mysql;
mysql> delete from db;
mysql> delete from user where not(host="localhost" anduser="root"); mysql> flush privileges;
Mysql replication設(shè)置
3、Master 機(jī)器設(shè)置權(quán)限,賦予Slave 機(jī)器FILE 及Replication Slave權(quán)利,并打包要同步的數(shù)據(jù)庫(kù)結(jié)構(gòu)。
Master#./mysql –u root –plinuxtone.org
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2 to server version: 5.1.51
Type 'help;'or 'h' for help. Type 'c' to clear the buffer.
mysql> GRANTFILE ON*.* TO IDENTIFIEDBY ?slaverep?; mysql>GRANT REPLICATION SLAVE ON *.* TO BY ?linuxtone.org?;
mysql>Flushprivilege;
賦予192.19.11.3也就是Slave 機(jī)器有File 權(quán)限, 這個(gè)5.1.51版對(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 –plinuxtone.org
Mysql>create database cdn;
Mysql>q
#mysql
cdn –uroot –plinuxtone.org 這樣master 就有了CDN 庫(kù)的數(shù)據(jù)結(jié)構(gòu)。 Slave 一樣。這樣不再重復(fù) 2設(shè)置主服務(wù)器Master 的my.cnf ,啟動(dòng)Mysql 服務(wù) Master# vi/etc/my.cnf 在[mysqld]添加或修改以下的 [mysqld] log-bin = /home/data/mysql/data/binlog/binlog#打開(kāi)logbin 選項(xiàng)以能寫(xiě)到slave 的 I/O線(xiàn)程; server-id=1 #表示是本機(jī)的序號(hào)為1, 一般來(lái)講就是master 的意思. binlog-do-db= cdn #表示同步cdn 數(shù)據(jù)庫(kù); PS:提供的My 。Cnf 比較詳細(xì)。 然后把Master 主服務(wù)器的Mysql 重啟。 Master# servicemysqld restart 4、修改Slave 服務(wù)器的my.cnf Slave# vi/etc/my.cnf 在[mysqld]添加或修改以下的 master-host=192.19.13.15 master-user=slaverep master-password=linuxtone.org 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 -fmaster.info 6、重啟動(dòng)Slave 的slave start。 Slave#servicemysqld 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.51 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 Enterpassword: 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 添加TCMalloc 庫(kù)降低系統(tǒng)負(fù)載 TCMalloc (Thread-CachingMalloc )是google 開(kāi)發(fā)的開(kāi)源工具──“”中的成員。與標(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 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 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 如果發(fā)現(xiàn)以下信息,說(shuō)明tcmalloc 已經(jīng)起效: mysqld 10847 mysql mem REG 8,5 1203756 20484960/usr/local/lib/libtcmalloc.so.0.0.0 四. 安裝配置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 #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/edu_acl.conf"; //教育網(wǎng)ACL include "/usr/local/bind/etc/view.conf"; //DLZ相關(guān)的配 3、配置DNSTSIG 1、使用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 HOSTcnc #./dnssec-keygen -a hmac-md5 -b 128 -n HOSTtelecom #./dnssec-keygen -ahmac-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 配置文件 2、配置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/sh # powered bydzwww.com case"$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 ;; *)