本文章爲各位介紹Bind基於DLZ實現智能DNS配置教程,如果有需要對於這個智能dns配置的朋友可以進來參考此教程。
簡介
在我看來基於Bind的只能DNS方案主要包括兩個部分:Geolocation和Dynamic Record。國內的業界對智能DNS的定位也無非這兩點,但是我所理解的智能DNS是建立在這兩條基礎上的智能調度系統,比如我有三個負載能力不同的數據中心,DNS可以根據數據中心的metrics(這裏可能包括帶寬,服務能力等)實現流量的調度,限於個人水平個人未在這個方向有所實踐,這個話題留作以後討論,所以本文只針對前兩個問題。由於Bind本身的配置可運維性比較差,這就引出本文主要討論的DLZ。
原理
DLZ實際上就是擴展了Bind,將Zonefle的內容放到外部數據庫裏,然後給Bind配置查詢語句從數據庫裏查詢記錄。當修改數據庫裏的記錄信息的時候,無需重啓Bind,下次客戶請求時直接就能返回新的記錄了。另外,DLZ本身不支持緩存,所以需要自己根據實際情況解決查詢的問題。
安裝
注意:這裏我以CentOS7上安裝dlz-mysql模塊爲例。
安裝依賴
yum install mariadb-devel gcc wget patch make
下載源碼
Bind9.8之前的版本需要打patch,具體可參考DLZ官方文檔,Bind9.8之後(包括9.8)的版本已經集成DLZ:
wgetftp://ftp.isc.org/isc/bind9/9.10.1/bind-9.10.1.tar.gz
tar xzfbind-9.10.1.tar.gz
cd bind-9.10.1
配置
由於CentOS7目錄結構上的變更,在編譯dlz-mysql時會找不到庫文件或者head文件,所以要做個軟連接:
ln -s /usr/lib/mysql/usr/lib64/mysql
./configure --prefix/opt/bind --with-dlz-filesystem --with-dlz-mysql
編譯
make
安裝
make install
模型
注意:DLZ沒有限制用戶的數據模型,你可以根據業務邏輯定義模型,然後構造自己的查詢語句即可。官方給出了建議的模型。
建模
Field Type Null Key Default Extra
zone text YES NULL
host text YES NULL
type text YES NULL
data text
ttl int(11) YES NULL
mx_priority text YES NULL
refresh int(11) YES NULL
retry int(11) YES NULL
expire int(11) YES NULL
minimum int(11) YES NULL
serial bigint(20)YES NULL
resp_person text YES NULL
primary_ns text YES NULL
zone 區域
host 記錄名
type 記錄類型
data 記錄值
ttl 緩存時間
mx_priority mx記錄優先級
refresh SOA記錄的刷新時間
retry SOA記錄的重試時間
expire SOA記錄的過期時間
minimum SOA記錄的minimum
serial SOA記錄的序列號
resp_person SOA記錄的序列號
primary_ns <尚不明確這個字段的意義>
建庫建表
新建數據庫:
create database demo;
新建record表:
CREATE TABLE IF NOT EXISTS records (
id int(10)unsigned NOT NULL AUTO_INCREMENT,
zone varchar(255) NOT NULL,
host varchar(255) NOT NULL,
type enum('A','MX','CNAME','NS','SOA','PTR','TXT','AAAA','SVR','URL')NOT NULL,
datavarchar(255) NOT NULL,
ttl int(11)NOT NULL,
mx_priorityint(11) DEFAULT NULL,
refreshint(11) DEFAULT NULL,
retryint(11) DEFAULT NULL,
expireint(11) DEFAULT NULL,
minimumint(11) DEFAULT NULL,
serial bigint(20) DEFAULT NULL,
resp_personvarchar(64) DEFAULT NULL,
primary_nsvarchar(64) DEFAULT NULL,
PRIMARY KEY(id),
KEY type(type),
KEY host(host),
KEY zone (zone)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
新建acl表:
CREATE TABLE IF NOT EXISTS acl (
id int(10)unsigned NOT NULL AUTO_INCREMENT,
zone varchar(255) NOT NULL,
client varchar(255) NOT NULL,
PRIMARY KEY(id),
KEY client (client),
KEY zone (zone)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
配置
GeoIP
這塊目前還沒有那麼靈活,基本上都是基於acl來實現的。雖然最新版的bind 9.10支持maxmind的api來做Geo,但還是改寫配置文件的方式。下面是一個示例:
acl "US" {
3.0.0.0/8;
4.0.0.0/25;
4.0.0.128/26;
4.0.0.192/28;
4.0.0.208/29;
4.0.0.216/30;
4.0.0.220/31;
};
view "north_america" {
match-clients { US; CA; MX; };
recursion no;
zone"foos.com" {
type master;
file "pri/foos-north-america.db";
};
};
view "other" {
match-clients { any; };
recursion no;
zone"foos.com" {
type master;
file "pri/foos-other.db";
};
};
該示例引用自這裏
但是我們可以通過DLZ實現GeoIP,二次開發一個自己的driver,然後在driver里根據clientip,結合自己的業務系統實現真正的Geo以及智能業務調度。
Dynamic Record
DLZ新定義了一個配置關鍵字dlz,完整的配置項參考官方文檔,這裏給出簡要說明:
dlz "Mysqlzone" { //定義DLZ標識
database "mysql //database爲dlz這個block唯一可指定的關鍵字,mysql表示使用mysql driver
{host=localhost dbname=dns_datassl=tRue} //連接數據庫的信息
{selectzone from dns_records where zone = '$zone$'} //用於findzone調用,查詢zone
{selectttl, type, mx_priority, case when lower(type)='txt' then concat('\"',data, '\"')
elsedata end from dns_records where zone = '$zone$' and host = '$record$'
andnot (type = 'SOA' or type = 'NS')} //用於lookup調用,查詢record
{selectttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire,minimum
fromdns_records where zone = '$zone$' and (type = 'SOA' or type='NS')} //用於authority調用,查詢SOA或者NS記錄,注意這個配置是可選的,SOA和NS查詢可以放到lookup調用裏,具體見後文
{selectttl, type, host, mx_priority, data, resp_person, serial, refresh, retry,expire,
minimum from dns_records where zone = '$zone$' and not (type = 'SOA' ortype = 'NS')} //用於allnode調用,和接下來的allowzonexfr一起來提供AXFR查詢,可選的配置項
{selectzone from xfr_table where zone = '$zone$' and client = '$client$'} //用於allowzonexfr()調用,用於查詢客戶端是否可發起AXFR查詢,可選的配置項
{updatedata_count set count = count + 1 where zone ='$zone$'}";
};
注意:此配置爲最新Bind版本的配置,如果是打patch的版本請將$換成%,以下的配置同樣。
這裏也給出我的配置:
logging {
channelall {
file "/opt/bind/log/named.log"versions 1;
print-time yes;
severity dynamic;
print-category yes;
print-severity yes;
};
category default { all; };
categoryqueries { all; };
};
options {
directory"/opt/bind/var/";
listen-on-v6 { none; };
listen-on{ any; };
pid-file"/var/run/named.pid";
recursionyes;
allow-transfer {127.0.0.1;};
};
dlz "mysql-dlz"{
database "mysql
{host=localhost dbname=demossl=false port=3306 user=root pass=thinkin}
{selectzone from records where zone = '$zone$' limit 1}
{selectttl, 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 fromrecords where zone = '$zone$' and host = '$record$'}
{}
{selectttl, type, host, mx_priority, data from records where zone = '$zone$' and not(type = 'SOA' or type = 'NS')}
{selectzone from acl where zone = '$zone$' and client = '$client$'}";
};
zone "." IN {
typehint;
file"named.root";
};
key "rndc-key" {
algorithmhmac-md5;
secret"OdEg+tCn/bMe+/2vbJgQvQ==";
};
controls {
inet127.0.0.1 allow { localhost; } keys { "rndc-key"; };
};
注意:這裏的配置開啓了遞歸解析且支持本機發起的AXFR請求。
根zonefile
wget -SO /opt/bind/var/named.root http://www.internic.net/domain/named.root
添加Lmysqlclient 軟鏈接
ln -s /usr/lib64/mysql/libmysqlclient.so /usr/lib
啓動
/opt/bind/sbin/named -n1 -c/opt/bind/etc/named.conf -d9 -g
測試
導入數據
導入records數據:
INSERT INTO dns.records (zone, host, type, data,ttl) VALUES ('111cn.net', 'www', 'A', '1.1.1.1', '60');
INSERT INTO dns.records(zone, host, type, data, ttl) VALUES ('111cn.net', 'cloud', 'A', '2.2.2.2','60');
INSERT INTO dns.records(zone, host, type, data, ttl) VALUES ('111cn.net', 'ns', 'A', '3.3.3.3', '60');
INSERT INTO dns.records (zone, host, type, data,ttl) VALUES ('111cn.net', 'blog', 'CNAME', 'cloud.111cn.net.', '60');
INSERT INTO dns.records (zone, host, type, data,ttl) VALUES ('111cn.net', '@', 'NS', 'ns.111cn.net.', '60');
INSERT INTO dns.records (zone, host, type, ttl, data,refresh, retry, expire, minimum, serial,resp_person) VALUES ('111cn.net', '@', 'SOA', '60', 'ns', '28800', '14400','86400', '86400', '2012020809', 'admin');
導入acl數據:
INSERT INTO demo.acl (zone, client) VALUES('111cn.net', '127.0.0.1');
測試記錄
dig @127.0.0.1 www.111cn.net a
dig @127.0.0.1 blog.111cn.net a
dig @127.0.0.1blog.111cn.net cname
dig @127.0.0.1 111cn.net ns
dig @127.0.0.1 www.111cn.net axfr
數據庫與bind服務器分離
bind服務器IP:192.168.1.110
數據庫服務器IP:192.168.1.111
bind服務器配置:
[root@localhost~]# cat /opt/bind/etc/named.conf
dlz "mysql-dlz" {
database "mysql
{host=192.168.1.111 dbname=dns ssl=false port=3306 user=dns_user1pass=inspur123}
{select zone from records where zone = '$zone$' limit 1}
{select ttl, type, mx_priority, case when lower(type)='txt' thenconcat('\"', data, '\"') when lower(type) = 'soa' then concat_ws(' ',data, resp_person, serial, refresh, retry, expire, minimum) else data end fromrecords where zone = '$zone$' and host = '$record$'}
{}
{select ttl, type, host, mx_priority, data from records where zone ='$zone$' and not (type = 'SOA' or type = 'NS')}
{select zone from acl where zone = '$zone$' and client ='$client$'}";
};
數據庫服務器配置:
配置bind服務器dns數據庫賬號,並授權
MariaDB[dns]> grant select on dns.* to dns_user1@"%" identified by"inspur123";
MariaDB [dns]> use dns
MariaDB [dns]> show tables;
+---------------+
| Tables_in_dns |
+---------------+
| records |
+---------------+
1 row in set (0.00 sec)
MariaDB[dns]>
MariaDB [dns]> select * fromrecords;
+----+-----------+-------+-------+------------------+-----+-------------+---------+-------+--------+---------+------------+-------------+------------+
| id | zone | host | type | data | ttl | mx_priority | refresh |retry | expire | minimum | serial |resp_person | primary_ns |
+----+-----------+-------+-------+------------------+-----+-------------+---------+-------+--------+---------+------------+-------------+------------+
| 1 | 111cn.net | www | A | 1.1.1.1 | 60 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | 111cn.net | cloud | A |2.2.2.2 | 60 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL |
| 3 | 111cn.net | ns | A | 3.3.3.3 | 60 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 4 | 111cn.net | blog | CNAME |cloud.111cn.net. | 60 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | 111cn.net | @ | NS | ns.111cn.net. | 60| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 6 | 111cn.net | @ | SOA | ns | 60 | NULL | 28800 | 14400 | 86400 | 86400 | 2012020809 | admin |NULL |
+----+-----------+-------+-------+------------------+-----+-------------+---------+-------+--------+---------+------------+-------------+------------+
6 rows in set (0.01 sec)
MariaDB [dns]>