centos7-安装内部dns

1. centos7-安装内部dns

  • 安装dns服务

    yum -y install dnsmasq
    
  • 创建一个内部添加dns解析文件

    touch  /etc/dns.hosts
    
  • 编写dnsmasq.conf主配置文件

    vim /etc/dnsmasq.conf
    
    # 定义dnsmasq从哪里获取上游DNS服务器的地址,默认是从/etc/resolv.conf获取
    resolv-file=/etc/resolv.dnsmasq.conf
    # 严格按照resolv-file文件中的顺序从上到下进行DNS解析,直到第一个解析成功为止
    strict-order
    # 启用泛域名解析,即自定义解析a记录
    # 访问baidu.com时的所有域名都会被解析成10.77.3.8
    address=/baidu.com/10.77.3.8
    address=/blog.csdn.net/10.77.3.8
    # 定义dnsmasq监听的地址,默认是监控本机的所有网卡上。局域网内主机若要使用dnsmasq服务时,指定本机的IP地址
    listen-address=10.77.3.8,127.0.0.1
    # 本地域名配置文件(不支持泛域名),添加内部需要解析的地址和域名(重新加载即可生效)
    addn-hosts=/etc/dns.hosts
    # 缓存条数
    cache-size=100
    # 为防止DNS污染,使用参数定义的DNS解析的服务器。注意:如果是阿里云服务器上配置dnsmasq要启用此项。
    bogus-nxdomain=114.114.114.114
    # 可以通过server对不通的网站使用不通的DNS服务器进行解析。如下表示对于google的服务,使用谷歌的DNS解析
    server=/google.com/8.8.8.8
    # 记录dns查询日志服务器
    log-queries
    # 设置日志记录器
    log-facility=/var/log/dnsmasq.log
    
  • 编写添加内部解析配置文件

    [root@dns ~]# cat /etc/dns.hosts 
    # 配置需要内部解析的域名
    192.168.0.190 harbor.scajy.cn        
    192.168.0.183 jenkins.scajy.cn
    
  • 重启dns服务

    [root@dns ~]# /bin/systemctl start dnsmasq
    [root@dns ~]# /bin/systemctl restart dnsmasq
    
  • 检查服务是否正常运行

    [root@localhost ~]# netstat -lntup
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      1809/dnsmasq        
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1166/sshd           
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1322/master         
    tcp6       0      0 :::53                   :::*                    LISTEN      1809/dnsmasq        
    tcp6       0      0 :::22                   :::*                    LISTEN      1166/sshd           
    tcp6       0      0 ::1:25                  :::*                    LISTEN      1322/master         
    udp        0      0 0.0.0.0:53              0.0.0.0:*                           1809/dnsmasq        
    udp        0      0 0.0.0.0:68              0.0.0.0:*                           964/dhclient        
    udp        0      0 10.0.0.19:123           0.0.0.0:*                           888/ntpd            
    udp        0      0 127.0.0.1:123           0.0.0.0:*                           888/ntpd            
    udp        0      0 0.0.0.0:123             0.0.0.0:*                           888/ntpd            
    udp6       0      0 :::53                   :::*                                1809/dnsmasq        
    udp6       0      0 ::1:123                 :::*                                888/ntpd            
    udp6       0      0 :::123                  :::*                                888/ntpd 
    
  • 客户端绑定dns的ip测试

    [root@k8s-master ~]# cat /etc/resolv.conf 
    # Generated by NetworkManager
    nameserver 192.168.0.161
    
    [root@k8s-master ~]# ping harbor.scajy.cn
    PING harbor.scajy.cn (192.168.0.190) 56(84) bytes of data.
    64 bytes from harbor.scajy.cn (192.168.0.190): icmp_seq=1 ttl=64 time=0.280 ms
    64 bytes from harbor.scajy.cn (192.168.0.190): icmp_seq=2 ttl=64 time=0.107 ms