李明
|
源码下载
#共享插件需要的vbox需要vagrant plugin install vagrant-vbguest vagrant up
vagrant ssh docker-node1 sudo service docker restart sudo docker run -d --name test1 busybox /bin/sh -c "while true; do sleep 3600;done"
sudo docker run -d --name test2 busybox /bin/sh -c "while true; do sleep 3600;done"
sudo docker exec -it test1 /bin/sh ip a#test1的网络ip是172.17.0.2
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 5: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever sudo docker exec -it test2 /bin/sh ip a#test1的网络ip是172.17.0.3
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever #在test2上ping下test1的namespace发现一个情况,可以ping通ping 172.17.0.2
exitip a
了解命令
sudo ip netns list
sudo ip netns delete 名称
sudo ip netns add 名称 创建linux的networknamespace。sudo ip netns list sudo ip netns add test3 sudo ip netns list sudo ip netns delete test3 sudo ip netns list sudo ip netns add test1 sudo ip netns add test2 sudo ip netns list
sudo ip netns exec test1 ip a
#sudo ip netns exec test1 ip link set dev lo up sudo ip netns exec test1 ip a
做个实验,按照下面这个图
创建veth 说白了就是创建一根网线,有2个头但是在一根线上 veth-test1 和veth-test2
sudo ip link add veth-test1 type veth peer name veth-test2 sudo ip link
image.png
sudo ip link set veth-test1 netns test1 sudo ip netns exec test1 ip link sudo ip link set veth-test2 netns test2 sudo ip netns exec test2 ip link sudo ip link
sudo ip netns exec test1 ip addr add 192.168.1.1/24 dev veth-test1 sudo ip netns exec test2 ip addr add 192.168.1.2/24 dev veth-test2 sudo ip netns exec test1 ip link sudo ip netns exec test2 ip link#虽然添加了ip地址但是不显示,这是为什么?因为需要把这2个端口都启动起来sudo ip netns exec test1 ip link set dev veth-test1 up sudo ip netns exec test2 ip link set dev veth-test2up sudo ip netns exec test1 ip link sudo ip netns exec test2 ip link sudo ip netns exec test1 ip a sudo ip netns exec test2 ip a
image.png
sudo ip netns exec test1 ping 192.168.1.2 sudo ip netns exec test2 ping 192.168.1.1
image.png PS:通过linux做的个实验跟通过docker创建的容器的是类似的,只是用linux的方式模拟了docker容器的方式。其实docker容器的原理就是围绕这linux底层的网络命名空间的原理实现的。
|
2018-09-08 14:49:23