---
title: DNS 基础设施
url: https://doc.liz6.com/networking/04-DNS/04-dns-infrastructure
locale: zh
area: networking
tags:
- networking
- DNS
date: 2026-06-30
modified: 2026-07-11
description: 根服务器不是 13 台机器,是 13 个 anycast 网络。从根到 TLD 到权威,每一层都是分布式的——理解这层基础设施的架构,才知道"改一条 DNS 记录"的背后发生了什么。
---

# DNS 基础设施

> 根服务器不是 13 台机器,是 13 个 anycast 网络。从根到 TLD 到权威,每一层都是分布式的——理解这层基础设施的架构,才知道"改一条 DNS 记录"的背后发生了什么。

## 概述

DNS 不只是协议——它是一个全球分布式数据库，由 13 个 root server（1000+ 物理实例通过 Anycast 部署）和数百个 TLD registry 构成。CDN 提供商（Cloudflare/AWS/Azure）利用 DNS 作为流量入口：根据查询来源 IP 返回最近的数据中心地址。Split DNS 在企业/家庭网络中分隔内外视图。理解 DNS 基础设施是理解互联网可用性和故障恢复的关键。

## Root Server System

13 个逻辑 root server 标识 (a.root-servers.net ~ m.root-servers.net)，1000+ 物理实例通过 Anycast 全球分布。

Root zone 大小: ~10KB (只有 TLD delegation records)。Root server 不应答 recursive queries (RD=0 only)，只做 referral。

Root hints file (`/usr/share/dns/root.hints` 或内置在 recursive resolver 中) 提供初始 bootstrap。

## Anycast

同一 IP prefix 同时 announce 在多个物理位置。BGP 自动将流量路由到最近的 (by AS path) 节点。

<svg viewBox="0 0 720 340" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,'Source Han Sans CN','Microsoft YaHei',sans-serif" role="img" aria-label="Anycast 原理:BGP 就近路由到最短路径实例">
  <defs>
    <marker id="mk-anycast" markerWidth="10" markerHeight="8" refX="8" refY="3" orient="auto"><path d="M0,0 L8,3 L0,6 Z" fill="#475569"/></marker>
  </defs>
  <rect width="720" height="340" fill="#ffffff"/>
  <text x="360" y="28" text-anchor="middle" font-size="17" font-weight="700" fill="#1f2933">Anycast:同一 IP 多地 announce,BGP 就近路由到最短路径实例</text>

  <rect x="40" y="55" width="300" height="70" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="190" y="80" text-anchor="middle" font-size="13" font-weight="700" fill="#3730a3">Anycast 实例 A · Tokyo</text>
  <text x="190" y="100" text-anchor="middle" font-size="11" fill="#4f46e5">AS Path: AS1 → dns-root</text>

  <rect x="380" y="55" width="300" height="70" rx="8" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="530" y="80" text-anchor="middle" font-size="13" font-weight="700" fill="#3730a3">Anycast 实例 B · Frankfurt</text>
  <text x="530" y="100" text-anchor="middle" font-size="11" fill="#4f46e5">AS Path: AS2 → AS3 → dns-root</text>

  <line x1="190" y1="195" x2="190" y2="130" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-anycast)"/>
  <text x="200" y="165" font-size="11" fill="#475569">BGP 见更短路径</text>

  <line x1="530" y1="195" x2="530" y2="130" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-anycast)"/>
  <text x="540" y="165" font-size="11" fill="#475569">BGP 见更短路径</text>

  <rect x="40" y="200" width="300" height="56" rx="8" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="190" y="223" text-anchor="middle" font-size="12" font-weight="700" fill="#115e59">Client in Asia</text>
  <text x="190" y="242" text-anchor="middle" font-size="11" fill="#0f766e">→ 查询 Tokyo instance</text>

  <rect x="380" y="200" width="300" height="56" rx="8" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="530" y="223" text-anchor="middle" font-size="12" font-weight="700" fill="#115e59">Client in Europe</text>
  <text x="530" y="242" text-anchor="middle" font-size="11" fill="#0f766e">→ 查询 Frankfurt instance</text>

  <rect x="60" y="278" width="600" height="50" rx="8" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="76" y="299" font-size="12.5" fill="#115e59">不保证同一 client 的两次查询落进同一实例(除非同一 /24 被一致 announce)——</text>
  <text x="76" y="317" font-size="12.5" fill="#115e59">但这对 DNS 是完美的:每个 query 独立处理。</text>
</svg>

Anycast 工作原理: 不保证同 client 的两次查询去同一个实例 (除非同一 /24 announced consistently)。这对 DNS 是完美的 — 每个 query 独立。

## CDN DNS

