# ip netns help Usage: ip netns list ip netns add NAME ip netns set NAME NETNSID ip [-all] netns delete [NAME] ip netns identify [PID] ip netns pids NAME ip [-all] netns exec [NAME] cmd ... ip netns monitor ip netns list-id
示例:
模拟创建docker0及docker网络
1、创建lxcbr0,相当于docker0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 添加一个网桥lxcbr0,相当于docker0 brctl addbr lxcbr0 brctl stp lxcbr0 off ifconfig lxcbr0 192.168.10.1/24 up # 配置网桥IP地址
# 添加网络命名空间ns1 ip netns add ns1 # 激活namespace中的loopback,即127.0.0.1 ip netns exec ns1 ip link set dev lo up
3、添加虚拟网卡
1 2 3 4
# 增加一个pair虚拟网卡,注意其中的veth类型,其中一个网卡要按进容器中 ip link add veth-ns1 type veth peer name lxcbr0.1 # 把 veth-ns1 按到namespace ns1中,这样容器中就会有一个新的网卡了 ip link set veth-ns1 netns ns1
4、修改容器内网卡为eth0,并分配IP
1 2 3 4
# 把容器里的 veth-ns1改名为 eth0 (容器外会冲突,容器内就不会了) ip netns exec ns1 ip link set dev veth-ns1 name eth0 # 为容器中的网卡分配一个IP地址,并激活它 ip netns exec ns1 ifconfig eth0 192.168.10.11/24 up