国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Bind基于DLZ實現智能DNS配置教程

2024-08-28 00:00:03
字體:
來源:轉載
供稿:網友

本文章為各位介紹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:

  1. wget ftp://ftp.isc.org/isc/bind9/9.10.1/bind-9.10.1.tar.gz 
  2. tar xzf bind-9.10.1.tar.gz 
  3. cd  bind-9.10.1 

配置:由于CentOS7目錄結構上的變更,在編譯dlz-mysql時會找不到庫文件或者head文件,所以要做個軟連接:

  1. ln -s /usr/lib/mysql /usr/lib64/mysql 
  2. ./configure --prefix /opt/bind --with-dlz-filesystem --with-dlz-mysql 

編譯:make

安裝:make install

模型:

注意:DLZ沒有限制用戶的數據模型,你可以根據業務邏輯定義模型,然后構造自己的查詢語句即可,官方給出了建議的模型.

建模:

  1. Field Type Null Key Default Extra 
  2. zone text YES  NULL  
  3. host text YES  NULL  
  4. type text YES  NULL  
  5. data text     
  6. ttl int(11) YES  NULL  
  7. mx_priority text YES  NULL  
  8. refresh int(11) YES  NULL  
  9. retry int(11) YES  NULL  
  10. expire int(11) YES  NULL  
  11. minimum int(11) YES  NULL  
  12. serial bigint(20) YES  NULL  
  13. resp_person text YES  NULL  
  14. primary_ns text YES  NULL  
  15. zone 區域 
  16. host 記錄名 
  17. type 記錄類型 
  18. data 記錄值 
  19. ttl 緩存時間 
  20. mx_priority mx記錄優先級 
  21. refresh SOA記錄的刷新時間 
  22. retry SOA記錄的重試時間 
  23. expire SOA記錄的過期時間 
  24. minimum SOA記錄的minimum 
  25. serial SOA記錄的序列號 
  26. resp_person SOA記錄的序列號 
  27. primary_ns <尚不明確這個字段的意義> 

建庫建表,新建數據庫:

create database demo;