<svg viewBox="0 0 720 290" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,'Source Han Sans CN','Microsoft YaHei',sans-serif" role="img" aria-label="CDN DNS:查询解析到最近 edge 节点的路径">
  <defs>
    <marker id="mk-cdn" markerWidth="10" markerHeight="8" refX="8" refY="3" orient="auto"><path d="M0,0 L8,3 L0,6 Z" fill="#475569"/></marker>
  </defs>
  <rect width="720" height="290" fill="#ffffff"/>
  <text x="360" y="28" text-anchor="middle" font-size="17" font-weight="700" fill="#1f2933">CDN DNS = 流量入口:查询解析到最近 edge 节点的路径</text>

  <rect x="14" y="52" width="128" height="54" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="78" y="74" text-anchor="middle" font-size="11" font-weight="700" fill="#3730a3">Client 查询</text>
  <text x="78" y="92" text-anchor="middle" font-size="10" fill="#4f46e5">www.example.com</text>

  <rect x="152" y="52" width="128" height="54" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="216" y="74" text-anchor="middle" font-size="11" font-weight="700" fill="#3730a3">Authoritative DNS</text>
  <text x="216" y="92" text-anchor="middle" font-size="10" fill="#4f46e5">由 CDN 托管</text>

  <rect x="290" y="52" width="150" height="54" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="365" y="74" text-anchor="middle" font-size="11" font-weight="700" fill="#3730a3">看 query source IP</text>
  <text x="365" y="92" text-anchor="middle" font-size="10" fill="#4f46e5">选最近的 edge PoP</text>

  <rect x="450" y="52" width="108" height="54" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="504" y="83" text-anchor="middle" font-size="11" font-weight="700" fill="#3730a3">返回 edge IP</text>

  <rect x="568" y="52" width="138" height="54" rx="6" fill="#eef2ff" stroke="#c7d2fe"/>
  <text x="637" y="74" text-anchor="middle" font-size="11" font-weight="700" fill="#3730a3">Client → edge</text>
  <text x="637" y="92" text-anchor="middle" font-size="10" fill="#4f46e5">→ CDN edge → backend</text>

  <line x1="142" y1="79" x2="152" y2="79" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-cdn)"/>
  <line x1="280" y1="79" x2="290" y2="79" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-cdn)"/>
  <line x1="440" y1="79" x2="450" y2="79" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-cdn)"/>
  <line x1="558" y1="79" x2="568" y2="79" stroke="#475569" stroke-width="1.6" marker-end="url(#mk-cdn)"/>

  <text x="20" y="132" font-size="13" font-weight="700" fill="#1f2933">关键技术</text>

  <rect x="20" y="145" width="215" height="72" rx="6" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="34" y="168" font-size="12" font-weight="700" fill="#115e59">GeoDNS</text>
  <text x="34" y="186" font-size="10.5" fill="#0f766e">source IP → geo database</text>
  <text x="34" y="203" font-size="10.5" fill="#0f766e">→ nearest region → return IP</text>

  <rect x="257" y="145" width="215" height="72" rx="6" fill="#f0fdfa" stroke="#99f6e4"/>
  <text x="271" y="168" font-size="12" font-weight="700" fill="#115e59">Latency-based</text>
  <text x="271" y="186" font-size="10.5" fill="#0f766e">实时测 edge↔source IP 延迟</text>
  <text x="271" y="203" font-size="10.5" fill="#0f766e">→ 选最低延迟</text>

  <rect x="494" y="145" width="215" height="72" rx="6" fill="#ffedd5" stroke="#f97316"/>
  <text x="508" y="168" font-size="12" font-weight="700" fill="#9a3412">ECS(Client Subnet)</text>
  <text x="508" y="186" font-size="10.5" fill="#c2410c">resolver 传子网 /24(v4)/56(v6)</text>
  <text x="508" y="203" font-size="10.5" fill="#c2410c">→ 更精确选 edge,但泄露信息</text>

  <rect x="20" y="230" width="680" height="50" rx="8" fill="#ffedd5" stroke="#f97316"/>
  <text x="36" y="251" font-size="12.5" fill="#9a3412">ECS 让 CDN 更精确地选 edge,但会把 client 的子网信息传给 authoritative</text>
  <text x="36" y="269" font-size="12.5" fill="#9a3412">——精度换隐私,是个 tradeoff。</text>
</svg>

## Split DNS

内部网络使用不同的 DNS 视图:

```
内部 (家庭/公司网络):
  grafana.liz6.com → 192.168.31.8 (内网 IP, 不经公网)

公网:
  grafana.liz6.com → VPS IP (经过 rathole 隧道)

实现:
  - BIND: views { match-clients { internal_net; }; ... }
  - Unbound: access-control-view
  - dnsmasq: address=/grafana.liz6.com/192.168.31.8
```

## DNS-based Load Balancing

```
Weighted round-robin:
  www.example.com A 1.1.1.1
  www.example.com A 2.2.2.2
  → resolver 随机选 (通常 client 选第一个, 但不同 resolver 行为不同)

Latency-based (Route 53):
  latency alias + health checks → 自动选择最低延迟的 healthy endpoint

Geo-proximity (Traffic Manager):
  geographic routing + endpoint monitoring
```

## 参考

- **RSSAC**: root-servers.org
- **CDN**: Cloudflare Anycast, AWS Route 53, Azure Traffic Manager

*Keywords: root servers, anycast, CDN DNS, GeoDNS, split DNS, ECS, DNS-based load balancing*
