当前位置: 首页 > news >正文

EC2 使用 dnsmasq 本地缓存 + EKS 使用 NodeLocalDNS

# DNS 缓存配置指南> EC2 使用 dnsmasq 本地缓存 + EKS 使用 NodeLocalDNS---## 目录- [一、EC2 使用本地 DNS 缓存](#一ec2-使用本地-dns-缓存)
- [二、EKS 使用 NodeLocalDNS](#二eks-使用-nodelocaldns)
- [三、最佳实践](#三最佳实践)---## 一、EC2 使用本地 DNS 缓存### 1.1 Ubuntu 系统配置#### 安装 dnsmasq```bash
# 安装 dnsmasq
apt install dnsmasq -y# 创建用户组
groupadd -r dnsmasq
usermod -g dnsmasq dnsmasq
```#### 配置 dnsmasq```bash
# 备份原配置
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.ori# 编辑新配置
vim /etc/dnsmasq.conf
```**dnsmasq.conf 配置内容**:```ini
# 监听地址
listen-address=127.0.0.1# 缓存大小
cache-size=1000# 上游 DNS 服务器(AWS 内网 DNS 地址)
server=10.19.0.2# 忽略 /etc/resolv.conf,防止循环解析
no-resolv# 可选:记录查询日志(调试时用)
log-queries
log-facility=/var/log/dnsmasq.log
```#### 配置系统 DNS```bash
# 停止 systemd-resolved
systemctl stop systemd-resolved
systemctl disable systemd-resolved# 备份 resolv.conf
mv /etc/resolv.conf /etc/resolv.conf.bak# 配置使用本地 DNS
echo "nameserver 127.0.0.1" | tee /etc/resolv.conf# 启动并启用 dnsmasq
systemctl restart dnsmasq
systemctl enable dnsmasq
```#### 配置日志滚动```bash
# 创建日志滚动配置
vim /etc/logrotate.d/dnsmasq
```**logrotate 配置内容**:```
/var/log/dnsmasq.log {dailyrotate 3missingoknotifemptycompressdelaycompresscreate 0640su root admcopytruncate
}
```### 1.2 Amazon Linux 系统参考 AWS 官方文档:
> https://repost.aws/zh-Hans/knowledge-center/dns-resolution-failures-ec2-linux

### 1.3 验证 DNS 缓存```bash
# 首次查询(无缓存)
time dig example.com# 再次查询(有缓存,应该更快)
time dig example.com# 查看缓存统计
kill -USR1 $(pidof dnsmasq)
cat /var/log/dnsmasq.log | grep "cache"
```---## 二、EKS 使用 NodeLocalDNS### 2.1 概述NodeLocalDNS 通过在每个节点上运行 DNS 缓存代理来提高集群 DNS 性能。> 官方文档:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/nodelocaldns/

### 2.2 部署步骤#### 下载配置文件```bash
wget https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/cluster/addons/dns/nodelocaldns/nodelocaldns.yaml
```#### 获取集群信息并修改配置```bash
# 获取 kube-dns ClusterIP
kubedns=$(kubectl get svc kube-dns -n kube-system -o jsonpath={.spec.clusterIP})# 设置集群域名
domain=cluster.local# NodeLocalDNS 监听地址
localdns=169.254.20.10# 替换配置文件中的占位符(仅适用于 kube-proxy 为 iptable 模式的集群)
sed -i "s/__PILLAR__LOCAL__DNS__/$localdns/g; \s/__PILLAR__DNS__DOMAIN__/$domain/g; \s/__PILLAR__DNS__SERVER__/$kubedns/g" \
        nodelocaldns.yaml
```#### 部署 NodeLocalDNS```bash
kubectl apply -f nodelocaldns.yaml
```### 2.3 验证部署```bash
# 检查 NodeLocalDNS Pod 状态
kubectl get pods -n kube-system -l k8s-app=node-local-dns# 预期输出:每个节点一个 Running 的 Pod
NAME                    READY   STATUS    RESTARTS   AGE
node-local-dns-xxxxx    1/1     Running   0          5m
node-local-dns-yyyyy    1/1     Running   0          5m
node-local-dns-zzzzz    1/1     Running   0          5m
```### 2.4 配置 Pod 使用 NodeLocalDNS默认情况下,Pod 会自动使用 NodeLocalDNS。也可以显式配置:```yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod
spec:dnsConfig:nameservers:- 169.254.20.10searches:- default.svc.cluster.local- svc.cluster.local- cluster.localoptions:- name: ndotsvalue: "5"containers:- name: testimage: busyboxcommand: ["sleep", "3600"]
```---## 三、最佳实践### 3.1 EC2 DNS 缓存| 场景 | 建议 |
|------|------|
| **高 DNS 查询场景** | 必须启用本地缓存 |
| **缓存大小** | 根据查询量设置,默认 1000 足够 |
| **上游 DNS** | 使用 VPC DNS(x.x.x.2) |
| **日志** | 生产环境可关闭 log-queries |### 3.2 EKS NodeLocalDNS| 场景 | 建议 |
|------|------|
| **大规模集群** | 强烈建议启用 |
| **DNS 查询密集应用** | 显著降低延迟 |
| **跨 AZ 流量** | 减少跨 AZ DNS 查询 |### 3.3 DNS 优化效果| 指标 | 无缓存 | 有缓存 | 提升 |
|------|--------|--------|------|
| DNS 查询延迟 | 1-5ms | <0.1ms | 10-50x |
| CoreDNS 负载 | 高 | 低 | 显著降低 |
| 跨 AZ 流量 | 有 | 无 | 消除 |---## 四、架构图### 4.1 EC2 DNS 缓存架构```
┌─────────────────────────────────────────────────────────────┐
│                        EC2 Instance                          │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│   ┌─────────────┐         ┌─────────────┐                   │
│   │ Application │────────►│  dnsmasq    │                   │
│   │             │   DNS   │  127.0.0.1  │                   │
│   └─────────────┘  Query  └──────┬──────┘                   │
│                                  │                           │
│                          ┌───────┴───────┐                  │
│                          │               │                  │
│                     Cache Hit      Cache Miss               │
│                          │               │                  │
│                          ▼               ▼                  │
│                    ┌─────────┐    ┌─────────────┐           │
│                    │ 返回缓存 │    │ 查询上游DNS │           │
│                    │ (<0.1ms)│    │  (1-5ms)   │           │
│                    └─────────┘    └──────┬──────┘           │
│                                          │                  │
└──────────────────────────────────────────┼──────────────────┘│▼┌─────────────┐│  VPC DNS    ││  10.x.x.2   │└─────────────┘
```### 4.2 EKS NodeLocalDNS 架构```
┌─────────────────────────────────────────────────────────────┐
│                        EKS Node                              │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│   ┌─────────────┐         ┌─────────────────┐               │
│   │    Pod      │────────►│  NodeLocalDNS   │               │
│   │             │   DNS   │  169.254.20.10  │               │
│   └─────────────┘  Query  └────────┬────────┘               │
│                                    │                        │
│                            ┌───────┴───────┐                │
│                            │               │                │
│                       Cache Hit      Cache Miss             │
│                            │               │                │
│                            ▼               ▼                │
│                      ┌─────────┐    ┌───────────┐           │
│                      │ 返回缓存 │    │ 转发查询  │           │
│                      └─────────┘    └─────┬─────┘           │
│                                           │                 │
└───────────────────────────────────────────┼─────────────────┘│▼┌─────────────┐│  CoreDNS    ││ (ClusterIP) │└─────────────┘
```---

 

http://www.jsqmd.com/news/412559/

相关文章:

  • 基于 Kubernetes + Helm 部署高可用 ETCD 集群
  • OS 核心知识点全解析(一)
  • Redis 迁移方案-RedisShake
  • qml可拖动折线图
  • 【linuxqt】qsql_mysql.cpp:57:10: fatal error: QtSql/private/qsqldriver_p.h: No such file or directory
  • 我草我怎么这么牛
  • 基于 AWS Global Accelerator 实现全球低延迟访问-RapidX 全球加速方案
  • day96(2.25)——leetcode面试经典150
  • 【Linux】进程的页表详解
  • YOLO26最新创新改进系列:主干网络全新设计——EfficientNetV2-BackBone ,引入渐进式学习策略、自适应正则强度调整机制,共同优化训练速度和参数效率,全方位提升模型检测性能!!
  • YOLO26最新创新改进系列:融入AKConv(可改变核卷积),加强特征提取,任意数量的参数和任意采样形状,为网络开销和性能之间的权衡提供了更丰富的选择。 拉升检测性能!
  • 瑞芯微开发板开机自启动设置
  • FastAsyncWorldEdit zh-cn strings.json 中文汉化
  • **0-1 背包问题中回溯法的搜索过程、通用解题步骤及两种算法实现框架(递归与非递归)**,是算法设计与分析中的经典范例
  • SRE 团队体系建设之路
  • 惊叹!大数据数据增强如何颠覆传统模式
  • int[] 与 integer[] 相互转换
  • Amazon EMR 高可用 EMR 部署注意事项及关键配置
  • SolidPlant 2020管道设计软件安装包(含详细图文教程)|兼容SolidWorks 2017及以上版本
  • 【踩坑】MacOS26上的浏览器无法显示麦克风/摄像头列表
  • 【DFS】BISHI77数水坑
  • SimpleDateFormat(YYYY-MM-dd)格式化时间出现了bug?
  • 《P1973 [NOI2011] NOI 嘉年华》
  • 华为OD机考双机位C卷 - 几何平均值最大子数组 (Java Python JS GO C++ C)
  • 实现一个简单的文本摘要生成器。
  • pyTorch环境搭建及遇到的算力问题
  • 卷积神经网络(CNN)简介-卷积神经网络介绍
  • 【RCCL】RCCL工具
  • 大数据交易数据湖架构设计指南
  • 2026年2月25日