Skip to content

Tech Docs

Dns Management

Example Ansible playbook for DNS configuration


  • name: Configure DNS hosts: dns_servers become: yes

tasks: - name: Install Bind9 apt: name: bind9 state: present

  • name: Configure Bind9 template: src: templates/named.conf.local.j2 dest: /etc/bind/named.conf.local notify: restart bind9

  • name: Configure Cloudflare DNS cloudflare_dns: api_key: "{{ cloudflare_api_key }}" api_secret: "{{ cloudflare_api_secret }}" zone: yourdomain.com record: www type: A content: 203.0.113.45 state: present delegate_to: localhost

Let me provide more detailed configurations and explanations for each component:

---

### **1. Internal DNS Configuration (Bind9)**
We'll use Bind9 as the internal DNS server for your private network.

```bash
// File: /etc/bind/named.conf.local
// Internal DNS configuration for 192.168.1.254

zone "yourdomain.com" IN {
    type master;
    file "/etc/bind/zones/db.yourdomain.com";
    allow-update {
        192.168.1.0/24;
    };
    notify yes;
    dnssec-validation yes;
    dnssec-lookaside auto;
};

zone "1.168.192.in-addr.arpa" IN {
    type master;
    file "/etc/bind/zones/192.168.1";
    allow-update {
        192.168.1.0/24;
    };
    notify yes;
    dnssec-validation yes;
    dnssec-lookaside auto;
};

// ... rest of configuration ...

Zone File Example:

// File: /etc/bind/zones/db.yourdomain.com
$TTL 1h
@ IN SOA ns1.yourdomain.com. admin.yourdomain.com. (
    2023100201 ; Serial
    3600 ; Refresh
    1800 ; Retry
    604800 ; Expire
    3600 ) ; Minimum

@ IN NS ns1.yourdomain.com
@ IN NS ns2.yourdomain.com

ns1 IN A 192.168.1.254
api IN A 192.168.1.100
www IN A 192.168.1.101

Reverse Zone File:

// File: /etc/bind/zones/192.168.1
$TTL 1h
@ IN SOA ns1.yourdomain.com. admin.yourdomain.com. (
    2023100201 ; Serial
    3600 ; Refresh
    1800 ; Retry
    604800 ; Expire
    3600 ) ; Minimum

@ IN NS ns1.yourdomain.com
@ IN NS ns2.yourdomain.com

254 IN PTR ns1.yourdomain.com
100 IN PTR api.yourdomain.com
101 IN PTR www.yourdomain.com

2. External DNS Configuration (Cloudflare)

For external DNS, we'll use Cloudflare's API to manage records.

Example Cloudflare DNS Configuration:

// Using Cloudflare API to configure external DNS records
{
  "api_key": "your_cloudflare_api_key",
  "api_secret": "your_cloudflare_api_secret",
  "zone": "yourdomain.com",
  "records": [
    {
      "type": "A",
      "name": "www",
      "content": "203.0.113.45",
      "proxied": true
    },
    {
      "type": "A",
      "name": "api",
      "content": "203.0.113.46",
      "proxied": false
    },
    {
      "type": "MX",
      "name": "@",
      "content": "mail.yourdomain.com",
      "priority": 10
    }
  ]
}

Cloudflare Configuration Example (Using cloudflare-dyndns):

// File: /etc/cloudflare/cloudflare.conf
api-key = your_cloudflare_api_key
api-secret = your_cloudflare_api_secret
zone = yourdomain.com

3. Automation with Ansible

We'll use Ansible to automate DNS configuration and record management.

Ansible Playbook Example:

# File: ansible/playbooks/dns.yml
---
- name: Configure DNS
  hosts: dns_servers
  become: yes

  tasks:
  - name: Install Bind9
    apt:
      name: bind9
      state: present

  - name: Configure Bind9
    template:
      src: templates/named.conf.local.j2
      dest: /etc/bind/named.conf.local
    notify: restart bind9

  - name: Configure Cloudflare DNS
    cloudflare_dns:
      api_key: "{{ cloudflare_api_key }}"
      api_secret: "{{ cloudflare_api_secret }}"
      zone: "{{ zone }}"
      record: "www"
      type: A
      content: "203.0.113.45"
      state: present
    delegate_to: localhost

4. DNS Security Measures

Here are the key security measures we’re implementing:

a. DNSSEC Configuration

Enable DNSSEC for both internal and external zones:

// File: /etc/bind/named.conf.options
dnssec-validation yes;
dnssec-lookaside auto;

// Enable DNSSEC for zones
zone "yourdomain.com" IN {
    dnssec-update-mode maintain;
    dnssec-validation yes;
};

b. TSIG Configuration

Use TSIG (Transaction Signature) for secure zone transfers:

// File: /etc/bind/named.conf.tsig
key "tsig-key" {
    algorithm hmac-md5;
    secret "your_secret_key_here";
};

zone "yourdomain.com" IN {
    type master;
    allow-transfer {
        192.168.1.254;
    };
    allow-update {
        key "tsig-key";
    };
};

c. Split-Horizon DNS

Configure split-horizon DNS to prevent internal records from being exposed externally:

// Internal DNS view
view "internal" {
    match-clients { 192.168.1.0/24; };
    zone "yourdomain.com" IN {
        type master;
        file "/etc/bind/zones/db.yourdomain.com";
    };
};

// External DNS view
view "external" {
    match-clients { any; };
    zone "yourdomain.com" IN {
        type master;
        file "/etc/bind/zones/db.external.yourdomain.com";
    };
};

5. Monitoring and Logging

Use Prometheus and Grafana to monitor DNS server performance:

Prometheus Configuration:

# File: /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: 'dns-server'
    static_configs:
      - targets: ['192.168.1.254:9153']

Grafana Dashboard:

  • Create a Grafana dashboard to monitor:
  • DNS query response time
  • Number of DNS queries per second
  • Cache hit rate
  • Memory usage
  • CPU usage

6. Backup and Disaster Recovery

Implement regular backups of DNS configurations and zones:

# File: /etc/cron.daily/backup-dns
tar -czf /backup/dns-config-$(date +%Y%m%d).tar.gz \
    /etc/bind/named.conf* \
    /etc/bind/zones/* \
    /etc/bind/tsig/*

Key Features of This Configuration:

  1. Internal DNS:
  2. Private IP management
  3. Internal service discovery
  4. Secure zone transfers using TSIG
  5. Split-horizon DNS

  6. External DNS:

  7. Public DNS records managed via Cloudflare
  8. DDoS protection
  9. Global CDN distribution
  10. Automated failover

  11. Security:

  12. DNSSEC for both internal and external zones
  13. TSIG for secure zone transfers
  14. Split-horizon DNS to prevent internal record exposure

  15. Automation:

  16. Ansible for consistent configuration
  17. CI/CD pipelines for DNS changes
  18. Cloudflare API integration