服务器 > 服务器 > dns服务器

ansible部署DNS缓存服务器的实现步骤

72人参与 2024-05-12 dns服务器

dns缓存服务器是一种不负责域名数据维护的dns服务器。简单来说,缓存服务器就是把用户经常使用到的域名与ip地址的解析记录保存在主机本地,从而提升下次解析的效率,这次使用unbound这款软件部署缓存服务器

一、 准备工作

1、 实验拓扑

节点ip地址说明
dns-server192.168.0.100缓存服务器
centos2192.168.0.101客户端

2、 unbound软件介绍

unbound是红帽公司(redhat)默认使用的的dns服务包,unbound是一个安全性高、功能强大、配置简单。

3、 使用参数说明

参数说明
interface监听ip
interface-automatic如果部署在非默认端口上,例如80/443,则需要禁用此选项
access-control允许网段
forward-zone允许域名,从哪里缓存

二、 ansible文件

1、 目录结构

[root@mmx_ansible dns_automating_unbound]# ls
ansible.cfg  inventory.yml  server.conf.j2  unbound.conf  unbound.yml

2、 配置文件

ansible.cfg

[defaults]
inventory=./inventory.yml
remote_user=root

3、主机清单文件

inventory.yml

home:
    caching_dns:
      ansible_host: 192.168.0.100
      ansible_ssh_password: "密码"
      ansible_user: "用户名"

4、 模板文件

server.conf.j2

{# 通过列举出所有的ipv4地址,来写 #}
server:
{% for ip in ansible_facts['all_ipv4_addresses'] %}
    interface: {{ ip }}
{% endfor %}
    interface-automatic: no
    access-control: {{ access_control }}
    domain-insecure: {{ domain_insecure }}
forward-zone:
    name: "{{ forward_zone_name }}"
    forward-addr: {{ forward_addr }}

5、 playbook

unbound.yml

---
- name: ubound is deployed on caching nameservers
  hosts: caching_dns
  become: yes
  vars:
    forward_addr: "114.114.114.114"
    access_control: "0.0.0.0/0 allow"
    domain_insecure: "*"
    forward_zone_name: "."
  tasks:
    - name: unbound is installed(安装软件包)
      yum:
        name: unbound
        state: present
    - name: unbound configuration is correct(配置unbound文件,当修改该文件时,重启服务)
      template:
        src: server.conf.j2
        dest: /etc/unbound/conf.d/server.conf
        owner: root
        group: unbound
        mode: '0644'
        setype: named_conf_t
      notify:
        - restart unbound
    - name: unbound is started and enabled(开启&&开机启动unbound服务)
      service:
        name: unbound
        state: started
        enabled: yes
    - name: unbound is started and enabled(确保防火墙开启)
      service:
        name: firewalld
        state: started
        enabled: yes
    - name: dns is enabled on the firewalld(放行unbound服务)
      ansible.posix.firewalld:
        service: dns
        state: enabled
        permanent: yes
        immediate: yes
  handlers:
      # 重启unbound服务
    - name: restart unbound
      service:
        name: unbound
        state: restarted

6、 执行playbook

ansible-playbook unbound.yml

[root@mmx_ansible dns_automating_unbound]# ansible-playbook unbound.yml 
play [ubound is deployed on caching nameservers] *************************************************************************************************************************************************************************************************
task [gathering facts] ***************************************************************************************************************************************************************************************************************************
ok: [caching_dns]
task [unbound is installed(安装软件包)] **********************************************************************************************************************************************************************************************************
ok: [caching_dns]
task [unbound configuration is correct(配置unbound文件,当修改该文件时,重启服务)] ***************************************************************************************************************************************************************
ok: [caching_dns]
task [unbound is started and enabled(开启&&开机启动unbound服务)] *********************************************************************************************************************************************************************************
ok: [caching_dns]
task [unbound is started and enabled(开启&&开机启动unbound服务)] *********************************************************************************************************************************************************************************
ok: [caching_dns]
task [dns is enabled on the firewalld(放行unbound服务)] ******************************************************************************************************************************************************************************************
ok: [caching_dns]
play recap ***************************************************************************************************************************************************************************************************************************************
caching_dns                : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

三、测试

1、 客户端

1) 临时修改客户端dns地址

[root@dns_client ~]# vim /etc/resolv.conf
nameserver 192.168.0.100

2) nslookup访问百度

[root@dns_client ~]# nslookup www.baidu.com
server:         192.168.0.100
address:        192.168.0.100#53
non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
name:   www.a.shifen.com
address: 182.61.200.7
name:   www.a.shifen.com
address: 182.61.200.6

2、 缓存服务器

1 ) 查看修改的配置文件

[root@dns-server_ubound ~]# cat /etc/unbound/conf.d/server.conf
server:
    interface: 192.168.0.100
    interface-automatic: no
    access-control: 0.0.0.0/0 allow
    domain-insecure: *
forward-zone:
    name: "."
    forward-addr: 114.114.114.114

2)查看缓存

unbound-control dump_cache

[root@dns-server_ubound ~]# unbound-control dump_cache
start_rrset_cache
;rrset 155 1 0 2 3
a.shifen.com.   155     in      soa     ns1.a.shifen.com. baidu_dns_master.baidu.com. 2301200016 5 5 2592000 3600
;rrset 125 2 0 5 3
www.a.shifen.com.       125     in      a       182.61.200.6
www.a.shifen.com.       125     in      a       182.61.200.7
;rrset 545 1 0 5 3
www.baidu.com.  545     in      cname   www.a.shifen.com.
end_rrset_cache
start_msg_cache
msg www.a.shifen.com. in aaaa 33152 1 155 3 0 1 0
a.shifen.com. in soa 4
msg www.baidu.com. in a 33152 1 125 3 2 0 0
www.baidu.com. in cname 0
www.a.shifen.com. in a 0
msg www.a.shifen.com. in a 33152 1 125 0 1 0 0
www.a.shifen.com. in a 0
end_msg_cache
eof

到此这篇关于ansible部署dns缓存服务器的实现步骤的文章就介绍到这了,更多相关ansible部署dns缓存服务器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

使用Unbound配置DNS缓存服务器的实现步骤

05-12

DNS信息查询工具dig用法详解

05-12

非常好用的DNS服务器(推荐)

05-12

DNS服务器的安装与配置步骤

05-12

DNS服务器未响应的原因及解决方法

05-12

DNS log注入原理解析

05-12

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论