使用dnsmasq搭建简单的DNS服务

使用dnsmasq搭建简单的DNS服务

前言

最近公司内网用户访问k8s服务越来越多了,之前还是直接在Hosts写死ip,不可能每个用户都这样,对比了PowerDNS,感觉暂时用不到那么复杂的服务,而直接使用dnsmasq快速便利即可实现我的需求。

部署dnsmasq

如果服务器没有dnsmasq服务,则需要安装

centos:
yum install dnsmasq -y
ubuntu:
apt install dnsmasq

详细配置步骤

创建配置目录

(如不存在)mkdir -p /etc/dnsmasq.d

创建对应文件

主配置文件: k8s-gzeport.conf文件

vim /etc/dnsmasq.d/k8s-gzeport.conf

# 内容如下:
# -*- mode: conf -*-
# dnsmasq config for K8s Ingress - Generated at 2024-09-26

# 查询控制
strict-order # 严格匹配配置顺序
all-servers # 并发查询所有上游DNS

# 网络绑定
bind-interfaces # 绑定到指定接口(需配合 interface 使用)
interface=eno16777984 # 替换为实际的内网网卡名(如 eth0、en0、wlan0)
listen-address=192.168.111.101 # 本机内网IP
listen-address=127.0.0.1

# 安全规则
filterwin2k # 过滤Windows查询
bogus-priv # 禁止私有IP反向查询
domain-needed # 防止无域名查询no-negcache

# 特殊域名规则(优先匹配)
host-record=grafana.gzeport.com,192.168.120.50

# 内网子域
# address=/.test.gzeport.com/192.168.120.50
# address=/.dev.gzeport.com/192.168.120.50

# 性能调优
log-queries # 记录查询日志
log-facility=/var/log/dnsmasq-k8s.log
cache-size=10000 # 10k 缓存条目
local-ttl=60 # 本地缓存TTL
dhcp-lease-max=0 # 禁用DHCP
dns-forward-max=150 # 并发查询数

# 上游DNS(其他域名走这里)
no-resolv # 禁用dnsmasq内置的resolv.conf
resolv-file=/etc/resolv.outer.conf

上游DNS配置

vim /etc/resolv.outer.conf

# 内容如下:
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 8.8.8.8
nameserver 1.1.1.1

验证配置

dnsmasq -C /etc/dnsmasq.d/k8s-gzeport.conf --test
# 输出:dnsmasq: syntax check OK.

启动服务

systemctl status dnsmasq
systemctl restart dnsmasq
systemctl enable dnsmasq
# 查看运行状态
systemctl status dnsmasq-k8s

# 查看解析日志
tail -f /var/log/dnsmasq-k8s.log
# 日志实时监控
tail -f /var/log/dnsmasq-k8s.log | grep -E '(query|reply|forward)'
# 统计查询次数
# grep "query" /var/log/dnsmasq-k8s.log | wc -l

测试内外网解析

C:\Users\a9527>nslookup grafana.gzeport.com 192.168.111.101
服务器:  UnKnown
Address:  192.168.111.101

名称:    grafana.gzeport.com
Address:  192.168.120.50

安全加固

chattr +i /etc/resolv.outer.conf  # 防止被覆盖
chmod 600 /etc/dnsmasq.d/*.conf  # 仅root可读
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