• 智能DNS(Bind DLZ MYSQL )实际部署案例


    XX网NS服务器为FreeBSD下的Bind master/slave ,起初因为访问量不太大,这个架构完全可以胜任,但是随着域名的增加,维护zone文件变得越来越复杂,一旦书写有误,会造成Bind无法解析域名。所以,特此部署了Bind DLZ Mysql,经过一个月的上线测试,发现其负载很小,每秒5qps。不存在mysql time out问题,因为mysql每时每刻都在查询.在这里感谢老曹的鼎力相助!

    目录

    一、相关术语简介

    二、智能DNS系统服务规划

    三、MYSQLReplication及添加GoogleTCMALLOC库降低系统负载

    四、安装配置Bind-DLZ 及相关脚本

    五、测试Bind-DLZ相关总结

    相关术语简介:
    1、智能DNSBind-view
    智能DNS的原理很简单:在用户解析一个域名的时候,判断一下用户的IP,然后跟DNS服务器内部的IP表匹配一下,看看用户是电信还是网通用户,然后给用户返回对应的IP地址。目前的域名服务运营商不提供智能DNS服务,所以必须自行架设DNS服务或者使用网上免费的智能DNS服务,如DNSPOD.

    2Bind-DLZ
    Bind-DLZ主页:http://bind-dlz.sourceforge.net/
    DLZ(Dynamically Loadable Zones)与传统的BIND9不同,BIND的不足之处:
    * BIND从文本文件中获取数据,这样容易因为编辑错误出现问题。
    * BIND需要将数据加载到内存中,如果域或者记录较多,会消耗大量的内存。
    * BIND启动时解析Zone文件,对于一个记录较多的DNS来说,会耽误更多的时间。
    * 如果近修改一条记录,那么要重新加载或者重启BIND才能生效,那么需要时间,可能会影响客户端查询。
    Bind-dlz 即将帮你解决这些问题, Zone文件操作也更方便了,直接对数据库操作,可以很方
    便扩充及开发管理程序。

    二、智能DNS系统服务规划

    1NameServer服务器注册(到新网或者万网后台添加)
    ns1.soshandong.com  192.19.13.15
    ns2.soshandong.com  192.19.11.3

    NS1master NS2slave。两者数据通过mysql来同步。

    2、测试NS记录是否生效

    #digns www.soshandong.com

    #digwww.soshandong.com+trace

    3Bind-View规划
    www.soshandong.com 网通 (CNC)  124.133.11.78
    www.soshandong.com
    电信(TELECOM) 58.56.11.153
    www.soshandong.com 移动(ANY) 120.192.11.13

     

    三、MYSQLReplicationUbuntu上的安装

    Ubuntu上安装Mysql 5.1.51

    因新安装的Ubuntu没有zlib-dev库和ncurses,需要执行apt-get 来安装他们

    #apt-get install zlib1g-dev ncurses

    首先下载mysql-5.1.51.tar.gz,并解压.

    #cd mysql-*

    #sh mysql.sh

    脚本执行完毕后,将my.cnf放到/etc

    # mv my.cnf /etc/

    默认脚本会启动mysql,如果报错,会在数据库目录里有错误日志。


    Mysql replication
    配置

    1、Mysql安全设置

    *  mysqladmin命令来改root用户口令 mysqladmin -uroot password linuxtone.org//设置MySQL管理操作用户root的密码为52netseek

    *  setpassword修改口令:
    mysql> set password for root@localhost=password('linuxtone.org);

    *  直接修改user表的root用户口令   
    mysql> use mysql;
    mysql> update user set password=password('linuxtone.org') where user='root';
    mysql> flush privileges;

    2.删除默认的数据库和用户我们的数据库是在本地,并且也只需要本地的php脚本对mysql进行读取,所以很多用户不需要。m
    ysql
    初始化后会自动生成空用户和test库,这会对数据库构成威胁,我们全部删除。我们使用mysql客户端程序连接到本地的mysql服务器后出现如下提示:
    mysql> drop database test;
    mysql> use mysql;
    mysql> delete from db;
    mysql> delete from user where not(host="localhost" anduser="root");
    mysql> flush privileges;

    Mysql replication设置
    3Master 机器设置权限,赋予Slave机器FILEReplication Slave权利,并打包要同步的数据库结构。

    Master#./mysql –u root –plinuxtone.org
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 2 to server version: 5.1.51

    Type 'help;'or '\h' for help. Type '\c' to clear the buffer.
    mysql> GRANTFILE ON*.* TO slaverep@172.19.1.3 IDENTIFIEDBY ‘slaverep’;
    mysql>GRANT REPLICATION SLAVE ON *.* TO slaverep@172.19.1.3 IDENTIFIEDBY ‘linuxtone.org’;
    mysql>Flushprivilege;
    赋予192.19.11.3也就是Slave 机器有File权限, 这个5.1.51版对replication的权限好像做了调整,只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。
    Master 导入CDNSql文件建立CDN数据结构
    #mysql–uroot –plinuxtone.org
    Mysql>create database cdn;
    Mysql>\q

    #mysql
    cdn –uroot –plinuxtone.org <cdn.sql #cdn.sql
    位置
    这样master 就有了CDN库的数据结构。
    Slave一样。这样不再重复
    2设置主服务器Mastermy.cnf,启动Mysql服务
    Master# vi/etc/my.cnf
    [mysqld]添加或修改以下的
    [mysqld]
    log-bin = /home/data/mysql/data/binlog/binlog#
    打开logbin选项以能写到slave I/O线程;
    server-id=1 #
    表示是本机的序号为1,一般来讲就是master的意思.

    binlog-do-db= cdn #表示同步cdn数据库;
    PS:提供的MyCnf 比较详细。
    然后把Master主服务器的Mysql重启。
    Master# servicemysqld restart

    4、修改Slave服务器的my.cnf

    Slave# vi/etc/my.cnf

    [mysqld]添加或修改以下的
    master-host=192.19.13.15
    master-user=slaverep
    master-password=linuxtone.org
    master-port=3306
    server-id=10
    master-connect-retry=60
    replicate-do-db=cdn
    [要更新的数据库]
    log-slave-updates

    5、删除Slave端数据库目录中的master.info
    Slave# rm -fmaster.info
    6、重启动Slaveslave start
    Slave#servicemysqld restart

     
    7、测试先检测两个Mysql数据库中的cdn是否正常。正常情况应该是MasterSlave 中的Mysql 都有相同的cdn 数据库,并且里面的数据都一样。然后测试replication 功能是否起用。Master中的reptest数据库添加一笔数据:
    Master#mysql –u root -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 12 to server version: 5.1.51

    Type 'help;'or '\h' for help. Type '\c' to clear the buffer.
    mysql>use cdn;
    Database changed
    mysql> INSERT INTO rep_table VALUES ('test1', '4321', 'T',24);
    Query OK, 1 row affected (0.00 sec)
    mysql>

    然后查看Slave机器的reptest数据库:
    Slave#mysql–u root –p
    Enterpassword:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 12 to server version: 5.1.49

    Type 'help;'or '\h' for help. Type '\c' to clear the buffer.
    mysql>use cdn;
    Database changed
    mysql>select * from reptable;;

    +------+------+------+------+
    | id | name| sex | age |
    +------+------+------+------+
    | test1|4321 | T | 24 |
    +------+------+------+------+
    1 row in set(0.00 sec)

    mysql 添加TCMalloc库降低系统负载

    TCMallocThread-CachingMalloc)是google开发的开源工具──google-perftools中的成员。与标准的glibc库的malloc相比,TCMalloc在内存的分配上效率和速度要高得多,可以在很大程度上提高MySQL服务器在高并发情况下的性能,降低系统负载。

    164位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API

    wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
    tar zxvf libunwind-0.99.tar.gz
    cd libunwind-0.99/
    CFLAGS=-fPIC ./configure
    make CFLAGS=-fPIC
    make CFLAGS=-fPIC install

    2、安装google-perftools

    wget http://google-perftools.googlecode.com/files/google-perftools-1.6.tar.gz
    tar zxvf google-perftools-1.6.tar.gz
    cd google-perftools-1.6/
    ./configure
    make && make install

    echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf
    /sbin/ldconfig

     

    3、修改MySQL启动脚本(根据你的MySQL安装位置而定):

    vi /usr/local/mysql/bin/mysqld_safe


      在# executingmysqld_safe的下一行,加上:

    export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

    保存后退出,然后重启MySQL服务器。

    4、使用lsof命令查看tcmalloc是否起效: lsof -n | grep tcmalloc

    如果发现以下信息,说明tcmalloc已经起效:

    mysqld 10847 mysql mem REG 8,5 1203756 20484960/usr/local/lib/libtcmalloc.so.0.0.0

    .安装配置Bind-DLZ 及相关脚本

    1.安装bind

    #mkdir /usr/local/src/bind-dlz
    #cd /usr/local/src/bind-dlz
    #wget http://ftp.isc.org/isc/bind9/9.6.0-P1/bind-9.6.0-P1.tar.gz
    #tar zxvf bind-9.6.0-P1.tar.gz

    #cd bind-9.6.0-P1
    #./configure --with-dlz-mysql --enable-largefile --enable-threads=no--prefix=/usr/local/bind

    #make –j4 && make install

    2.创建相关配置文件

    cd /usr/local/bind/etc
    ../sbin/rndc-confgen >rndc.conf
    tail -n10 rndc.conf | head -n9 | sed -e s/#\//g >named.conf

    # vilocalhost.zone

    ttl 86400
    @ IN SOA localhost. root.localhost. (
    1997022700 ; Serial
    28800 ; Refresh
    14400 ; Retry
    3600000 ; Expire
    86400 ) ; Minimum
    IN NS localhost.
    1 IN PTR localhost.

    # dig >named.root
    #vi named.conf
    在后面加入如下:

    include"/usr/local/bind/etc/cnc_acl.conf"; //网通ACL
    include "/usr/local/bind/etc/telecom_acl.conf"; //
    电信ACL
    include "/usr/local/bind/etc/edu_acl.conf"; //
    教育网ACL
    include "/usr/local/bind/etc/view.conf"; //DLZ
    相关的配

    3、配置DNSTSIG

    1、使用dnssec-keygenfunction 产生加密密钥,一个为public key,另一

    个为private key,本文假设应用服务器存在CNC,TELECOM,EDU,ANY

    (1)产生加密金钥

    #cd /usr/local/bind/sbin

    #./dnssec-keygen -a hmac-md5 -b 128 -n HOSTcnc

    #./dnssec-keygen -a hmac-md5 -b 128 -n HOSTtelecom

    #./dnssec-keygen -ahmac-md5 -b 128 -n HOST edu
    #./dnssec-keygen -a hmac-md5 -b 128 -n HOST any
    (2)
    查看生成的密钥文件

    # cat Kcnc.+157+24406.private //以网通为例.

    Private-key-format: v1.2

    Algorithm: 157 (HMAC_MD5)

    Key: YTjTOw00PzeEaasA16/Rvw==

    Bits: AAA=

    :YTjTOw00PzeEaasA16/Rvw== 加入到named.conf,其它同例。

    详细请参照named.conf配置文件

    2、配置named.conf

    #vi /usr/local/bind/etc/named.conf

    key "rndc-key" {

    algorithm hmac-md5;

    secret "5PubnjGuAWeH9F2dIUYd6g==";

    };

    controls {

    inet 127.0.0.1 port 953

    allow { 127.0.0.1; } keys {"rndc-key"; };

    };

    #

    options {

    directory "/usr/local/bind/etc";

    pid-file "named.pid";

    };

    #TSIG-key

    key "cnc" {

    algorithm hmac-md5;

    secret "YTjTOw00PzeEaasA16/Rvw==";

    };

    key "telecom" {

    algorithm hmac-md5;

    secret"pUcQGLpSH2tQgVZ9ZHU6Yg==";

    };

    key "edu"{

    algorithm hmac-md5;

    secret"Bzo6MTzrzbRFQbONYTS1Cw==";

    };

    key "any"{

    algorithm hmac-md5;

    secret"DHpPfGJdMLv91OygBf9H6w==";

    };

    #acl

    acl"dns-ip-list"{

    172.19.3.15; #masterDNS IP

    172.19.1.3; #slaveDNS IP

    };

    #acl include

    include"/usr/local/bind/etc/cnc_acl.conf"; //网通ACL

    include"/usr/local/bind/etc/telecom_acl.conf"; //电信 ACL

    include"/usr/local/bind/etc/edu_acl.conf"; //教育网ACL

    include"/usr/local/bind/etc/view.conf"; //bind-view部分

    3Bind 启动脚本

    #!/bin/sh

    # powered bydzwww.com

    case"$1" in

    start)

    if [ -x/usr/local/bind/sbin/named ]; then

    /usr/local/bind/sbin/named-c /usr/local/bind/etc/named.conf -u bind

    echo"BIND9-named server started"

    fi

    ;;

    stop)

    kill `cat/usr/local/bind/etc/named.pid` && echo . && echo 'BIND9 serverstopped'

    ;;

    restart)

    echo .

    echo"Restart BIND9 server"

    $0 stop

    sleep 10

    $0 start

    ;;

    reload)

    /usr/local/bind/sbin/rndcreload

    ;;

    status)

    /usr/local/bind/sbin/rndcstatus

    ;;

    *)

    echo"$0 start | stop | restart |reload |status"

    ;;

    esac

    ./bind9 start 启动bind./bind9 stop停止bind;
    ./bind9 reload #
    重载配置文件

    剩余文档见打包的bind-dlz.tar.gz。里面很详细。部署的master时候用tar zvxf bind-dlz-tar.gz –C /usr/local/bind/etc覆盖即可,剩下的slave在部署bind还是将配置文件解压到/usr/local/bind/etc/即可,将rndc-key dnssec重新配置一遍就可以用了!

    部署DNS,防火墙和路由器要设置清楚,我部署的时候就是因为黑洞防火墙没有对masterslave服务器开放tcpudp53端口,造成不能解析域名。需要大家切记!
    MYSQL Master info

    1.jpg

    bind 配置文档及最新修订IP库

    etc.zip (8.39 KB, 下载次数: 78)

  • 相关阅读:
    java.io.EOFException ValueOperations.increment()操作后,获取值时有的bug
    使用maven profile指定配置文件打包适用多环境
    关于3Q大战和反垄断
    在ECUG2010上的演讲稿
    让Windows7在启动时自动挂载虚拟磁盘
    也谈并行计算(一)C#版的Parallel.For实现
    给.NET的string类添加一个命令行参数分解的扩展
    顺序表 code
    很高兴开始博客之旅 code
    (原)前端知识杂烩(css系列)
  • 原文地址:https://www.cnblogs.com/studio313/p/1889522.html
Copyright © 2020-2023  润新知