zoukankan      html  css  js  c++  java
  • 【Linux】基于Bind_DLZ和MySQL数据的DNS搭建

    参考:http://www.linuxfromscratch.org/blfs/view/cvs/server/bind.html

    参考:http://bind-dlz.sourceforge.net/

    参考:http://www.shencan.net/index.php/2013/05/21/bind9-dlz-%e6%90%9edns/

    参考:http://www.phpfensi.com/linux/20150507/9927.html

    依赖:libcap libxml2 OpenSSL geoip (MIT) 

    编译参数(官网说明强调编译关闭多线程,即--enable-threads=no):

    [root@dns bind-9.10.2-P4]# ./configure --prefix=/usr/local/bind --enable-epoll --enable-largefile --enable-threads=no --with-dlz-mysql=/usr/local/mysql --disable-openssl-version-check

    生成rndc.cnf:

    [root@dns etc]# cd /usr/local/bind/etc
    [root@dns etc]# ../sbin/rndc-confgen > rndc.conf

    生成named.cnf:

    [root@dns etc]# tail -10 rndc.conf | head -9 | sed s/# //g > named.conf

    编辑named.cnf,添加dlz mysql驱动,参考其他资料也可以配置智能DNS。

    (官方的dlz mysql驱动中有5个查询和1个更新语句,具体每个查询的功能可以参考官方说明)

    key "rndc-key" {
            algorithm hmac-md5;
            secret "THnBnPv//oADGjeKQMoNng==";
    };
    
    controls {
            inet 127.0.0.1 port 953
                    allow { 127.0.0.1; } keys { "rndc-key"; };
    };
    
    options {
            allow-recursion { none; };
            recursion no;
            directory "/usr/local/bind/etc";
            version "leju-db-direct";
            pid-file "named.pid";
    };
    logging {
            channel default_debug {
                    file "/tmp/bind_query.log"  versions 3 size 10m;
                    severity info;
                    print-time yes;
                    print-category yes;
            };
            category queries {
                    default_debug;
            };
    #        category database {
    #                default_debug;
    #        };
    };
    
    dlz "mysql zone" {
       database "mysql
       {host=localhost dbname=name ssl=false port=3307 user=user pass=pass socket=/tmp/mysql_3306.sock}
       {select zone from dns_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 as mydata from dns_records where zone = '$zone$' and host ='$record$' and IsLive=1}";
    };

    官网给的mysql驱动模板:

    dlz "mysql zone" {
        database "mysql
        {host=127.0.0.1 dbname=name ssl=false port=3306 user=user pass=pass}
        {select zone from dns_records where zone = '$zone$' limit 1}
        {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') and IsLive=1}
        {select ttl, type, mx_priority, data from dns_records where zone = '$zone$' and (type = 'SOA' or type='NS')}
        {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') and IsLive=1}
        {select zone from xfr_table where zone = '$zone$' and client = '$client$'}";
    };

    模板的查询需要根据环境修改。

    如果驱动部分存在查询问题,或者表中值定义存在异常会出现以下报错:

    23-Sep-2015 11:39:47.621 dns_rdata_fromtext: buffer-0x7fffc7277da0:1: near eof: unexpected end of input
    23-Sep-2015 11:39:47.621 dns_sdlz_putrr returned error. Error code was: unexpected end of input

    参考:http://blog.163.com/digoal@126/blog/static/163877040201110235657127/

    数据库建表:

    CREATE TABLE `dns_records` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `zone` varchar(255) NOT NULL,
      `host` varchar(255) NOT NULL DEFAULT '@',
      `type` enum('MX','CNAME','NS','SOA','A','PTR') NOT NULL,
      `data` varchar(255) DEFAULT NULL,
      `ttl` int(11) NOT NULL DEFAULT '800',
      `mx_priority` int(11) DEFAULT NULL,
      `refresh` int(11) NOT NULL DEFAULT '3600',
      `retry` int(11) NOT NULL DEFAULT '3600',
      `expire` int(11) NOT NULL DEFAULT '86400',
      `minimum` int(11) NOT NULL DEFAULT '3600',
      `serial` bigint(20) NOT NULL DEFAULT '2008082700',
      `resp_person` varchar(64) NOT NULL DEFAULT 'root.domain.com.',
      `primary_ns` varchar(64) NOT NULL DEFAULT 'ns1.domain.com.',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

    插入数据:

    mysql> insert INTO dns_records  (zone,host,type,data,ttl,retry) values ('helloworld.com','bbs','A','192.168.1.101',86400,15);
    mysql> insert INTO dns_records  (zone,host,type,data,ttl,retry) values ('helloworld.com','news','A','192.168.1.102',86400,15);

    debug启动:

    [root@dns sbin]# ./named -g -d 1
    [root@dns sbin]# ./named -c ../etc/named.cnf #启动服务
     

    如果出现MySQL无法建立连接的报错,例如:

    mysql driver failed to create database connection after 4 attempts

    我通过在dlz mysql驱动中使用socket方式连接数据库解决这个问题。

    强制手动刷新

    [root@dns sbin]# ./rndc  flush

    停止服务

    [root@dns sbin]# ./rndc stop

    DNS数据说明

    type

    type字段用于说明RR的类型。常用的RR类型如下:
    1、区记录
    SOA (Start Of Authority):SOA记录标示一个授权区定义的开始。SOA 记录后的所有信息是控制这个区的。
    NS (Name Server):标识区的域名服务器以及授权子域。
    2、基本记录
    A (Address):用于将主机名转换为 IP 地址,任何一个主机都只能有一个A记录。
    PTR (PoinTeR):将地址转换为主机名。
    MX (Mail eXchanger):邮件交换记录。控制邮件的路由。
    3、安全记录
    KEY (Public Key):储存一个关于DNS 名称的公钥。
    NXT (Next):与 DNSSEC 一起使用,用于指出一个特定名称不在域中。
    SIG (Signatrue):指出带签名和身份认证的区信息,细节见 RFC 2535。
    4、可选记录
    CNAME (Canonical NAME):给定主机的别名,主机的规范名在A记录中给出。
    SRV (Services):指出知名网络服务的信息。
    TXT (Text):注释或非关键的信息。
    RR的顺序:
    SOA RR应该放在最前面。通常NS RR紧跟在SOA RR之后,其他记录的顺序无关紧要。

    data

    存放该行host+zone域名解析的IP。

    ttl (time to live)

    它以秒为单位定义该资源记录中的信息存放在高速缓存中的时间长度。通常省略该字段,而使用位于文件开始处的$TTL语句所指定值。

    SerialNumber

    本区配置数据的序列号,用于从服务器判断何时获取最新的区数据。

    可以是 32 位的任何整数,每当更新区文件时都应该增加此序列号的值,否则 named 将不会把区的更新数据传送到从服务器。

    Refresh

    辅助域名服务器多长时间更新数据库。Refresh 1 到 6 小时。

    Retry

    若辅助域名服务器更新数据失败,多长时间再试。Retry 20 到 60 分钟。

    Expire

    若辅助域名服务器无法从主服务器上更新数据,原有的数据何时失效。Expire 1 周 到 1 月。

    Minimum

    设置被缓存的否定回答的存活时间。Minimum 1 到 3 小时。

    缓存时间字段 Refresh、Retry、Expire、Minimum 可以使用时间单位字符m、h、d、w分别表示分钟、小时、天、星期。

    resp_person

    区域管理员邮箱

    primary_ns

    区域主节点机器名

  • 相关阅读:
    Kafka0.10.2.0分布式集群安装
    Windows下本机简易监控系统搭建(Telegraf+Influxdb+Grafana)
    Influxdb1.2.2安装
    Grafana4.2安装
    Zookeeper3.4.9分布式集群安装
    Grafana中多租户设置
    Grafana关键表结构分析
    Spark2.1.0分布式集群安装
    Hadoop2.7.3分布式集群安装
    JVM学习--(七)性能监控工具
  • 原文地址:https://www.cnblogs.com/jiangxu67/p/4801230.html
Copyright © 2011-2022 走看看