mDNS,Homelab网络的良药

在自己的Homelab里面来折腾多层网络的时候,遇到了一些问题。痛点如下

  1. 机器太多IP实在记不住
  2. 都需要手段的静态IP的绑定非常不优雅
  3. 主机之间的互调和IP强绑定。

首先说一下网络环境:有两层路由,第一个是openwrt 作为家庭的主路由来进行日常上网。第二层路由是pfsence,作为VM网络的统一路由。因为我想把VM网络以及主机作为一个整体来解耦掉。这样VM就能作为一个独立的网络和集群

整个配置环境还是折腾了一下午的时间,在这里记录一下。

路由配置篇

因为一级路由是用的openwrt,二级路由是用的pfsense。但是配置上大同小异。

这里使用的是 avahi 服务,op和pf都可以直接安装。

openwrt安装

opkg update
opkg install avahi-daemon-service-ssh avahi-daemon-service-http

之后修改mdns配置

; vim /etc/avahi/avahi-daemon.conf
 [server]
#host-name=foo
#domain-name=local
use-ipv4=yes
use-ipv6=yes
check-response-ttl=no
use-iff-running=no
allow-interfaces=br-lan   #这里是重点

[publish]
publish-addresses=yes
publish-hinfo=yes
publish-workstation=no
publish-domain=yes
#publish-dns-servers=192.168.1.1
#publish-resolv-conf-dns-servers=yes

[reflector]
enable-reflector=yes    #这里是重点
reflect-ipv=no

[rlimits]
#rlimit-as=
rlimit-core=0
rlimit-data=4194304
rlimit-fsize=0
rlimit-nofile=30
rlimit-stack=4194304
rlimit-nproc=3

参考:

  • https://openwrt.org/docs/guide-developer/mdns
  • https://openwrt.org/docs/guide-user/network/zeroconfig/zeroconf

pfSense安装

直接在插件中心安装avahi 的插件。

pf的配置过程是踩了点坑,但是好在最后解决了。因为对WAN暴露mdns是有风险的。(如果上一级是公网)

但是在这里的两层路由的条件下是很有必要的,但是pf为了安全,默认是不开放WAN的

需要对插件的源代码进行修改。去掉强制的wan过滤逻辑。

vi /usr/local/www/avahi_settings.php
//找到两处wan的过滤代码,注释掉。
//unset($available_interfaces['wan']);

之后在web控制台就可以看到WAN的选项出现了。同时选中WAN/LAN enable

不推荐直接修改系统配置,但是这样就不能和GUI同步,且会被覆盖掉。

vi /usr/local/etc/avahi/avahi-daemon.conf
# This file is generated by the pfSense Avahi package.
# Do not edit this file, it will be overwritten automatically.
[server]
allow-interfaces=em0,em1
allow-point-to-point=yes
use-ipv4=yes
use-ipv6=no
enable-dbus=no
cache-entries-max=0

[wide-area]
enable-wide-area=no

[publish]
disable-publishing=no
publish-addresses=yes
publish-hinfo=yes
publish-workstation=yes
publish-domain=yes
publish-aaaa-on-ipv4=no
publish-a-on-ipv6=no
disable-user-service-publishing=yes

[reflector]
enable-reflector=yes

这一步可选,开启系统的dbus

mkdir -p /var/run/dbus/
dbus-daemon --system

这样pf的配置也算配置完毕了。

共同配置

前面漏了一点,这里补上,mdns 是用的5353端口,所以需要开放LAN主机到路由本机的 5353UDP 的防火墙流量。方法大家自行研究了,很简单。

主机配置

首先给自己的主机设置一个hostname

sudo hostnamectl set-hostname server3

修改主机的IP为DHCP模式

$ vim /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens34:
      dhcp4: true
  version: 2
$ netplan apply

启用Resolved服务的mdbs特性,新版本的ubuntu 全部使用systemctl-resolved 控制,所以修改resolve是无效修改了。这点请注意。

$ vim /etc/systemd/resolved.conf
[Resolve]
MulticastDNS=yes
LLMNR=yes
#OR
sed -i "s|#MulticastDNS=no|MulticastDNS=yes|g" /etc/systemd/resolved.conf
sed -i "s|#LLMNR=no|LLMNR=yes|g" /etc/systemd/resolved.conf

systemctl restart systemd-resolved

还有一点大坑的是,这样的话mdns在主机上还是没有生效,需手动的来设置mdns 的功能开启。

root@code-env:~# resolvectl mdns ens34
Link 2 (ens34): no
root@code-env:~# resolvectl mdns ens34 yes

一旦开机重启之后又恢复为no 的状态,这里是netplan 的一个大坑,它用来配置主机的网络。但是他并没有支持mdns 的选项。链接 这里可见在2019年提出的问题,现在还没有更新上。。。

This solution does not work out of the box, because Netplan creates configuration file for the network interface in the /run directory and this file has a higher priority than the file in /etc. It’s possible to enable mDNS with a command like sudo systemd-resolve --set-mdns=yes --interface=enp1s0 but it’s a temporary solution that works only until reboot.

所以这里变成了一个开机自启动(执行)的问题。那就用systemd来解决吧。

vi /etc/systemd/system/[email protected]

[Unit]
Description=Enable MulticastDNS on ens network link
After=systemd-resolved.service
[Service]
ExecStart=resolvectl mdns %i yes
[Install]
WantedBy=multi-user.target
#OR 

cat << EOF > /etc/systemd/system/[email protected]
[Unit]
Description=Enable MulticastDNS on ens network link
After=systemd-resolved.service
[Service]
ExecStart=resolvectl mdns %i yes
[Install]
WantedBy=multi-user.target
EOF



sudo systemctl enable user-set-mdns@ens34
sudo systemctl start user-set-mdns@ens34
sudo systemctl status user-set-mdns@ens34
sudo systemctl restart systemd-resolved

至此主机的配置就完成了。

补充

对于一些老系统没有使用 resolved 来进行DNS 解析的

可以手动的安装 avahi 服务来实现mDNS 的支持。

sudo apt-get install avahi-daemon libnss-mdns libnss-mymachines

验收

完成了整体的mdns配置之后,在也不需使用静态IP来对主机进行绑定。尽情的使用DHCP带来的自由。

所有的内网资源都可以使用 xx.local 的方式来进行访问。

 ping server3.local
 ping code-env.local
 ping vm-proxy.local
 ping pfsense.local
 ping openwrt.local
 ...

主机之间互调也也可以直接使用 hostname.local.

mdns Homelab的良药

更新

补充一下后面重新部署的时候遇到的问题。这里如果PFsense 的二级路由直接配置静态IP的话会导致在一级路由无法获取二级的 mdns记录。这里需要注意。

解决方法是使用一级路由来给他设置静态IP 并且配置二级IP的路由规则在一级的网管之上。

之后重启二级路由即可。

Referer

  • https://forum.netgate.com/topic/134339/new-avahi-package/11
  • https://forum.netgate.com/topic/142916/avahi-failed-to-create-client-object-daemon-not-running/2
  • https://openwrt.org/docs/guide-developer/mdns
  • https://www.linksysinfo.org/index.php?threads/avahi-tutorial-configuring-a-reflector-aka-mdns-repeater.75706/
  • https://0e39bf7b.blog/posts/mdns-on-ubuntu-server/
  • https://bugs.launchpad.net/netplan/+bug/1830507

发表回复

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