天天看點

Centos環境下實作DNS的智能解析

DNS智能解析的實質:基于不同的來源,dns伺服器傳回不同的結果

DNS政策解析最基本的功能是可以智能的判斷通路您網站的使用者,然後根據不同的通路者把您的域名分别解析成不同的IP位址。如通路者是網通使用者,DNS政策解析伺服器會把你的域名對應的網通IP位址解析給這個通路者。DNS政策解析伺服器會把您域名對應的電信IP位址解析給這個通路者。

實驗環境:dns伺服器是在centos6.4 32位作業系統上實作的,中間用了一台H3C的防火牆。Internet用的是一台電腦pc模拟的。

實驗中用到的mysql-5.5.15-linux2.6-i686.tar.gz可以到mysql的官方網站 www.mysql.com 下載下傳,

也可以到 http://pan.baidu.com/s/1kZcj8   下載下傳,

bind-9.8.6-P1.tar.gz可以到 http://pan.baidu.com/s/1qW4joJE    下載下傳。

網絡拓撲圖如下:

Centos環境下實作DNS的智能解析

試驗結果預期:内網使用者解析出來的位址是192.168.80.12,外網位址解析出來的位址是61.130.130.1.

[root@localhost ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/

把源碼解壓到/usr/local/目錄下

[root@localhost ~]# cd /usr/local/

[root@localhost local]# ln -s mysql-5.5.15-linux2.6-i686 mysql

進入解壓好的目錄,為了友善通路給此目錄建立一個快捷方式

[root@localhost local]# cd mysql

[root@localhost mysql]# vim INSTALL-BINARY

進入mysql目錄,裡邊有一個INSTALL-BINARY檔案,這是個安裝幫助文檔,可以看到如下步驟介紹:

Centos環境下實作DNS的智能解析

[root@localhost ~]# groupadd mysqlsts

[root@localhost ~]# useradd -r -g mysql mysql

建立一個mysql組并添加一個賬号mysql

[root@localhost mysql]# chown -R mysql .

[root@localhost mysql]# chgrp -R mysql .

改變所有檔案的所有者和所有組都改為mysql

[root@localhost mysql]# scripts/mysql_install_db --user=mysql

以sql的身份來初始化資料庫

[root@localhost mysql]# chown -R root .

[root@localhost mysql]# chown -R mysql data

再把所有者和所有組改回來

[root@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf

将support-files/my-medium.cnf拷貝到 /etc/my.cnf,是否覆寫選擇y。

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql]# chmod a+x /etc/init.d/mysqld

給mysqld執行權限

[root@localhost mysql]# service mysqld start

啟動服務mysqld。

發現出現錯誤:

Centos環境下實作DNS的智能解析

如果出現此錯誤的話,就把剛才做的指令重新執行一遍:

後邊拷貝的那兩步就不用重做了,然後再次開始mysqld服務,運作成功!

[root@localhost mysql]# netstat -tupln |grep 3306

檢視mysqld端口是否打開

Centos環境下實作DNS的智能解析

端口已經打開。

[root@localhost mysql]# chkconfig --add mysqld

[root@localhost mysql]# chkconfig mysqld on

将mysqld設為開機自動啟動。

[root@localhost mysql]# vim /etc/profile

PATH=$PATH:/usr/local/mysql/bin

為了友善使用目錄下的bin檔案,将它加入到系統檔案中:

[root@localhost mysql]# . /etc/profile

重新讀取profile檔案:(重新登入也可以)

[root@localhost mysql]# mysqladmin -u root -p password '123'然後直接回車

給mysql建立一個管理者使用者,密碼為123

到這裡,mysql已經安裝完畢。

接下裡安裝bind:

[root@localhost ~]# tar -zxvf bind-9.8.6-P1.tar.gz -C /usr/local/src/

解壓bind安裝包

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# ll

total 4

drwxrwxr-x. 12 10132 wheel 4096 Oct 16 08:09 bind-9.8.6-P1

[root@localhost src]# cd bind-9.8.6-P1/

切換到bind目錄

[root@localhost bind-9.8.6-P1]# ./configure --prefix=/usr/local/bind9 --with-dlz-mysql=/usr/local/mysql --enable-threads=no --disable-openssl-version-check

[root@localhost bind-9.8.6-P1]# make && make install 執行make指令已經make install

[root@localhost bin]# cd /usr/local/bind9/

進入此目錄,此目錄為bind的安裝目錄

[root@localhost bind9]# cd sbin/

[root@localhost sbin]# ./rndc-confgen -a

Centos環境下實作DNS的智能解析

執行此指令會在/usr/local/bind9/etc/下産生一個rndc.key 檔案

[root@localhost sbin]# ./rndc-confgen >../etc/named.conf

生成named.conf 檔案

以上兩步最好不要在終端中做,因為在終端中做檔案生的特别慢,最好是直接在系統中執行指令。

現在進入/usr/local/bind9/etc/目錄會發現其中有三個檔案:

Centos環境下實作DNS的智能解析

進入named.conf檔案:

去掉第20行至23行前邊的#:

Centos環境下實作DNS的智能解析

修改option如下:

directory "/usr/local/bind9/etc/";

pid-file "/usr/local/bind9/var/run/named.pid";

allow-query { any; };

recursion no;

version "gaint-d1"

Centos環境下實作DNS的智能解析

在此檔案中建立“lan”和“wan”兩個通路控制連結清單:

Centos環境下實作DNS的智能解析

并在此檔案中添加以下内容:

view "lan-view" {

match-clients {lan;};

dlz "Mysql zone" {

database "mysql

{host=127.0.0.1 dbname=mydata ssl=false user=root pass=123}

{select zone from lan_dns_records where zone='$zone$'}

{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"') 32 when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum) 33 else data end from lan_dns_records where zone='$zone$' and host='$record$'}";

};

view "wan-view" {

match-clients {wan;};

{select zone from wan_dns_records where zone='$zone$'}

{select ttl, type, mx_priority, case when lower(type)='txt' then concat('\"', data, '\"')

when lower(type) = 'soa' then concat_ws(' ', data, resp_person, serial, refresh, retry, expire, minimum)

else data end from wan_dns_records where zone='$zone$' and host='$record$'}";

};

到此處為止,bind也基本安裝完成。

接下來給資料庫中添加内容:

[root@localhost etc]# mysql -u root -p

Enter password:

以管理者身份登入mysql

mysql> create database mydata;

建立資料庫mydate

mysql> use mydata;

打開mydate

建立lan表:

create table lan_dns_records (

zone varchar (255),

host varchar (255),

type varchar (255),

data varchar (255),

ttl int(11),

mx_priority varchar (255),

refresh int(11),

retry int(11),

expire int(11),

minimum int(11),

serial bigint(20),

resp_person varchar (255),

primary_ns varchar (255)

);

建立wan表:

create table wan_dns_records (

可以看到現在資料庫中有如下表:

Centos環境下實作DNS的智能解析

mysql> insert into lan_dns_records (zone,host,type,data,ttl,retry) values ('abc.com','www','A','192.168.80.12','86400','15');

mysql> insert into wan_dns_records (zone,host,type,data,ttl,retry) values ('abc.com','www','A','61.130.130.1','86400','15')

兩表格中各插入一條記錄。

此時可看到資料庫中的兩個表中有如下資料:

Centos環境下實作DNS的智能解析

使用/usr/local/bind9/sbin/named -g -d 1 -c /usr/local/bind9/etc/named.conf指令測試:

發現出錯,缺少一個libmysqlclient.so.18庫檔案

Centos環境下實作DNS的智能解析

其實這個檔案時存在的,在/usr/local/mysql/lib/目錄下,

[root@localhost sbin]# vim /etc/ld.so.conf.d/mysql.conf

在/etc/ld.so.conf.d/下随便建一個檔案,比如叫做mysql.conf 然後将此庫檔案的路徑寫入:

/usr/local/mysql/lib/

[root@localhost sbin]# ldconfig

執行ldconfig重新整理緩存。

[root@localhost etc]# ifconfig eth0:0 61.130.130.10 netmask 255.255.255.0

給網卡eth0再加個位址61.130.130.0/24網段内的,以便測試之用。

[root@localhost etc]# dig www.abc.com @192.168.80.12

[root@localhost etc]# dig www.abc.com @61.130.130.10

測試結果如下:

Centos環境下實作DNS的智能解析
Centos環境下實作DNS的智能解析

然後進入防火牆配置:

[H3C]int eth0/0

[H3C-Ethernet0/0]ip add 192.168.80.254 255.255.255.0

[H3C-Ethernet0/0]int eth0/4

[H3C-Ethernet0/4]ip add 61.130.130.100 255.255.255.0

[H3C-Ethernet0/0]quit

[H3C]firewall zone trust

[H3C-zone-trust]add int eth0/4

[H3C-zone-trust]quit

[H3C]firewall zone untrust

[H3C-zone-untrust]add int eth0/0

[H3C-Ethernet0/4]nat server protocol udp global 61.130.130.1 53 inside 192.168.80.12 53

配置完成後可以測試一下:

Centos環境下實作DNS的智能解析

繼續閱讀