了解Docker常用网络模式,掌握Docker常用网络模式的使用。本实验主要任务是利用busybox镜像建立容器,容器名称为test_busybox1和test_busybox2,将网络模式设置为none,并为容器配置IP地址,容器test_busybox1的IP设置为172.17.0.100,容器test_busybox2的IP设置为172.17.0.200,要求实现两容器互通。
1. 在原有虚拟机上面完成
1)建立容器test_busybox1,设置网络模式为none
docker run -dit --name=test_busybox1--net=none busybox:latest
docker exec -it test_busybox1/bin/sh
ip address
exit

2)建立容器test_busybox2,设置网络模式为none
docker run -dit --name test_busybox2--net=none busybox:latest
docker exec -it test_busybox2 /bin/sh
ip address
exit

3)为容器test_busybox1设置IP地址,IP地址设置为172.17.0.100。
yum -y install bridge-utils
ip link add veth0 type veth peer name veth1,添加一个veth对,指定veth0的对端是veth1
brctl addif docker0 veth0,将veth0加入网桥
brctl show

ip link set veth0 up
docker inspect test_busybox1 | grep Pid

mkdir -p /var/run/netns
pid1=按照自己进程号来改
ln -s /proc/$pid1/ns/net/var/run/netns/$pid1

ip link set veth1 netns $pid1

ip netns exec $pid1 ip link set dev veth1name eth0

ip netns exec $pid1 ip link set eth0 up

ip netns exec $pid1 ip addr add172.17.0.100/24 dev eth0

ip netns exec $pid1 ip route add defaultvia 172.17.0.1

4)进入容器test_busybox1测试
docker exec -it test_busybox1 /bin/sh
ip address

ping baidu.com
exit

5)为容器test_busybox2设置IP地址,IP地址设置为172.17.0.100
ip link add veth2 type veth peer name veth3,添加一个veth2对,指定veth0的对端是veth3

brctl addif docker0 veth2,将veth2加入网桥
brctl show

ip link set veth2 up
docker inspect test_busybox2 | grep Pid


pid2=按照自己进程号来改
ln -s /proc/$pid2/ns/net/var/run/netns/$pid2,制作软连接
ip link setveth3 netns $pid2,将veth3连接容器busybox2网络
ip netns exec$pid2 ip link set dev veth3 name eth0,将veth3重命名为eth0
ip netns exec$pid2 ip link set eth0 up,启用eth0
ip netns exec$pid2 ip addr add 172.17.0.200/24 dev eth0,在namespace中指定IP地址
ip netns exec$pid2 ip route add default via 172.17.0.1,设置网关

6)进入容器test_busybox2测试
docker exec -it test_busybox2 /bin/sh
ip address
ping baidu.com
exit

测试ping test_busybox1
docker exec -it test_busybox2 /bin/sh
ping 172.17.0.100
exit





