什么是DNS?
DNS(域名系统)是互联网上的一种技术,它使得我们可以通过方便记忆的域名(如 www.example.com)来访问网站,而不需要记住复杂的IP地址(如 192.168.1.1)。简而言之,DNS 作为互联网的电话簿,将人类可读的域名转换为机器可读的IP地址,这个过程称为DNS解析。这样,当你在浏览器中输入一个网址时,DNS服务器就会帮助你的电脑找到相应的服务器的IP地址,从而能够加载并显示网站内容。
二、DNS服务查询方式
DNS服务器的查询方式主要有两种:递归查询和迭代查询。这两种查询方式是解析域名时的不同步骤和方法:
1、递归查询:
2、迭代查询:
(1)在递归查询中,当递归DNS服务器需要向其他服务器请求解析时,它进行的是迭代查询。
(2)在迭代查询中,DNS服务器不会为客户端完成所有查询任务。而是返回一个引导客户端继续查询的“下一步服务器”的地址。
(3)客户端或请求的DNS服务器收到这个地址后,会向这个新的DNS服务器发起查询,如此重复,直到找到最终的IP地址。
三、安装配置
1、环境准备
DNS主:192.168.18.22
DNS从:192.168.18.23
操作系统:Centos7 (3.10.0-1127.el7.x86_64)
软件版本:
[root@k8svip named]# rpm -qa |grep bind
bind-license-9.11.4-26.P2.el7_9.15.noarch
bind-utils-9.11.4-26.P2.el7_9.15.x86_64
bind-libs-lite-9.11.4-26.P2.el7_9.15.x86_64
bind-export-libs-9.11.4-16.P2.el7.x86_64
bind-9.11.4-26.P2.el7_9.15.x86_64
bind-libs-9.11.4-26.P2.el7_9.15.x86_64
[root@k8svip named]#
2、DNS主从安装
[ ]
3、DNS主从服务器默认配置文件及目录
主要执行程序:/usr/sbin/named
服务脚本:/etc/init.d/named
默认监听端口:53
主配置文件:/etc/named.conf
4、DNS主上面配置 named.conf 文件解析
[root@k8svip data]# cat /etc/named.conf
options {
# DNS标准端口
listen-on port 53 { any; };
# 禁用ipv6
listen-on-v6 { none; };
#listen-on-v6 port 53 { ::1; };
# bind 工作目录
directory "/var/named";
# 缓存文件的位置
dump-file "/var/named/data/cache_dump.db";
# 记录服务器的运行的统计信息
statistics-file "/var/named/data/named_stats.txt";
# 指定内存统计数据文件的位置
memstatistics-file "/var/named/data/named_mem_stats.txt";
# 允许任何来源的查询请求
allow-query { any; };
# 对主区域文件中的DNS名称进行检查,如果有问题则发出警告
check-names master warn;
# 允许递归查询
recursion yes;
# 设置为首先尝试转发
forward first;
# 指向上游DNS服务器,用于转发解析请求。
forwarders {
114.114.114.114;
};
# 允许向从服务器发送通知,告知区域数据的变更;
notify yes;
# 禁用 DNSSEC 支持
dnssec-enable no;
# 禁用 DNSSEC 校验
dnssec-validation no;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging {
# /var/named/data/named.run
channel default_debug {
file "data/named.run";
severity dynamic;
};
# /var/named/data/query.log
channel query_log {
file "data/query.log" versions 3 size 128m;
severity info;
print-time yes;
print-severity yes;
print-category yes;
};
category queries {
query_log;
};
# /var/named/data/named.log
channel default_log {
file "data/named.log" versions 3 size 128m;
severity info;
print-time yes;
print-severity yes;
print-category yes;
};
category default {
default_log;
};
};
# 区域配置文件
zone "k8s.vip" IN {
# 主服务器
type master;
# 区域数据文件
file "k8s.vip";
# 允许向从服务器发送通知,告知区域数据的变更
notify yes;
# 指定在区域更新时除了默认的从服务器之外,还需要通知的额外服务器
also-notify { 192.168.18.23; };
# 允许指定的从服务器地址列表;
allow-transfer { 192.168.18.23; };
};
# 反向解析配置文件
zone "18.168.192.in-addr.arpa" IN {
type master;
file "18.168.192.zone";
notify yes;
also-notify { 192.168.18.23; };
allow-transfer { 192.168.18.23; };
};
# 反向解析配置文件,多个网络可以写成多个。
zone "22.168.192.in-addr.arpa" IN {
type master;
file "22.168.192.zone";
notify yes;
also-notify { 192.168.18.23; };
allow-transfer { 192.168.18.23; };
};
# 转发区域设置
zone "cluster.local" {
# 指定这是一个转发区域
type forward;
# 只转发查询,不做任何递归查询
forward only;
# 转发查询到指定的DNS服务器。
forwarders {
100.72.128.2;
};
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
[root@k8svip data]#
注意:区域配置文件中以下两项为【从服务器同步主配置】所需要的
also-notify { 192.168.18.23; };
5、DNS主上面配置区域文件
[root@k8svip named]# pwd
/var/named
[root@k8svip named]# cat k8s.vip
$TTL 180
@ IN SOA @ k8s.vip. (
20204221 3600 14400 360000 86400 )
IN NS @
IN A 192.168.18.22
file IN A 192.168.18.23
www IN A 192.168.18.24
[root@k8svip named]#
6、DNS主上面反向解析区域文件
[root@k8svip named]# cat 18.168.192.zone
$TTL 180
@ IN SOA @ k8s.vip. (
20201211 3600 14400 360000 86400 )
IN NS @
IN A 192.168.18.22
23 IN PTR file.k8s.vip.
24 IN PTR www.k8s.vip.
[root@k8svip named]# cat 22.168.192.zone
$TTL 180
@ IN SOA @ k8s.vip. (
20202211 3600 14400 360000 86400 )
IN NS @
IN A 192.168.18.22
24 IN PTR ua.k8s.vip.
25 IN PTR vpn.k8s.vip.
26 IN PTR tt2.k8s.vip.
[root@k8svip named]#
7、DNS主服务器权限修改
[root@k8svip named]# chown root:named 18.168.192.zone 22.168.192.zone k8s.vip
[root@k8svip named]# systemctl restart named.service
注意如果这个权限没有设置,反向解析会失败,我测试的正向解析,如果没有设置权限的话,也是成功的。
[root@k8svip named]# host 192.168.18.24
Host 24.18.168.192.in-addr.arpa not found: 2(SERVFAIL)
[root@k8svip named]#
8、 DNS 从服务器配置文件
[root@k8svip ~]# cat /etc/named.conf
options {
listen-on port 53 { any; };
#listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
masterfile-format text;
allow-query { any; };
check-names master warn;
recursion yes;
forward first;
forwarders {
114.114.114.114;
};
dnssec-enable no;
dnssec-validation no;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging {
# /var/named/data/named.run
channel default_debug {
file "data/named.run";
severity dynamic;
};
# /var/named/data/query.log
channel query_log {
file "data/query.log" versions 3 size 128m;
severity info;
print-time yes;
print-severity yes;
print-category yes;
};
category queries {
query_log;
};
# /var/named/data/named.log
channel default_log {
file "data/named.log" versions 3 size 128m;
severity info;
print-time yes;
print-severity yes;
print-category yes;
};
category default {
default_log;
};
};
# 区域配置文件
zone "k8s.vip" IN {
# slave 从
type slave;
# 指定master
masters { 192.168.18.22; };
# 区域配置文件路径
file "slaves/k8s.vip";
#allow-update { none; };
};
zone "18.168.192.in-addr.arpa" IN {
type slave;
masters { 192.168.18.22; };
file "slaves/18.168.192.zone";
# allow-update { none; };
};
zone "22.168.192.in-addr.arpa" IN {
type slave;
masters { 192.168.18.22; };
file "slaves/22.168.192.zone";
};
zone "cluster.local" {
type forward;
forward only;
forwarders {
100.72.128.2;
};
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
[root@k8svip ~]#
注意从服务器上面配置masterfile-format text; 否则从服务器上面同步过来的文件会是乱码。
9、DNS从服务器启动
[ ]
10、DNS从服务器查看配置同步
[root@k8svip slaves]# ll
总用量 12
-rw-r--r-- 1 named named 397 5月 14 15:03 18.168.192.zone
-rw-r--r-- 1 named named 417 5月 14 15:03 22.168.192.zone
-rw-r--r-- 1 named named 421 5月 14 15:03 k8s.vip
[root@k8svip slaves]#
四、测试
1、修改DNS配置
[ ]
options timeout:3 attempts:1 single-request single-request-reopen
nameserver 192.168.18.22
nameserver 192.168.18.23
[ ]
2、基本测试
[root@k8svip ~]# nslookup www.k8s.vip
Server: 192.168.18.22
Address: 192.168.18.22#53
Name: www.k8s.vip
Address: 192.168.18.24
[root@k8svip ~]# host 192.168.18.24
24.18.168.192.in-addr.arpa domain name pointer www.k8s.vip.
[root@k8svip ~]#
3、主修改区域配置文件,从是否同步
主服务器重启
[ ]
从服务器查看
[root@k8svip slaves]# ll
总用量 12
-rw-r--r-- 1 named named 397 5月 14 15:03 18.168.192.zone
-rw-r--r-- 1 named named 417 5月 14 15:03 22.168.192.zone
-rw-r--r-- 1 named named 399 5月 14 15:49 k8s.vip #时间已更改
[root@k8svip slaves]#
4、主服务器挂掉
人为停掉主systemctl stop named.service
[root@k8svip ~]# time nslookup www.k8s.vip
Server: 192.168.18.23
Address: 192.168.18.23#53
Name: www.k8s.vip
Address: 192.168.18.24
real 0m2.015s
user 0m0.003s
sys 0m0.011s
[root@k8svip ~]#
主恢复后,再次解析正常,遇到这种情况,可以修改/etc/resolv.conf,改变nameserver的顺序。
5、日志中有大量IPv6相关的东西,我们可以只用IPv4传输数据
echo 'OPTIONS="-4"' /etc/sysconfig/named
6、dig命令测试
7、配置文件check
[root@k8svip named]# named-checkconf # 检查配置文件语法
[root@k8svip named]# named-checkzone k8s.vip /var/named/k8s.vip
zone k8s.vip/IN: loaded serial 20205221
OK
[root@k8svip named]#
8、host命令测试反向解析
[root@k8svip ~]# host 192.168.18.23
23.18.168.192.in-addr.arpa domain name pointer file.k8s.vip.
[root@k8svip ~]#
原创文章,作者:速盾高防cdn,如若转载,请注明出处:https://www.sudun.com/ask/58578.html