network namespaces netns

preparation

ufw disable

create namespapce

ip netns add red
ip netns add blue

veth

ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red
ip link set veth-blue netns blue
ip -n red addr add 192.168.15.1 dev veth-red
ip -n blue addr add 192.168.15.2 dev veth-blue
ip -n red link set veth-red up
ip -n blue link set veth-blue up
 
# delete
ip -n  red link del veth-red
 
# show unattached veth
ip -c link show type veth

bride

# show unattached bride
ip link add v-net-0 type bridge
ip link set dev v-net-0 up
 
ip link add veth-red type veth peer name veth-red-br
ip link set veth-red netns red
ip -n red l set veth-red up
 
ip link add veth-blue type veth peer name veth-blue-br
ip link set veth-blue netns blue
ip -n blue l set veth-blue up
 
ip link set veth-red-br master v-net-0
ip -n red addr add 192.168.15.1/24 dev veth-red
ip l set veth-blue-br up
 
ip link set veth-blue-br master v-net-0
ip -n blue addr add 192.168.15.2/24 dev veth-blue
ip l set veth-red-br up
 
ip -c link show type bridge

forward

ip addr add 192.168.15.5/24 dev v-net-0
ip -n blue r add 192.168.178.0/24 via 192.168.15.5
iptables -t nat -A POSTROUTING -s 192.168.15.0/24 -j MASQUERADE
ip -n blue r add default via 192.168.15.5
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.15.2:80

Liinks
https://matthewarcus.wordpress.com/2018/02/04/veth-devices-network-namespaces-and-open-vswitch/