關於vlan的冷知識
vlan范圍:0~4095
0,4095 保留 僅限系統使用 用戶不能查看和使用這些VLAN
1 正常 Cisco默認VLAN 用戶能夠使用該VLAN,但不能刪除它
2-1001 正常 用於以太網的VLAN 用戶可以創建、使用和刪除這些VLAN
1002-1005 正常 用於FDDI和令牌環的Cisco默認VLAN 用戶不能刪除這些VLAN
1006-1024 保留 僅限系統使用 用戶不能查看和使用這些VLAN
1025-4094 擴展 僅用於以太網VLAN
實驗環境
發現在workstation上不支持混雜模式,導致vlan不通
在centos上
- 在no1 no2上開啟vlan功能
modprobe --first-time 8021q
modinfo 8021q
- 在no1和no2上添加2個vlan
ip link add link eth0 name eth0.10 type vlan id 10
ip link set dev eth0.10 up
ip link add link eth0 name eth0.20 type vlan id 20
ip link set dev eth0.20 up
- 在no1上操作docker
docker network create -d macvlan --subnet=172.16.10.0/24 --gateway=172.16.10.1 -o parent=eth0.10 mac_net10
docker network create -d macvlan --subnet=172.16.20.0/24 --gateway=172.16.20.1 -o parent=eth0.20 mac_net20
docker run -itd --name b1 --ip=172.16.10.10 --network mac_net10 busybox
docker run -itd --name b2 --ip=172.16.20.10 --network mac_net20 busybox
- 在no2上操作docker
docker network create -d macvlan --subnet=172.16.10.0/24 --gateway=172.16.10.1 -o parent=eth0.10 mac_net10
docker network create -d macvlan --subnet=172.16.20.0/24 --gateway=172.16.20.1 -o parent=eth0.20 mac_net20
docker run -itd --name b3 --ip=172.16.10.11 --network mac_net10 busybox
docker run -itd --name b4 --ip=172.16.20.11 --network mac_net20 busybox
- 測試
通
$ docker exec b3 ping 172.16.10.10
PING 172.16.10.10 (172.16.10.10): 56 data bytes
64 bytes from 172.16.10.10: seq=0 ttl=64 time=0.434 ms
64 bytes from 172.16.10.10: seq=1 ttl=64 time=0.516 ms
^C
不通
$ docker exec b3 ping 172.16.20.10
查看802.1q的數據包
https://github.com/lannyMa/scripts/blob/master/802.1q-vlan.pcap
使用單臂路由的模式使得2個vlan相通
- 配置no3-容器的網關
ip link add link eth0 name eth0.10 type vlan id 10
ip link set dev eth0.10 up
ip link add link eth0 name eth0.20 type vlan id 20
ip link set dev eth0.20 up
ifconfig eth0.10 172.16.10.1 netmask 255.255.255.0 up
ifconfig eth0.20 172.16.20.1 netmask 255.255.255.0 up
- 用n3的vlan10 ping n1的vlan20的地址
$ docker exec b3 ping 172.16.20.10
PING 172.16.20.10 (172.16.20.10): 56 data bytes
64 bytes from 172.16.20.10: seq=0 ttl=63 time=0.573 ms
64 bytes from 172.16.20.10: seq=1 ttl=63 time=0.576 ms
64 bytes from 172.16.20.10: seq=2 ttl=63 time=0.555 ms
Ubuntu版-vbox里做的,因為他的網卡支持混雜模式
參考: Ubuntu16.04的docker阿里雲安裝: https://yq.aliyun.com/articles/110806
apt-get install vlan
ip link add link enp0s8 name enp0s8.10 type vlan id 10
ip link set dev enp0s8.10 up
ip link add link enp0s8 name enp0s8.20 type vlan id 20
ip link set dev enp0s8.20 up
docker network create -d macvlan --subnet=172.16.10.0/24 --gateway=172.16.10.1 -o parent=enp0s8.10 mac_net10
docker network create -d macvlan --subnet=172.16.20.0/24 --gateway=172.16.20.1 -o parent=enp0s8.20 mac_net20
docker run -itd --name b1 --ip=172.16.10.10 --network mac_net10 busybox
docker run -itd --name b2 --ip=172.16.20.10 --network mac_net20 busybox
docker network create -d macvlan --subnet=172.16.10.0/24 --gateway=172.16.10.1 -o parent=enp0s8.10 mac_net10
docker network create -d macvlan --subnet=172.16.20.0/24 --gateway=172.16.20.1 -o parent=enp0s8.20 mac_net20
docker run -itd --name b3 --ip=172.16.10.11 --network mac_net10 busybox
docker run -itd --name b4 --ip=172.16.20.11 --network mac_net20 busybox