系统架构师-DNS学习笔记

DNS学习笔记

1.DNS概述

  • DNS(Domain Name System)
  • 分布式数据库,域名空间
  • DNS服务运行在UDP协议之上,使用端口号53。

2.DNS解析过程

  1. 本地缓存
  2. DNS服务器缓存
  3. DNS服务器数据库
  4. 根域DNS服务器,顶级-》二级域 -》三级域
  5. 解析结果返回或返回错误信息

3.DNS的分类

  • 主DNS服务器
  • 从DNS服务器
  • 缓存服务器
  • 转发器

4.DNS的记录类型

  • SOA - 自己dns说明文本
  • NS - 域的授权名称服务器
  • MX - 域的邮件交换器,优先级值,越小越高
  • A - IPV4主机地址
  • AAAA - IPV6主机地址
  • PTR - 解析IP的指针,反向记录
  • CNAME - 权威名称,定义别名记录

5.DNS命名规范

  • 字母、数字、下划线、最多63字节长度
  • 如果命名不规范,在master-view文件上配置check-names ignore

6.DIG,NSLOOKUP,HOST

[root@linux-node1 ~]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.38
www.a.shifen.com has address 14.215.177.37
[root@linux-node1 ~]# nslookup www.baidu.com
Server:     192.168.88.2
Address:    192.168.88.2#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 14.215.177.38
Name:   www.a.shifen.com
Address: 14.215.177.37

[root@linux-node1 ~]# dig www.baidu.com

; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.3 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50183
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; MBZ: 0005 , udp: 4096
;; QUESTION SECTION:
;www.baidu.com.         IN  A

;; ANSWER SECTION:
www.baidu.com.      5   IN  CNAME   www.a.shifen.com.
www.a.shifen.com.   5   IN  A   14.215.177.38
www.a.shifen.com.   5   IN  A   14.215.177.37

;; Query time: 2481 msec
;; SERVER: 192.168.88.2#53(192.168.88.2)
;; WHEN: Thu Aug 17 11:42:28 CST 2017
;; MSG SIZE  rcvd: 101

7. 部署BIND9

Bind是一款开源DNS服务器软件,Berkeley Internet Name Domain

  1. 安装软件
yum install -y  bind-utils bind bind-devel bind-chroot
  1. vim /etc/named.conf
options {
    listen-on port 53 { any; };
    directory   "/var/named/chroot/etc/";
    allow-query { any; };
    dump-file   "/var/named/chroot/var/log/binddump.db";
    Statistics-file "/var/named/chroot/var/log/named_stats";
    zone-statistics yes;
    memstatistics-file  "log/mem_stats";
    empty-zones-enable  no;
    forwarders {202.106.196.115;8.8.8.8; };
};

key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};

controls {
    inet 127.0.0.1 port 953
        allow { 127.0.0.1; } keys {"rndc-key";};
};

logging {
    channel warning {
    file "/var/named/chroot/var/log/dns_warning" versions 10 size 10m;
    severity warning;
    print-category yes;
    print-severity yes;
    print-time yes;
    };

    channel general_dns {
        file "/var/named/chroot/var/log/dns_log";
        severity info;
        print-category yes;
        print-severity yes;
        print-time yes;
    };
    category default {
        warning;
    };
    category queries {
        general_dns;
    };
};

include "/var/named/chroot/etc/view.conf";
  1. vim /etc/rndc.key
key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};
  1. vim /etc/rndc.conf
key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};

options {
    default-key "rndc-key";
    default-server 127.0.0.1;
    default-port 953;
};
  1. vim /var/named/chroot/etc/view.conf
view "View" {
    zone "fbo.com" {
    type    master;
    file    "fbo.com.zone";
    allow-transfer  {
        192.168.57.200;
    };
    notify yes;
    also-notify {
        192.168.57.200;
    };
    };
};
  1. vim /var/named/chroot/etc/fbo.com.zone
$ORIGIN .
$TTL    3600    ;   1   hour
fbo.com IN SOA op.fbo.com.  dns.fbo.com.    (
        2000    ;   serial
        900 ;   refresh (15 minutes)
        600 ;   retry   (10 minutes)
        86400   ;   expire  (1 day)
        3600    ;   minimum (1  hour)
        )
        NS  op.fbo.com.
$ORIGIN fbo.com.
shanks  A   1.2.3.4
op  A 1.2.3.4
  1. 启动服务
systemctl enable named
systemctl start named
  1. 检查结果
[root@linux-node1 var]# dig @127.0.0.1 shanks.fbo.com

; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7_3.1 <<>> @127.0.0.1 shanks.fbo.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23459
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;shanks.fbo.com.            IN  A

;; ANSWER SECTION:
shanks.fbo.com.     3600    IN  A   1.2.3.4

;; AUTHORITY SECTION:
fbo.com.        3600    IN  NS  op.fbo.com.

;; ADDITIONAL SECTION:
op.fbo.com.     3600    IN  A   1.2.3.4

;; Query time: 2 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Aug 17 15:05:18 CST 2017
;; MSG SIZE  rcvd: 92

8.部署从DNS服务器

  1. 安装软件
yum install -y  bind-utils bind bind-devel bind-chroot
  1. vim /etc/named.conf
options {
    listen-on port 53 { any; };
    directory   "/var/named/chroot/etc/";
    allow-query { any; };
    dump-file   "/var/named/chroot/var/log/binddump.db";
    Statistics-file "/var/named/chroot/var/log/named_stats";
    zone-statistics yes;
    memstatistics-file  "log/mem_stats";
    empty-zones-enable  no;
    forwarders {202.106.196.115;8.8.8.8; };
};

key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};

controls {
    inet 127.0.0.1 port 953
        allow { 127.0.0.1; } keys {"rndc-key";};
};

logging {
    channel warning {
    file "/var/named/chroot/var/log/dns_warning" versions 10 size 10m;
    severity warning;
    print-category yes;
    print-severity yes;
    print-time yes;
    };

    channel general_dns {
        file "/var/named/chroot/var/log/dns_log";
        severity info;
        print-category yes;
        print-severity yes;
        print-time yes;
    };
    category default {
        warning;
    };
    category queries {
        general_dns;
    };
};

include "/var/named/chroot/etc/view.conf";
  1. vim /etc/rndc.key
key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};
  1. vim /etc/rndc.conf
key "rndc-key" {
    algorithm hmac-md5;
    secret "Eqw4hClGExUWeDkKBX/pBg==";
};

options {
    default-key "rndc-key";
    default-server 127.0.0.1;
    default-port 953;
};
  1. vim /var/named/chroot/etc/view
view "SalveView" {
    zone "fbo.com" {
        type    slave;
        masters {192.168.57.100;};
        file    "slave.fbo.com.zone";
    };
};
  1. 修改master上的view.conf配置,将slave节点ip加入,之后再fbo.com.zone将serial+1
  2. 在salve上修改目录权限,并启动
  3. 在master上执行rdnc reload

9.添加A、CNAME、MX、PTR记录

  1. 在zone文件末尾里添加A记录(实现负载均衡)
a A x.x.x.x

[root@linux-node1 ~]# host a.fbo.com localhost
Using domain server:
Name: localhost
Address: 127.0.0.1#53
Aliases: 
a.fbo.com has address 192.168.122.100
  1. 在zone文件末尾里添加CNAME
cname   CNAME   a.fbo.com.

[root@linux-node1 ~]# rndc reload
WARNING: key file (/etc/rndc.key) exists, but using default configuration file (/etc/rndc.conf)
server reload successful
[root@linux-node1 ~]# host cname.fbo.com localhost
Using domain server:
Name: localhost
Address: 127.0.0.1#53
Aliases: 

cname.fbo.com is an alias for a.fbo.com.
a.fbo.com has address 192.168.122.100
  1. 在zone文件末尾添加mx记录,mx值越小优先级越高
mx  mx 5    x.x.x.x
mx  mx 10   x.x.x.x
  1. 添加PTR记录
# /var/named/chroot/etc/view.conf
zone    "168.192.in-addr.arpa"  {
    type master;
    file    "168.192.zone";
    allow-transfer  {
        10.6.0.254;
    };
    notify  yes;
    also-notify {
        10.6.0.254;
        };
};
# /var/named/chroot/etc/168.192.zone
$TTL    3600    ;   1   hour
                    IN  SOA op.fbo.com. dns.fbo.com.    (
                        2004    ;   serial
                        900     ;   refresh (15 minutes)
                        600     ;   retry   (10 minutes)
                        86400   ;   expire  (1  day)
                        3600    ;   minimum (1 hour)
                        )
                        NS  op.fbo.com.
102.122 IN  PTR a.fbo.com.
  1. 配置DNS视图(智能DNS)- 分区访问
# master节点/var/name/chroot/etc/named.conf,在include上面添加
acl group1  {
    192.168.57.100;
};
acl group2 {
    192.168.57.200;
};
# 修改/var/named/chroot/etc/view.conf为
view "GROUP1"   {
    match-clients { group1; };
    zone    "viewfbo.com"   {
        type master;
        file "group1.viewfbo.com.zone";
    };
};
view "GROUP2"   {
    match-clients { group2; };
    zone    "viewfbo.com"   {
        type master;
        file "group2.viewfbo.com.zone";
    };
};
# master节点/var/named/chroot/etc/group1.viewfbo.com.zone
$ORIGIN .
$TTL    3600    ;   1   hour
viewfbo.com IN  SOA op.viewfbo.com. dns.viewfbo.com.    (
    2004    ;   serial
    900     ;   refresh (15 minutes)
    600     ;   retry   (10 minutes)
    86400   ;   expire  (1  day)
    3600    ;   minimum (1 hour)
    )
            NS  op.viewfbo.com.
$ORIGIN viewfbo.com.
op  A   192.168.122.1
view    A   192.168.122.1
# master节点/var/named/chroot/etc/group2.viewfbo.com.zone
$ORIGIN .
$TTL    3600    ;   1   hour
viewfbo.com IN  SOA op.viewfbo.com. dns.viewfbo.com.    (
    2004    ;   serial
    900     ;   refresh (15 minutes)
    600     ;   retry   (10 minutes)
    86400   ;   expire  (1  day)
    3600    ;   minimum (1 hour)
    )
            NS  op.viewfbo.com.
$ORIGIN viewfbo.com.
op  A   192.168.122.2
view    A   192.168.122.2
# 修改文件权限
chown named.named /var/named/chroot/etc/group*.zone
rndc reload
dig @192.168.57.100 view.viewfbo.com

高可用、高性能

压测:queryperf
queryperf -d test.txt -s 8.8.8.8
配置管理自动化:bind-dlz
https://github.com/shanks1127/dns

其他软件

  • DNSMASQ
  • HTTPDNS
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容