新建record表:

  1. CREATE TABLE IF NOT EXISTS records ( 
  2.   id int(10) unsigned NOT NULL AUTO_INCREMENT, 
  3.   zone varchar(255) NOT NULL
  4.   host varchar(255) NOT NULL
  5.   type enum('A','MX','CNAME','NS','SOA','PTR','TXT','AAAA','SVR','URL'NOT NULL
  6.   data varchar(255) NOT NULL
  7.   ttl int(11) NOT NULL
  8.   mx_priority int(11) DEFAULT NULL
  9.   refresh int(11) DEFAULT NULL
  10.   retry int(11) DEFAULT NULL
  11.   expire int(11) DEFAULT NULL
  12.   minimum int(11) DEFAULT NULL
  13.   serial bigint(20) DEFAULT NULL
  14.   resp_person varchar(64) DEFAULT NULL
  15.   primary_ns varchar(64) DEFAULT NULL
  16.   PRIMARY KEY (id),  --Vevb.com 
  17.   KEY type (type), 
  18.   KEY host (host), 
  19.   KEY zone (zone) 
  20. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

新建acl表:

  1. CREATE TABLE IF NOT EXISTS acl ( 
  2.   id int(10) unsigned NOT NULL AUTO_INCREMENT, 
  3.   zone varchar(255) NOT NULL
  4.   client varchar(255) NOT NULL
  5.   PRIMARY KEY (id), 
  6.   KEY client (client), 
  7.   KEY zone (zone) 
  8. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

配置:GeoIP

這塊目前還沒有那么靈活,基本上都是基于acl來實現的,雖然最新版的bind 9.10支持maxmind的api來做Geo,但還是改寫配置文件的方式,下面是一個示例:

  1. acl "US" { 
  2.      3.0.0.0/8; 
  3.      4.0.0.0/25; 
  4.      4.0.0.128/26; 
  5.      4.0.0.192/28; 
  6.      4.0.0.208/29; 
  7.      4.0.0.216/30; 
  8.      4.0.0.220/31; 
  9. }; 
  10. view "north_america" { 
  11.       match-clients { US; CA; MX; }; 
  12.       recursion no
  13.       zone "foos.com" { 
  14.             type master; 
  15.             file "pri/foos-north-america.db"
  16.       }; 
  17. }; 
  18. view "other" { 
  19.       match-clients { any; }; 
  20.       recursion no
  21.       zone "foos.com" { 
  22.             type master; 
  23.             file "pri/foos-other.db"
  24.       }; 
  25. }; 

該示例引用自這里,但是我們可以通過DLZ實現GeoIP,二次開發一個自己的driver,然后在driver里根據client ip,結合自己的業務系統實現真正的Geo以及智能業務調度.

Dynamic Record

DLZ新定義了一個配置關鍵字dlz,完整的配置項參考官方文檔,這里給出簡要說明:

  1. dlz "Mysql zone" { //定義DLZ標識 
  2.    database "mysql //database為dlz這個block唯一可指定的關鍵字,mysql表示使用mysql driver 
  3.    {host=localhost dbname=dns_data ssl=tRue} //連接數據庫的信息 
  4.    {select zone from dns_records where zone = '$zone$'} //用于findzone調用,查詢zone 
  5.    {select ttl, type, mx_priority, case when lower(type)='txt' then concat('/"', data, '/"'
  6.         else data end from dns_records where zone = '$zone$' and host = '$record$' 
  7.         and not (type = 'SOA' or type = 'NS')} //用于lookup調用,查詢record 
  8.    {select ttl, type, mx_priority, data, resp_person, serial, refresh, retry, expire, minimum 
  9.         from dns_records where zone = '$zone$' and (type = 'SOA' or type='NS')} //用于authority調用,查詢SOA或者NS記錄,注意這個配置是可選的,SOA和NS查詢可以放到lookup調用里,具體見后文 
  10.    {select ttl, type, host, mx_priority, data, resp_person, serial, refresh, retry, expire, 
  11.         minimum from dns_records where zone = '$zone$' and not (type = 'SOA' or type = 'NS')} //用于allnode調用,和接下來的allowzonexfr一起來提供AXFR查詢,可選的配置項 
  12.    {select zone from xfr_table where zone = '$zone$' and client = '$client$'} //用于allowzonexfr()調用,用于查詢客戶端是否可發起AXFR查詢,可選的配置項 
  13.    {update data_count set count = count + 1 where zone ='$zone$'}"; 
  14. }; 

注意:此配置為最新Bind版本的配置,如果是打patch的版本請將$換成%,以下的配置同樣,這里也給出我的配置:

  1. logging { 
  2.     channel all { 
  3.         file "/opt/bind/log/named.log" versions 1; 
  4.         print-time yes; 
  5.         severity dynamic; 
  6.         print-category yes; 
  7.         print-severity yes; 
  8.     }; 
  9. category default { all; }; 
  10. category queries { all; }; 
  11.  
  12. }; 
  13.  
  14. options { 
  15.     directory "/opt/bind/var/"
  16.     listen-on-v6 { none; }; 
  17.     listen-on { any; }; 
  18.     pid-file "/var/run/named.pid"
  19.     recursion yes; 
  20.     allow-transfer {127.0.0.1;}; 
  21. }; 
  22.  
  23. dlz "mysql-dlz" { 
  24.     database "mysql 
  25.     {host=localhost dbname=demo ssl=false port=3306 user=root pass=thinkin} 
  26.     {select zone from records where zone = '$zone$' limit 1} 
  27.     {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$'
  28.     {} 
  29.     {select ttl, type, host, mx_priority, data from records where zone = '$zone$' and not (type = 'SOA' or type = 'NS')} 
  30.     {select zone from acl where zone = '$zone$' and client = '$client$'}"; 
  31. }; 
  32.  
  33. zone "." IN { 
  34.     type hint; 
  35.     file "named.root"
  36. }; 
  37.  
  38. key "rndc-key" { 
  39.     algorithm hmac-md5; 
  40.         secret "OdEg+tCn/bMe+/2vbJgQvQ=="
  41. }; 
  42.  
  43. controls { 
  44.         inet 127.0.0.1 allow { localhost; } keys { "rndc-key"; }; 
  45. }; 

注意:這里的配置開啟了遞歸解析且支持本機發起的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數據:

  1. INSERT INTO demo.records (zone, host, type, data, ttl) VALUES (&#Vevb.com''www''A''1.1.1.1''60'); 
  2. INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com''cloud''A''2.2.2.2''60');  
  3. INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com''ns''A''3.3.3.3''60'); 
  4. INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com''blog''CNAME''cloud.Vevb.com.''60'); 
  5. INSERT INTO demo.records (zone, host, type, data, ttl) VALUES ('Vevb.com''@''NS''ns.Vevb.com.''60'); 
  6. 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');

測試記錄:

  1. dig @127.0.0.1 m.survivalescaperooms.com a 
  2. dig @127.0.0.1 blog.Vevb.com a 
  3. dig @127.0.0.1 blog.Vevb.com cname 
  4. dig @127.0.0.1 Vevb.com ns 
  5. dig @127.0.0.1 m.survivalescaperooms.com axfr

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁津县| 靖江市| 天台县| 新密市| 丽江市| 精河县| 大化| 霞浦县| 黄陵县| 安化县| 汉阴县| 察哈| 卓资县| 凉城县| 龙里县| 个旧市| 萍乡市| 尖扎县| 西畴县| 永济市| 丰城市| 彰武县| 惠东县| 贵州省| 深州市| 南阳市| 沾益县| 高州市| 阳新县| 柳州市| 元阳县| 龙里县| 郯城县| 灵丘县| 佛山市| 虹口区| 隆安县| 涟源市| 崇左市| 丹棱县| 普兰县|