使用openswitch和vyos操作系统在虚拟机之间组建vxlan
使用ovs组建vxlan
网络拓扑
ovs网桥配置
添加网桥
ovs-vsctl add-br br-ovs # 内部网络网桥
添加端口
ovs-vsctl add-port br-ovs ovs-vm1-eth -- set Interface ovs-vm1-eth type=internal # vm1使用的网卡接口
ovs-vsctl set Port ovs-vm1-eth tag=201 # 设置vm1网卡隔离域标志
ovs-vsctl add-port br-ovs ovs-vm2-eth -- set Interface ovs-vm2-eth type=internal # vm2使用的网卡接口
ovs-vsctl add-port br-ovs ovs-vyos1-eth -- set Interface ovs-vyos1-eth type=internal # vyos1使用的网卡接口
ovs-vsctl add-port br-ovs ovs-vyos2-eth -- set Interface ovs-vyos2-eth type=internal # vyos2使用的网卡接口
添加vxlan端口
ovs-vsctl add-port br-ovs vxlan-vx01 -- set Interface vxlan-vx01 type=vxlan options:remote_ip=10.75.206.7 options:key=flow # 与206.7建立隧道
ovs-vsctl add-port br-ovs vxlan-vx02 -- set Interface vxlan-vx02 type=vxlan options:remote_ip=10.75.206.21 options:key=flow # 与206.21建立隧道
外部网络网桥
物理机10.75.206.6网络配置, 新建网桥br-eno1, 连接外部网卡eno1
在br-eno1上新建两个端口
vyos1-inside-----10.75.206.152
vyos2-inside-----10.75.206.153
分别作为vyos1和vyos2的外部网卡。
配置虚拟机
kvm创建虚拟机
2台安装vyos镜像作为内部网关, 虚拟路由器, 其余安装正常的centos镜像。
虚拟机内部网络网卡与网桥上添加的端口编号一一对应。
centos虚拟机配置ip
ip addr add 192.168.201.211/24 dev eth0
ip link set eth0 up
开启ssh
systemctl start sshd
添加网关
route add default gw 192.168.201.1
vyos配置ip 以vyos1为例, vyos2配置基本相同
外部网络配置,可以选择dhcp或者静态ip
set interface ethernet eth0 address dhcp
set interface ethernet eth0 address 10.75.206.152/24
set interface ethernet eth0 description "OUTSIDE"
set protocols static route 0.0.0.0/0 next-hop <address> # 外部网络网关
set service ssh port 22 # 开启ssh
内部网络配置
set interface ethernet eth1 address 192.168.201.1/24
set interface ethernet eth1 description "INSIDE"
使用vyos管理网络
进行上述配置后, vxlan已经建立成功,局域网内可以互相ping通, 不同的tag可以区分不同的隔离域, 互相之间不能通信, 不同隔离域内相同ip也不会冲突。 使用vyos配置端口转发实现对内对外的访问。
对外访问时snat转换为出局地址
set nat source rule 10 outbound-interface 'eth1'
set nat source rule 10 source address '192.168.225.3/24'
set nat source rule 10 translation address 'masquerade'
外部网络不能直接访问子网内节点, 需要在vyos处配置dnat将外部访问的端口映射到内部节点的端口
例如通过外部网络实现ssh访问vm2,dnat转换vyos的指定ip地址10.75.206.162:9821-->vm2:192.168.201.211:22
set nat destination rule 10 description 'Port Forward: 10.75.206.162:9821 to 192.168.201.211:22'
set nat destination rule 10 inbound-interface eth0
set nat destination rule 10 destination address 10.75.206.162
set nat destination rule 10 destination port 9821
set nat destination rule 10 protocol tcp
set nat destination rule 10 translation address 192.168.201.211
set nat destination rule 10 translation port 22
ssh不能连接原因及解决办法
vxlan组建后, 局域网内可ping通, 但是互相ssh不能连通。 排查原因发现是ovs设置的vxlan没有开启分片, 超过mtu的包直接被丢弃, 导致不能正确传输ssh密钥。 参考文章: 通过ssh连接debug排查原因 ssh -v root@192.168.201.211 www.cnblogs.com/starof/p/47…
卡在expecting SSH2_MSG_KEX_ECDH_REPLY这一步 查询这个报错信息 ssh密钥大于mtu被丢弃 blog.csdn.net/wang_zhenwe…
配置vxlan支持分片 blog.csdn.net/hailwind/ar…
解决办法
ovs-vsctl set Interface vxlan-vx01 options:df_default=false
下次新建vxlan的时候就直接配置好属性
ovs-vsctl add-port br-ovs vxlan-vx01 -- set Interface vxlan-vx01 type=vxlan options:remote_ip=10.75.206.7 options:key=flow options:df_default=false # 与206.7建立隧道
其他常用指令
ovs配置
ovs-vsctl show # 查看网桥
ovs-vsctl add-br <br-int># 添加网桥
ovs-vsctl add-br br-eno1
ovs-vsctl list-br # 查看网桥列表
ovs-vsctl del-br # 删除网桥
ovs-vsctl add-port <br-int> <port> # 添加端口到网桥
ovs-vsctl add-port br-eno1 eth0
ovs-vsctl del-port <br-int> <port> # 删除网桥x
ovs-vsctl set Port vnet0 tag=100 # 设置VLAN tag
ovs-vsctl remove Port vnet0 tag 100 # 移除vnet0上面的VLAN tag配置
ovs-vsctl set Port vnet0 trunks=100,200 # 设置vnet0允许通过的VLAN tag
ovs-vsctl remove Port vnet0 trunks 100,200 # 移除vnet0允许通过的的VLAN tag配置
kvm相关
virsh list # 查看虚拟机列表
virsh domiflist vm1 # 查看对应虚拟机的网络
vyos
set system name-server 223.5.5.5 # 添加dns服务器
linux下网络配置
ip route show # 查看网关/路由