Press "Enter" to skip to content

Mikrotik使用container部署基于alpine的smartdns提供智能dns服务

背景

我这边有策略dns请求的需求,希望有些域名的dns请求能走特定的服务器,之前一直使用的是ros的udp 53端口拦截,然后配合layer7 protools来实现这个目的。 后来新的ros部署中发现这个方式在一些特殊组网场景下不太稳定,有些流量拦截不到,于是想要在ros上面部署一个container然后跑一个smardns的想法。

方案

我的Mikrotik买的是 hap ax2,坑爹的只有128M的硬盘,CPU是ARM64的,这就限定了我的容器必须是ARM架构的,占用空间还要必须非常小。

编译我们镜像的Dockerfile

FROM arm64v8/alpine as builder

RUN apk add --no-cache gcc g++ linux-headers make openssl-dev
RUN wget -qO- https://github.com/pymumu/smartdns/archive/refs/heads/master.zip | unzip -d / -
RUN cd /smartdns-master && make -j && strip --strip-all src/smartdns


FROM arm64v8/alpine

RUN apk add --no-cache openssl libgcc
COPY --from=builder /smartdns-master/etc/smartdns /etc/smartdns
COPY --from=builder /smartdns-master/src/smartdns /usr/local/bin/smartdns

ENTRYPOINT ["smartdns", "-f", "-x"]

如果你的是x86架构的,只需要将arm64v8/alpine换成alpine:latest即可。

将docker镜像推送到你的harbor私服或者中央仓库。

ros配置

/system/device-mode/update container=yes
#启用container模式
/interface/veth/add name=veth1 address=172.17.0.2/24 gateway=172.17.0.1
/interface/bridge/add name=containers
/ip/address/add address=172.17.0.1/24 interface=containers
/interface/bridge/port add bridge=containers interface=veth1

/container/mounts/add name=smartdns src=/smartdns.conf dst=/etc/smartdns/smartdns.conf

/container/config/set registry-url=https://registry-1.docker.io tmpdir=disk1/pull


/container/add remote-image=smartdns/smartdns:latest interface=veth1 root-dir=disk1/smartdns mounts=smartdns 

smartdns配置

bind [::]:53 -no-cache -no-speed-check

server 10.255.255.1
server 192.168.1.1 -group office -exclude-default-group

nameserver /*.office.com/office 

将这个smartdns.conf文件传到ros的文件目录就行了,然后启动你的容器。

将你的dhcp的dns设置成172.17.0.2容器的地址即可。

如果对smartdns不了解,不建议瞎几把改配置,就用官方配置蛮好的,顶多加个server指定下上游服务器,别的最好别改。

参考资料:

https://github.com/laboys/docker-smartdns.git

https://help.mikrotik.com/docs/display/ROS/Container

https://pymumu.github.io/smartdns/config/domain-forwarding/

https://github.com/pymumu/smartdns/blob/master/etc/smartdns/smartdns.conf

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注