本文章為各位介紹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:
- wget ftp://ftp.isc.org/isc/bind9/9.10.1/bind-9.10.1.tar.gz
- tar xzf bind-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,
- data varchar(255) NOT NULL,
- ttl int(11) NOT NULL,
- mx_priority int(11) DEFAULT NULL,
- refresh int(11) DEFAULT NULL,
- retry int(11) DEFAULT NULL,
- expire int(11) DEFAULT NULL,
- minimum int(11) DEFAULT NULL,
- serial bigint(20) DEFAULT NULL,
- resp_person varchar(64) DEFAULT NULL,
- primary_ns varchar(64) DEFAULT NULL,
- PRIMARY KEY (id), --Vevb.com
- 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里根據client ip,結合自己的業務系統實現真正的Geo以及智能業務調度.
Dynamic Record
DLZ新定義了一個配置關鍵字dlz,完整的配置項參考官方文檔,這里給出簡要說明:
- dlz "Mysql zone" { //定義DLZ標識
- database "mysql //database為dlz這個block唯一可指定的關鍵字,mysql表示使用mysql driver
- {host=localhost dbname=dns_data ssl=tRue} //連接數據庫的信息
- {select zone from dns_records where zone = '$zone$'} //用于findzone調用,查詢zone
- {select ttl, type, mx_priority, case when lower(type)='txt' then concat('/"', data, '/"')
- else data end from dns_records where zone = '$zone$' and host = '$record$'
- and not (type = 'SOA' or type = 'NS')} //用于lookup調用,查詢record
- {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum
- from dns_records where zone = '$zone$' and (type = 'SOA' or type='NS')} //用于authority調用,查詢SOA或者NS記錄,注意這個配置是可選的,SOA和NS查詢可以放到lookup調用里,具體見后文
- {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire,
- minimum from dns_records where zone = '$zone$' and not (type = 'SOA' or type = 'NS')} //用于allnode調用,和接下來的allowzonexfr一起來提供AXFR查詢,可選的配置項
- {select zone from xfr_table where zone = '$zone$' and client = '$client$'} //用于allowzonexfr()調用,用于查詢客戶端是否可發起AXFR查詢,可選的配置項
- {update data_count set count = count + 1 where zone ='$zone$'}";
- };
注意:此配置為最新Bind版本的配置,如果是打patch的版本請將$換成%,以下的配置同樣,這里也給出我的配置:
- logging {
- channel all {
- file "/opt/bind/log/named.log" versions 1;
- print-time yes;
- severity dynamic;
- print-category yes;
- print-severity yes;
- };
- category default { all; };
- category queries { all; };
- };
- options {
- directory "/opt/bind/var/";
- listen-on-v6 { none; };
- listen-on { any; };
- pid-file "/var/run/named.pid";
- recursion yes;
- allow-transfer {127.0.0.1;};
- };
- dlz "mysql-dlz" {
- database "mysql
- {host=localhost dbname=demo ssl=false port=3306 user=root pass=thinkin}
- {select zone from records where zone = '$zone$' limit 1}
- {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 records 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$'}";
- };
- zone "." IN {
- type hint;
- file "named.root";
- };
- key "rndc-key" {
- algorithm hmac-md5;
- secret "OdEg+tCn/bMe+/2vbJgQvQ==";
- };
- controls {
- inet 127.0.0.1 allow { localhost; } keys { "rndc-key"; };
- };
注意:這里的配置開啟了遞歸解析且支持本機發起的AXFR請求。
根zonefile
wget -SO /opt/bind/var/named.root http://www.internic.net/domain/named.root
啟動:/opt/bind/sbin/named -n1 -c /opt/bind/etc/named.conf -d9 -g
測試,導入數據,導入records數據:
- INSERT INTO demo.records (zone, host, type, data, ttl) VALUES (Vevb.com', 'www', 'A', '1.1.1.1', '60');
- INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com', 'cloud', 'A', '2.2.2.2', '60');
- INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com', 'ns', 'A', '3.3.3.3', '60');
- INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com', 'blog', 'CNAME', 'cloud.Vevb.com.', '60');
- INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com', '@', 'NS', 'ns.Vevb.com.', '60');
- INSERT INTO demo.records (zone, host, type, ttl, data,refresh, retry, expire, minimum, serial, resp_person) VALUES ('Vevb.com', '@', 'SOA', '60', 'ns', '28800', '14400', '86400', '86400', '2012020809', 'admin');
導入acl數據:
INSERT INTO demo.acl (zone, client) VALUES ('Vevb.com', '127.0.0.1');
測試記錄:
- dig @127.0.0.1 m.survivalescaperooms.com a
- dig @127.0.0.1 blog.Vevb.com a
- dig @127.0.0.1 blog.Vevb.com cname
- dig @127.0.0.1 Vevb.com ns
- dig @127.0.0.1 m.survivalescaperooms.com axfr
新聞熱點
疑難解答