这里我使用docker一个小巧轻量的dns服务器,它的大小仅仅6M左右,镜像为lhstack/small-dns,它的功能比较单一,目前仅支持Ptr和Ipv4的域名映射和解析转发,不过也能满足我们内网的大部分需求,同时2.0.0版本支持正则域名解析,以及修改配置自动加载最新配置等功能,使它变得更加灵活
查找镜像
docker search small-dns
docker pull small-dns
docker run --name small-dns -p 53:53/udp -d lhstack/small-dns
通过日志可以看到,这里有几个环境变量,DNS_REPO_TYPE支持Local和HighLevelLocal两种,我们后面将使用HighLevelLocal,Local是1.0.0版本的兼容层,后面不予演示
加载了/app/config/config.json配置,这里查看加载的配置内容
停掉之前启动的容器
docker rm -f small-dns
使用HighLevelLocal方式
docker run --name small-dns -p 53:53/udp -e DNS_REPO_TYPE=HighLevelLocal -d lhstack/small-dns
可以看到,这里有以下环境变量
HIGH_LEVEL_LOCAL_DNS_RESOLVE_CONFIG_CHECK_INTERVAL_SECONDS: 配置检查时间间隔,当配置内容发生修改,可以及时加载最新的配置
HIGH_LEVEL_LOCAL_DNS_RESOLVE_CONFIG: 配置文件的地址
DNS_REPO_TYPE: 支持的dns解析配置存储的仓库类型
DNS_REMOTE_RESOLVE_ADDRESS: 需要转发到远程的dns地址
DNS_LISTEN_ADDRESS: dns启动时监听的udp地址,使用golang的地址解析方式
mkdir config
cat >config/config.json<<EOF
[
{
"type": "Ptr",
"names": [
"127.0.0.1",
"192\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}"
],
"ttl": 600,
"values": [
"www.lhstack.com",
"www.lhstack.cm",
"lhstack.com"
]
},
{
"type": "Ipv4",
"names": [
"www\\\\.l[\\\\d\\\\w]{1,3}k\\\\.org"
],
"ttl": 600,
"values": [
"192.168.2.188",
"192.168.2.37",
"192.168.2.36"
]
}
]
EOF
docker rm -f small-dns
docker run --name small-dns -p 53:53/udp -e HIGH_LEVEL_LOCAL_DNS_RESOLVE_CONFIG=/config/config.json -e DNS_REPO_TYPE=HighLevelLocal -v ${PWD}/config:/config -d lhstack/small-dns
mkdir conf.d config
cat > conf.d/default.conf <<EOF
server {
listen 80;
server_name www.lhstack.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOF
cat > conf.d/gitee.conf <<EOF
server {
listen 80;
server_name www.lhstack.org;
location / {
proxy_pass https://www.baidu.com;
}
}
EOF
cat >config/config.json<<EOF
[
{
"type": "Ptr",
"names": [
"127.0.0.1",
"192\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}"
],
"ttl": 600,
"values": [
"www.lhstack.com",
"www.lhstack.cm",
"lhstack.com"
]
},
{
"type": "Ipv4",
"names": [
"www\\\\.lhstack\\\\.[\\\\w\\\\d]*"
],
"ttl": 600,
"values": [
"192.168.101.170"
]
}
]
EOF
cat > docker-compose.yml <<EOF
version: '3'
services:
dns:
image: lhstack/small-dns
container_name: small-dns
restart: always
ports:
- '53:53/udp'
environment:
HIGH_LEVEL_LOCAL_DNS_RESOLVE_CONFIG: "/config/config.json"
DNS_REPO_TYPE: "HighLevelLocal"
volumes:
- ./config:/config
logging:
options:
max-file: '1'
max-size: '16k'
deploy:
resources:
limits:
cpus: '0.1'
memory: '6M'
nginx:
image: nginx:alpine
container_name: nginx
restart: always
ports:
- '80:80'
- '443:443'
volumes:
- ./conf.d:/etc/nginx/conf.d
logging:
options:
max-file: '1'
max-size: '16k'
deploy:
resources:
limits:
cpus: '1'
memory: '64M'
EOF
docker-compose up -d
small-dns可以使用正则方式解析自定义的域名到指定地址,同时也支持解析公网域名,根据配置调整即可