#1、先查询本机电脑是否安装iproute
[root@bogon ~]# rpm -q iproute
#2、添加2个网络名称空间
[root@bogon ~]# ip netns add r1
[root@bogon ~]# ip netns add r2
#3、在某个网络名称空间中执行命令,例如查询 #exec 为在网络空间中执行命令
[root@bogon ~]# ip netns exec r1 ifconfig -a
lo: flags=8<LOOPBACK> mtu 65536
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#4、增加2个虚拟网口
[root@bogon ~]# ip link add name veth1.1 type veth peer name veth1.2
#增加2个名为veth1.1、1.2 网口类型为veth
#veth1.1(10.1.0.1/24) 作为外部linux主机网卡,veth1.2(10.1.0.2/24)作为内部网络空间网卡
[root@bogon ~]# ip link show
#新增的2个网口并未激活,无法使用ifconfig查看
#5、把1.2网口移到新增的网络空间中,并更改名称、激活
[root@bogon ~]# ip link set dev veth1.2 netns r1
[root@bogon ~]# ip netns exec r1 ifconfig -a
veth1.2: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 1a:9b:06:30:6d:44 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@bogon ~]# ip netns exec r1 ip link set dev veth1.2 name eth0
[root@bogon ~]# ip netns exec r1 ifconfig -a
eth0: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 1a:9b:06:30:6d:44 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@bogon ~]# ip netns exec r1 ifconfig eth0 10.1.0.2/24 up
[root@bogon ~]# ip netns exec r1 ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 10.1.0.2 netmask 255.255.255.0 broadcast 10.1.0.255
ether 1a:9b:06:30:6d:44 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#6、设置外部网卡1.1,并测试连通性
[root@bogon ~]# ifconfig veth1.1 10.1.0.1/24 up
[root@bogon ~]# ifconfig
veth1.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.1.0.1 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::4c87:12ff:fe33:7f1e prefixlen 64 scopeid 0x20<link>
ether 4e:87:12:33:7f:1e txqueuelen 1000 (Ethernet)
RX packets 6 bytes 516 (516.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 2962 (2.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@bogon ~]# ping 10.1.0.2
PING 10.1.0.2 (10.1.0.2) 56(84) bytes of data.
64 bytes from 10.1.0.2: icmp_seq=1 ttl=64 time=0.117 ms
# docker run --name NAME --rm --network TYPE -p PORT -it SERVER #指定网络类型;暴露指定端口
[root@bogon ~]# docker run --name myweb --rm -p 192.168.176.173::80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 192.168.176.173:49153
#指定宿主机的IP地址上的动态端口
[root@bogon ~]# docker run --name myweb --rm -p 80:80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
http://192.168.176.173/
#指定宿主机的IP地址上的80端口
[root@bogon ~]# docker run --name myweb --rm -p 192.168.176.173:8080:80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 192.168.176.173:8080
#指定8080
# docker run --name NAME --rm --network TYPE -p PORT -it SERVER #指定网络类型;暴露指定端口
[root@bogon ~]# docker run --name myweb --rm -p 192.168.176.173::80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 192.168.176.173:49153
#指定宿主机的IP地址上的动态端口
[root@bogon ~]# docker run --name myweb --rm -p 80:80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
http://192.168.176.173/
#指定宿主机的IP地址上的80端口
[root@bogon ~]# docker run --name myweb --rm -p 192.168.176.173:8080:80 test/httpd:v0.2
[root@bogon ~]# docker port myweb
80/tcp -> 192.168.176.173:8080
#指定8080
# tty1 上启动
[root@bogon ~]# docker run --name b1 --rm -it busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
# tty2 上启动
[root@bogon ~]# docker run --name b2 --network container:b1 --rm -it busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
## 复制b1的网络,网络共享,但是文件系统不共享
/ # [root@bogon ~]# docker run --name b2 --network host --rm -it busybox
/ # ifconfig
ens33 Link encap:Ethernet HWaddr 00:0C:29:45:D0:2C
inet addr:192.168.176.173 Bcast:192.168.176.255 Mask:255.255.255.0
inet6 addr: fe80::6194:4872:728a:decf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15454 errors:0 dropped:0 overruns:0 frame:0
TX packets:5857 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2747424 (2.6 MiB) TX bytes:701072 (684.6 KiB)
## 复制主机的网络