sudo modprobe macvlan
sudo ip link set enp0s3 promisc on
#建立新的macvlan network
docker network create -d macvlan --subnet 192.168.43.0/24 --gateway 192.168.43.1 --ip-range 192.168.43.192/27 --aux-address 'host=192.168.43.65' -o parent=enp0s3 -o macvlan_mode=bridge macnet
sudo ip link add serverFarm link enp0s3 type macvlan mode bridge
sudo ip link set serverFarm up
sudo ip route add 192.168.43.192/27 dev serverFarm
docker run -id --net macnet --name c1 busybox sh
docker exec c1 ping 192.168.43.65
sudo apt install sipcalc
#計算CIDR可用IP範圍
sipcalc 192.168.43.192/27
Usable range - 192.168.43.193 - 192.168.43.222
#指定容器使用的IP(需在 192.168.43.192/27 範圍中)
docker run -id --net macnet --ip 192.168.43.195 --name c2 busybox sh
docker run -id --net macnet --ip 192.168.43.199 --name nginx nginx
透過 portainer 可以看資源使用狀況
如何開機啟動
sudo nano /etc/rc.local
#!/bin/sh
card=enp0s3
serverFarmRange=192.168.43.192/27
sudo modprobe macvlan
sudo ip link set $card promisc on
sudo ip link add serverFarm link $card type macvlan mode bridge
sudo ip link set serverFarm up
sudo ip route add $serverFarmRange dev serverFarm
exit 0