Back to articles
AI & Cloud

Configure Anycast DNS with Knot DNS and GeoIP on 3 VPS to Optimize Domain Resolution Time in Vietnam

A detailed guide to setting up an Anycast DNS system using Knot DNS and GeoIP on 3 VPS servers to reduce domain resolution time in Vietnam, improving website and API access performance.

DPTCloud Engineering4 min read
Configure Anycast DNS with Knot DNS and GeoIP on 3 VPS to Optimize Domain Resolution Time in Vietnam

Introduction to Anycast DNS and Benefits in Vietnam

In Vietnam, where internet usage continues to grow rapidly, optimizing DNS resolution time is essential for enhancing user experience. Anycast DNS is an effective solution that reduces latency by routing DNS queries to the nearest network server. By deploying this system across 3 VPS servers distributed in different regions of Vietnam, the service can deliver faster and more stable DNS responses.

Knot DNS is a high-performance, open-source DNS server designed for environments requiring speed and reliability. When combined with GeoIP, the system can route DNS queries to the VPS closest to the user based on their IP address, minimizing response time.

Requirements and Preparations Before Deployment

Hardware and Software Requirements

  • 3 VPS servers (each with at least 2GB RAM, 1 vCPU, and good network connectivity)
  • 3 public IP addresses (one per VPS)
  • Operating system: Ubuntu 22.04 LTS or Debian 11
  • Software: Knot DNS, GeoIP database, DNS client (bind9 or unbound)

Network Preparation

Ensure that the VPS servers can communicate with each other over the internal or public network. Open the following ports: 53 (UDP/TCP), 1053 (UDP/TCP if using xfr), and the management port (if applicable).

Deploying Knot DNS on Each VPS

Install Knot DNS

On each VPS, run the following commands:

sudo apt update
sudo apt install knotd

Configure Knot DNS

Create the configuration file `/etc/knot/knot.conf` with the following content:

server:
    ip4: 127.0.0.1@53
    ip4: <IP_VPS_1>@53
    ip4: <IP_VPS_2>@53
    ip4: <IP_VPS_3>@53
    ip4: <IP_VPS_1>@1053
    ip4: <IP_VPS_2>@1053
    ip4: <IP_VPS_3>@1053

zone:
    name: "example.com"
    file: "/var/lib/knot/example.com.db"
    type: master

acl:
    name: "any"
    ip: 0.0.0.0/0

control:
    ip: 127.0.0.1@1053
    acl: "any"

Replace `<IP_VPS_1>`, `<IP_VPS_2>`, and `<IP_VPS_3>` with the actual IP addresses of each VPS.

Start the Service

sudo systemctl enable knotd
sudo systemctl start knotd

Integrating GeoIP for Query Routing

Download GeoIP Database

Run the following command to install the GeoIP database:

sudo apt install geoip-database

Run Query Routing Script

Create the script `/usr/local/bin/geoip-dns-routing.sh`:

#!/bin/bash
IP=$1
COUNTRY=$(geoiplookup $IP | cut -d' ' -f5)

case "$COUNTRY" in
    "VN")
        echo "127.0.0.1"  # Route query to the VPS in Vietnam
        ;;
    *)
        echo "127.0.0.1"  # Route query to the nearest VPS (possibly in another region)
        ;;
esac

Make the script executable:

sudo chmod +x /usr/local/bin/geoip-dns-routing.sh

Testing and Performance Monitoring

Run Test Commands

Test from multiple locations in Vietnam:

dig @127.0.0.1 example.com

Compare response times across different locations.

Monitor Performance

To track performance, use NetData. For detailed instructions, refer to: Install and Configure NetData on VPS for 1-Second System Performance Monitoring with 0.1s Latency.

Troubleshooting

Unable to Access DNS

Check if the Knot DNS service is running:

sudo systemctl status knotd

If not running, check the logs at `/var/log/knot/knot.log`.

GeoIP Not Working

Ensure the GeoIP database is installed correctly. Test with:

geoiplookup 8.8.8.8

If no result is returned, reinstall the GeoIP database.

References

Related infrastructure & engineering guides: High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478