Ubuntu server 24.04 (Linux) 搭建DNS服务器 通过Nginx实现UDP/TCP负载均衡 轻量级dnsmasq服务器

作者 : admin 本文共2177个字,预计阅读时间需要6分钟 发布时间: 2024-06-9 共3人阅读

一 系统运行环境

test@test:~$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="http://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
test@test:~$ uname -a
Linux test 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
test@test:~$ /usr/local/openresty/nginx/sbin/nginx -v
nginx version: openresty/1.25.3.1

二 安装dnsmasq

1 Ubuntu24 输入如下安装

sudo apt update
sudo apt-get  install dnsmasq

2 查看版本

test@test:~$ dnsmasq -v
Dnsmasq version 2.90  Copyright (c) 2000-2024 Simon Kelley
Compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset nftset auth cryptohash DNSSEC loop-detect inotify dumpfile

This software comes with ABSOLUTELY NO WARRANTY.
Dnsmasq is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License, version 2 or 3.

三 Dnsmasq 配置

1 安装配置一台实例

#增加如下
sudo vim  /etc/dnsmasq.conf
#启动端口
port=853
#不解析/etc/hosts文件
no-hosts
#解析域名记录
addn-hosts=/etc/dnsmasq.d/dnsmasq.hosts
#上游dns
resolv-file=/etc/dnsmasq.d/resolv.dnsmasq.conf
#禁用轮询机制
no-poll
#按照顺序解析
strict-order
#记录dns查询日志
log-queries
#设置日志文件
log-facility=/var/log/dnsmasq.log
#本地缓存时间,根据实际情况配置
local-ttl=86500
#缓存数量
cache-size=90000
#监听地址
listen-address=192.168.50.18,127.0.0.1
#增加解析记录
sudo vim  /etc/dnsmasq.d/dnsmasq.hosts
192.168.50.18  www.test.com
#设置上游DNS地址
sudo  vim /etc/dnsmasq.d/resolv.dnsmasq.conf
nameserver 223.6.6.6
nameserver 114.114.114.114
#启动
sudo systemctl start dnsmasq
#系统启动
sudo systemctl enable dnsmasq
#查看状态
sudo systemctl status dnsmasq
#语法检查
sudo  dnsmasq --test
dnsmasq: syntax check OK.

2 参考例子,另外在安装配置一台dnsmasq服务器 

四 openresty(Nginx) 安装 可参考:ubuntu server 24.04 (Linux) 源码编译安装 OpenResty 1.25.3.1 Released-CSDN博客

五 Nginx 配置

#增加udp配置,在http段外面(⊙﹏⊙)
stream {
    # udp负载均衡
    upstream dns-server {
        server 192.168.50.19:53;
        server 192.168.50.18:8053;
    }
 
    server {
        listen 53 udp reuseport;
        proxy_pass dns-server;
        proxy_timeout 9s; #最长的超时时间
        proxy_responses 1; #连接超时时间
        error_log /var/log/dnsmasq.log;
    }
 
}
#重新加载配置
sudo /usr/local/openresty/nginx/sbin/nginx -s reload

六  客户端UDP 负载均衡测试

windows 10 dns 配置为nginx 服务器的IP

Ubuntu server 24.04 (Linux) 搭建DNS服务器 通过Nginx实现UDP/TCP负载均衡 轻量级dnsmasq服务器插图

查看2台服务器 dnsmasq 日志

Ubuntu server 24.04 (Linux) 搭建DNS服务器 通过Nginx实现UDP/TCP负载均衡 轻量级dnsmasq服务器插图(1)

Ubuntu server 24.04 (Linux) 搭建DNS服务器 通过Nginx实现UDP/TCP负载均衡 轻量级dnsmasq服务器插图(2)

本站无任何商业行为
个人在线分享 » Ubuntu server 24.04 (Linux) 搭建DNS服务器 通过Nginx实现UDP/TCP负载均衡 轻量级dnsmasq服务器
E-->