我是一名云平台IAAS层的运维开发工程师,在本系列文章中,我会介绍服务器装机的方方面面,欢迎大家关注.
本系列已经发布的文章,欢迎大家阅读
服务器安装(1)服务器安装系统
服务器安装(2)PXE交换机的dhcp中继和dhcp服务器的配置
服务器安装(3)带外网络和服务器配置
对于数据中心,通常需要自己的域名服务器来管理环境中的域名解析和注册。毕竟环境中有很多机器,其他一些服务也有自己的IP对外暴露。如果所有的服务都用IP来相互指代,有两个麻烦的问题。3360
一般来说,IP不容易记住。当我们看到一个IP,比如192.168.1.1,我们根本不知道这个IP下是什么服务。但是如果它是一个域名,比如zk1.example.com,那么我们很容易知道它是zk集群中的一台机器。IP换了就不好维护了。如果服务直接使用IP来相互引用,由于某种原因,当IP不得不更改时,这种交叉引用关系就变得难以维护。如果是域名,只需要更新域名对应的A记录即可。最多,一些服务可能需要重新启动。bind9命名服务器通常用于构建dns域名服务器。在centos7中,可以通过yum install -y bind直接安装。
安装完成后,按如下方式配置/etc/named.conf。最重要的一个是配置example.com的区域,其类型是master,这意味着可以更新域名记录。为了限制本地机器通过rndc更新域名记录,配置allow-update { 127 . 0 . 0 . 1;};
# cat/etc/named . conf options { directory '/var/named ';转储文件“/var/named/data/cache _ dump . db”;statistics-file '/var/named/data/named _ stats . txt ';mem statistics-file '/var/named/data/named _ mem _ stats . txt ';sec roots-file“/var/named/data/named . sec roots”;recursing-file“/var/named/data/named . recursing”;allow-query { localhost;0.0.0.0/0;};};日志记录{ channel default _ debug { file ' data/named . run };严重性动态;打印时间是;};};区域“.”在{ type提示中;文件“named . ca”;};包括“/etc/named . RFC 1912 . zones”;#定义一个可以使用{ type master中的nsupdate zone ' example.com '在本地更新的区域;允许-更新{ 127 . 0 . 0 . 1;};文件“example . com”;};在下面配置example.com的时区数据。这里除了配置必要的SOA信息和NS记录,最重要的是配置dnsapi.example.com的A记录为本地IP 172.18.0.1。这样,当其他服务想要更新域名时,它们可以调用dnsapi.example.com提供的API服务。
# cat/var/named/example . com $ ORIGIN。$ TTL 601 minuteexample.com在SOA ns1.example.com。admin.example.com。( 10002 ;系列600;刷新(10分钟)300;重试(5分钟)2419200;过期(4周)3600;最短(1小时))nsns1.example.com。$ origin example.com.dnsapi a 172 . 18 . 0 . 1 ns1a 172 . 18 . 0 . 1然后用systemctl start名启动dns服务器。成功后,使用dig @ localhostaxfr example.com来确保内容是正确的。
# dig @ localhost axfr example.com;DiG 9 . 11 . 9-red hat-9 . 11 . 9-1 . fc30 @ localhost axfr example.com;(找到2台服务器);全球选项: cmdexample.com。60在SOA ns1.example.com。admin.example.com。10002 600 300 2419200 3600example.com。60在南ns1.example.com.dnsapi.example.com。172.1中的60
8.0.1ns1.example.com. 60 IN A 172.18.0.1example.com. 60 IN SOA ns1.example.com. admin.example.com. 10002 600 300 2419200 3600;; Query time: 1 msec;; SERVER: ::1#53(::1);; WHEN: Wed Aug 28 21:44:33 CST 2019;; XFR size: 5 records (messages 1, bytes 203)如下是给test.example.com绑定A记录为1.2.3.6,TTL为60s
nsupdate -v << EOFserver 127.0.0.1update add test.example.com 60 A 1.2.3.6sendEOF可以看到这条域名记录有了.(也可以使用dig +short @localhost test.example.com查看 )
#dig @localhost axfr example.com; <<>> DiG 9.11.9-RedHat-9.11.9-1.fc30 <<>> @localhost axfr example.com; (2 servers found);; global options: +cmdexample.com. 60 IN SOA ns1.example.com. admin.example.com. 10003 600 300 2419200 3600example.com. 60 IN NS ns1.example.com.dnsapi.example.com. 60 IN A 172.18.0.1ns1.example.com. 60 IN A 172.18.0.1test.example.com. 60 IN A 1.2.3.6example.com. 60 IN SOA ns1.example.com. admin.example.com. 10003 600 300 2419200 3600;; Query time: 0 msec;; SERVER: ::1#53(::1);; WHEN: Wed Aug 28 21:46:46 CST 2019;; XFR size: 6 records (messages 1, bytes 224)
我使用flask实现了一个简单的可以更新和删除A记录的接口.支持update和remove两个API调用
如下是update的调用样例
#curl 'dnsapi.example.com/api/dns/update?name=test1.example.com&ip=2.3.4.5'ok完成之后域名信息如下,可以看到test1.example.com的域名A记录被成功添加了.
#dig @localhost axfr example.com; <<>> DiG 9.11.9-RedHat-9.11.9-1.fc30 <<>> @localhost axfr example.com; (2 servers found);; global options: +cmdexample.com. 60 IN SOA ns1.example.com. admin.example.com. 10004 600 300 2419200 3600example.com. 60 IN NS ns1.example.com.dnsapi.example.com. 60 IN A 172.18.0.1ns1.example.com. 60 IN A 172.18.0.1test.example.com. 60 IN A 1.2.3.6test1.example.com. 60 IN A 2.3.4.5example.com. 60 IN SOA ns1.example.com. admin.example.com. 10004 600 300 2419200 3600;; Query time: 0 msec;; SERVER: ::1#53(::1);; WHEN: Wed Aug 28 21:56:19 CST 2019;; XFR size: 7 records (messages 1, bytes 246)如下是remove的调用样例
#curl 'dnsapi.example.com/api/dns/remove?name=test1.example.com'ok完成之后域名信息如下,可以看到test1.example.com的域名A记录被成功删除了.
#dig @localhost axfr example.com; <<>> DiG 9.11.9-RedHat-9.11.9-1.fc30 <<>> @localhost axfr example.com; (2 servers found);; global options: +cmdexample.com. 60 IN SOA ns1.example.com. admin.example.com. 10005 600 300 2419200 3600example.com. 60 IN NS ns1.example.com.dnsapi.example.com. 60 IN A 172.18.0.1ns1.example.com. 60 IN A 172.18.0.1test.example.com. 60 IN A 1.2.3.6example.com. 60 IN SOA ns1.example.com. admin.example.com. 10005 600 300 2419200 3600;; Query time: 0 msec;; SERVER: ::1#53(::1);; WHEN: Wed Aug 28 21:58:21 CST 2019;; XFR size: 6 records (messages 1, bytes 224)其中实现的核心函数就是使用subprocess.run来调用nsupate命令.整体来说还是比较简单的.
基于这个实现,大家可以进行扩展,完成对其他类型,比如CNAME,AAAA类型域名的更新操作.