[docker]bind9.11-with-mysql5.6 docker容器化实战
参考:
https://www.centos.bz/2012/09/bind-with-mysql-support/
http://blog.51niux.com/?id=125
http://4709096.blog.51cto.com/4699096/1599424
这些编译安装的东西没必要深究,没任何意义.简单方便为好.
1. bind物理机安装
-
需求: 找一个有api的dns.可以直接增加mysql记录不用重启dns即可动态更新,于是动手搞一套mysqlbackend+bind.
后来发现有 powerdns dj-dns也可以实现这些功能,没必要自己搞. -
思路: 先搞清楚物理机怎么编译bind-->容器化mysql--> 容器化mysql+supervisor-->容器化mysql+supervisor+bind
-
目前最新的bind集合mysql不需要改bind源代码了,改起来很烦的.
-
bind和mysql本来不想安装在一台机器上,无奈编译bind需要依赖mysql的lib库(期间我尝试按照mysql-devel还是没能解决),这里已经解决了. 我用debian:jessie为基础
apt-get install -y g++ libboost-all-dev libtool make pkg-config libmysqlclient-dev libssl-dev
然后编译安装bind9.11就可以了.见文末
- 1.1二进制安装 mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
yum -y install perl-devel
tar -xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
ln -s mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local/mysql
useradd mysql -s /sbin/nologin -M
chown -R mysql:mysql /usr/local/mysql/
cd /usr/local/mysql/support-files/
\cp my-default.cnf /etc/my.cnf ## 这个配置文件可以用第二部分my.conf改了字符集
\cp mysql.server /etc/init.d/mysqld ## 这样/etc/init.d/mysqld start 就可以启动了
/usr/local/mysql/bin/mysqladmin -u root password '123456'
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
# 初始化-物理机这样操作,如果在容器部分,则可以将初始化好的/data目录替换mysql二进制包里的data,之后就不需要构建镜像时候初始化了.
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 启动
/usr/local/mysql/bin/mysqld_safe --user=mysql #前台启动
/usr/local/mysql/bin/mysqld_safe --user=mysql &
- 1.2编译安装bind-9.11.2.tar.gz
一定要编译安装,加上with-dlz-mysql, yum安装默认是不支持mysql backend的.
echo "/usr/local/mysql/lib" >>/etc/ld.so.conf \
ldconfig
yum install -y perl-devel openssl-devel
cd /usr/local/src/
tar xf bind-9.11.2.tar.gz
cd bind-9.11.2/ \
&& ./configure --prefix=/usr/local/bind \
--with-dlz-mysql=/usr/local/mysql \
--enable-largefile \
--disable-openssl-version-check \
--enable-threads \
--disable-ipv6
&& make && make install
./configure --prefix=/usr/local/bind \
--with-dlz-mysql \
--enable-largefile \
--disable-openssl-version-check \
--enable-threads \
--disable-ipv6
make
- 1.3配置named
cat /usr/local/bind/etc/named.conf
logging {
channel query_log {
file "/var/log/named.log" versions 3 size 100m;
severity info;
print-time yes;
print-category yes;
print-severity yes;
};
category queries {
query_log;
};
category lame-servers {
null;
};
};
dlz "My zone" {
database "mysql
{host=172.17.92.3 dbname=dns ssl=false port=3306 user=root pass=123456}
{select zone from dns_records where zone = '$zone$'}
{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 dns_records where zone = '$zone$' and host = '$record$'}";
};
- 1.4登录数据库插入示例数据
create database dns;
use dns
CREATE TABLE dns_records(id int(11) unsigned NOT NULL auto_increment,zone varchar(255) NOT NULL,host varchar(255) NOT NULL default '@',type varchar(255) NOT NULL,data text,ttl int(11) default 600,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(255) default NULL,primary_ns varchar(255),PRIMARY KEY(id));
CREATE INDEX host_index ON dns_records(host);
CREATE INDEX type_index ON dns_records(type);
CREATE INDEX zone_index ON dns_records(zone);
insert into dns_records (zone,host,type,data,refresh,retry,expire,minimum,serial,resp_person,primary_ns) values('test.com','@','SOA','ns.test.com',7200,3600,86400,3600,1000,'root.test.com.','ns.test.com.');
insert into dns_records (zone,host,type,data) values('test.com','@','NS','ns.test.com.');
insert into dns_records (zone,host,type,data) values('test.com','ns','A','1.1.1.1');
insert into dns_records (zone,host,type,data) values('test.com','www','A','127.0.0.1');
- 1.5启动调试named
/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g
/usr/local/bind/sbin/named -c /home/bind/etc/named.conf
2.mysql
- Dockerfile
From centos:7
# Init centos
ENV TZ="PRC"
RUN ln -sf \
/usr/share/zoneinfo/Asia/Shanghai \
/etc/localtime
RUN yum install -y autoconf libaio perl-devel make gcc gcc-c++ \
&& yum clean all
COPY my.conf /etc/
ADD mysql.tar.gz /usr/local/
RUN useradd mysql -s /sbin/nologin -M \
&& chown -R mysql:mysql /usr/local/mysql \
&& echo 'echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile' \
&& source /etc/profile
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld_safe","--user=mysql"]
- my.conf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
default-storage-engine = innodb
innodb_file_per_table = 1
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
3.mysql-supervisor
- Dockerfile
From centos:7
# Init centos
ENV TZ="PRC"
RUN ln -sf \
/usr/share/zoneinfo/Asia/Shanghai \
/etc/localtime
RUN yum install -y autoconf libaio perl-devel make gcc gcc-c++ openssl-devel epel-release \
&& yum clean all
#supervisor
RUN yum -y install supervisor && \
mkdir -p /etc/supervisor/ && \
yum clean all
COPY supervisord.conf /etc/supervisor/
COPY my.conf /etc/
ADD mysql.tar.gz /usr/local/
RUN useradd mysql -s /sbin/nologin -M \
&& chown -R mysql:mysql /usr/local/mysql \
&& echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile \
&& source /etc/profile
COPY entry.sh /usr/local/bin/
EXPOSE 3306
ENTRYPOINT ["entry.sh"]
- entry.sh
#!/usr/bin/env bash
## mysql初始化 设置密码
echo "-----------------------------usage------------------------------------------------------"
echo "insert into dns_records (zone,host,type,data) values('test.com','@','NS','ns.test.com.');"
echo "insert into dns_records (zone,host,type,data) values('test.com','ns','A','1.1.1.1');"
echo "insert into dns_records (zone,host,type,data) values('test.com','www','A','127.0.0.1');"
echo "-----------------------------------------------------------------------------------------"
supervisord -c /etc/supervisor/supervisord.conf
- my.conf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
default-storage-engine = innodb
innodb_file_per_table = 1
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
4.bind-with-mysql-supervisor
CentOS-7 bind-9.11.2.tar.gz mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
- Dockerfile
From centos:7
# Init centos
ENV TZ="PRC"
RUN ln -sf \
/usr/share/zoneinfo/Asia/Shanghai \
/etc/localtime
RUN yum install -y autoconf libaio perl-devel make gcc gcc-c++ openssl-devel epel-release \
&& yum clean all
#supervisor
RUN yum -y install supervisor && \
mkdir -p /etc/supervisor/ && \
yum clean all
COPY supervisord.conf /etc/supervisor/
# mysql
ADD mysql.tar.gz /usr/local/
RUN useradd mysql -s /sbin/nologin -M \
&& chown -R mysql:mysql /usr/local/mysql \
&& echo 'export PATH=$PATH:/usr/local/mysql/bin/' >> /etc/profile \
&& source /etc/profile \
&& echo "/usr/local/mysql/lib" >>/etc/ld.so.conf \
&& ldconfig
# bind
ADD bind-9.11.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/bind-9.11.2/ \
&& ./configure --prefix=/usr/local/bind \
--with-dlz-mysql=/usr/local/mysql \
--enable-largefile \
--disable-openssl-version-check \
--enable-threads \
--disable-ipv6 \
--disable-linux-caps \
&& make && make install
COPY named.conf /usr/local/bind/etc
COPY entry.sh /usr/local/bin/
EXPOSE 53/udp 53/tcp 3306/tcp
ENTRYPOINT ["entry.sh"]
- entry.sh
#!/usr/bin/env bash
## mysql初始化 设置密码
echo "-----------------------------usage------------------------------------------------------"
echo "insert into dns_records (zone,host,type,data) values('test.com','@','NS','ns.test.com.');"
echo "insert into dns_records (zone,host,type,data) values('test.com','ns','A','1.1.1.1');"
echo "insert into dns_records (zone,host,type,data) values('test.com','www','A','127.0.0.1');"
echo "-----------------------------------------------------------------------------------------"
supervisord -c /etc/supervisor/supervisord.conf
echo 'sdasdfadsfda \
asdfadsfads'
- named.conf
logging {
channel query_log {
file "/var/log/named.log" versions 3 size 100m;
severity info;
print-time yes;
print-category yes;
print-severity yes;
};
category queries {
query_log;
};
category lame-servers {
null;
};
};
dlz "My zone" {
database "mysql
{host=127.0.0.1 dbname=dns ssl=false port=3306 user=root pass=123456}
{select zone from dns_records where zone = '$zone$'}
{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 dns_records where zone = '$zone$' and host = '$record$'}";
};
- supervisor.sh
[supervisord]
nodaemon = true
[program:mysqld]
command=/usr/local/mysql/bin/mysqld_safe --user=mysql
process_name=%(program_name)s
auto_start = true
autorestart = true
[program:named]
command=/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g
process_name=%(program_name)s
auto_start = true
autorestart = true
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes = 0
- my.conf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
default-storage-engine = innodb
innodb_file_per_table = 1
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
至此一切编译顺利,
期间遇到dockerfile COPY目录一个小问题, COPY mysql /user/local/ 拷贝不进去,拷贝文件没问题, 最终解决: COPY mysql /user/local/mysql即可.
更新: 从debian:jessie,实现bind和mysql分离
- Dockerfile
FROM debian:jessie
#http://mirrors.aliyun.com/help/debian
COPY sources.list /etc/apt/sources.list
RUN apt-get update \
&& apt-get install -y g++ libboost-all-dev libtool make pkg-config libmysqlclient-dev libssl-dev \
&& apt-get clean
# bind
ADD bind-9.11.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/bind-9.11.2/ \
&& ./configure --prefix=/usr/local/bind \
--with-dlz-mysql \
--enable-largefile \
--disable-openssl-version-check \
--enable-threads \
--disable-ipv6 \
--disable-linux-caps \
&& make && make install
COPY named.conf /usr/local/bind/etc
COPY entry.sh /usr/local/bin/
ENV TZ="PRC"
RUN ln -sf \
/usr/share/zoneinfo/Asia/Shanghai \
/etc/localtime
EXPOSE 53/udp 53/tcp
ENTRYPOINT ["entry.sh"]
- entry.sh
#!/usr/bin/env bash
echo
echo "--------------------------------请初始化你的bind的mysqlbackend库-----------------------"
echo "
create database dns;
use dns
CREATE TABLE dns_records(id int(11) unsigned NOT NULL auto_increment,zone varchar(255) NOT NULL,host varchar(255) NOT NULL default '@',type varchar(255) NOT NULL,data text,ttl int(11) default 600,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(255) default NULL,primary_ns varchar(255),PRIMARY KEY(id));
CREATE INDEX host_index ON dns_records(host);
CREATE INDEX type_index ON dns_records(type);
CREATE INDEX zone_index ON dns_records(zone);
insert into dns_records (zone,host,type,data,refresh,retry,expire,minimum,serial,resp_person,primary_ns) values('test.com','@','SOA','ns.test.com',7200,3600,86400,3600,1000,'root.test.com.','ns.test.com.');
insert into dns_records (zone,host,type,data) values('test.com','@','NS','ns.test.com.');
insert into dns_records (zone,host,type,data) values('test.com','ns','A','1.1.1.1');
insert into dns_records (zone,host,type,data) values('test.com','www','A','127.0.0.1');
"
echo "----------------------------------------------be happy---------------------------------------------------"
echo
if [ ! -n "$1" ] ;then
echo "dokcer run -p 53:53/udp -p 53:53/tcp bind-with-nomysql your_mysqlserver_ip"
exit 0;
fi
mysqlserver_ip=$1
sed -i "s#127.0.0.1#${mysqlserver_ip}#g" /usr/local/bind/etc/named.conf
/usr/local/bind/sbin/named -c /usr/local/bind/etc/named.conf -g
- named.conf
options{
forwarders { 192.168.6.6; };
};
logging {
channel query_log {
file "/var/log/named.log" versions 3 size 100m;
severity info;
print-time yes;
print-category yes;
print-severity yes;
};
category queries {
query_log;
};
category lame-servers {
null;
};
};
dlz "My zone" {
database "mysql
{host=127.0.0.1 dbname=dns ssl=false port=3306 user=root pass=123456}
{select zone from dns_records where zone = '$zone$'}
{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 dns_records where zone = '$zone$' and host = '$record$'}";
};
- sources.list
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib