
Minha tentativa é modelada noeste tutorial.
Consigo fazer ping do namespace para a rede se a interface física não estiver atribuída à ponte.
# Create namespace
ip netns add namespace1
# Create veth pair.
ip link add veth1 type veth peer name br-veth1
# Associate the non `br-` side with the namespace.
ip link set veth1 netns namespace1
# Give namespace-side veth ip addresses.
ip netns exec namespace1 ip addr add 192.168.1.11/24 dev veth1
# Create a bridge device naming it `br1` and set it up.
ip link add name br1 type bridge
# Turn up the bridge.
ip link set br1 up
# Set the bridge veth from the default namespace up.
ip link set br-veth1 up
# Set the veth from the namespace up too.
ip netns exec namespace1 ip link set veth1 up
# Add the br-veth1 interface to the bridge by setting the bridge device as their master.
ip link set br-veth1 master br1
# Add the physical interface to the bridge
ip link set enp3s0 master br1
# Set the address of the `br1` interface (bridge device) to 192.168.1.10/24 and also set the broadcast address to 192.168.1.255 (the `+` symbol sets the host bits to 255).
ip addr add 192.168.1.10/24 brd + dev br1
# add the default gateway in all the network namespace.
ip netns exec namespace1 ip route add default via 192.168.1.10
# Set us up to have responses from the network.
# -t specifies the table to which the commands should be directed to. By default, it's `filter`.
# -A specifies that we're appending a rule to the chain that we tell the name after it.
# -s specifies a source address (with a mask in this case).
# -j specifies the target to jump to (what action to take).
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
sysctl -w net.ipv4.ip_forward=1
Responder1
Não use veth + ponte! Use macvlan!
Eu estava lutando com veth + bridge recentemente como você, felizmente encontreiesse linkesta noite, que diz:
Antes do MACVLAN, se você quisesse se conectar à rede física a partir de uma VM ou namespace, seria necessário criar dispositivos TAP/VETH e anexar um lado a uma ponte e anexar uma interface física à ponte no host ao mesmo tempo, como mostrado abaixo.
Agora, com o MACVLAN, você pode vincular uma interface física associada a um MACVLAN diretamente aos namespaces, sem a necessidade de uma ponte.
E foi isso que eu fiz:
$ sudo ip netns add ns0
$ sudo ip netns exec ns0 ip link set lo up
$ sudo ip link add macvlan0 link eth0 type macvlan mode bridge
$ sudo ip link set macvlan0 netns ns0
$ sudo ip netns exec ns0 ip link set macvlan0 up
$ sudo ip netns exec ns0 ip addr add 172.29.6.123/21 dev macvlan0
$ sudo ip netns exec ns0 ping 172.29.0.1
PING 172.29.0.1 (172.29.0.1) 56(84) bytes of data.
64 bytes from 172.29.0.1: icmp_seq=1 ttl=64 time=0.360 ms
64 bytes from 172.29.0.1: icmp_seq=2 ttl=64 time=0.412 ms
Está funcionando!
Responder2
Tudo parece bem até os dois últimos comandos (gateway padrão no namespace da rede + mascaramento no namespace principal).
Se você pular esses dois, deverá ter uma configuração em que a interface física seja interligada a duas interfaces internas, uma interna 192.168.1.10
da ponte no namespace principal e outra 192.168.1.11
no namespace1
.
Portanto, isso funciona da mesma maneira que ter duas interfaces de rede física conectadas à mesma sub-rede, uma do namespace principal e outra do namespace namespace
. (Você pode obter o mesmo efeito com um macvlan
par em vez de um veth).
Nem o encaminhamento nem o mascaramento são necessários, e uma rota padrão a partir 192.168.1.10
do namespace principal está errada.
Se as rotas estiverem corretas para ambos os namespaces (verifique isso), você poderá executar ping na outra interface e também em tudo o que estiver conectado à interface física.
Para testes , recomendo iniciar um xterm
etc. namespace1
então você pode configurar tudo diretamente, tendo que digitar ip netns exec namespace1 ip ...
o tempo todo.