最近公司的幾個關鍵業務跑在openstack中的虛擬機中,想把幾個虛擬機作成負載均衡和高可用集羣。node
對於負載均衡,G版本已經集成了haproxy插件,對haproxy的配置作了一層封裝,能夠很方便的經過quantum去建立一個負載均衡池,爲相同或者不一樣宿主機上的虛擬機提供負載均衡的能力。python
在這個模式下,haproxy是運行在宿主機上的。linux
遺憾的是,目前還不能經過openstack作到haproxy的高可用。負載均衡
想要作高可用,只能在虛擬機中去飄VIP了python2.7
可是建立了虛擬機以後,在這個虛擬機實例中只能使用指定的IP。tcp
這就致使想在虛擬機中部署高可用去飄VIP是不可行的。ide
能夠理解,在公有云環境下,是不可能讓用戶在虛擬機中隨意去配置額外地址的。測試
但咱們是私有云環境,這個規則對私有云環境下非常麻煩。ui
在openstack中建立虛擬機,經過nova boot的--nic選項指定網卡和IP地址:spa --nic net-id=${NETWORK_ID},v4-fixed-ip=${Host_IP} |
以前一直覺得是iptables規則致使的。因而去看了一遍宿主機中的iptables規則
root@node1:~# iptables -vnL Chain INPUT (policy ACCEPT 3556K packets, 744M bytes) pkts bytes target prot opt in out source destination 1778K 372M nova-compute-INPUT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 150 13488 nova-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0 6 1392 nova-compute-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 4208K packets, 567M bytes) pkts bytes target prot opt in out source destination 4202K 567M nova-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0 2106K 284M nova-compute-OUTPUT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain nova-compute-FORWARD (1 references) pkts bytes target prot opt in out source destination 4 1312 ACCEPT udp -- * * 0.0.0.0 255.255.255.255 udp spt:68 dpt:67 2 80 ACCEPT all -- brq3eefcd79-07 * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- * brq3eefcd79-07 0.0.0.0/0 0.0.0.0/0 Chain nova-compute-INPUT (1 references) pkts bytes target prot opt in out source destination 2 656 ACCEPT udp -- * * 0.0.0.0 255.255.255.255 udp spt:68 dpt:67 Chain nova-compute-OUTPUT (1 references) pkts bytes target prot opt in out source destination Chain nova-compute-inst-15 (1 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 nova-compute-provider all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT udp -- * * 10.16.0.102 0.0.0.0/0 udp spt:67 dpt:68 0 0 ACCEPT all -- * * 10.16.0.0/24 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 1:65535 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 1:65535 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 code 8 0 0 nova-compute-sg-fallback all -- * * 0.0.0.0/0 0.0.0.0/0 Chain nova-compute-inst-17 (1 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 nova-compute-provider all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT udp -- * * 10.16.0.102 0.0.0.0/0 udp spt:67 dpt:68 0 0 ACCEPT all -- * * 10.16.0.0/24 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 1:65535 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 1:65535 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 code 8 0 0 nova-compute-sg-fallback all -- * * 0.0.0.0/0 0.0.0.0/0 Chain nova-compute-local (1 references) pkts bytes target prot opt in out source destination 0 0 nova-compute-inst-15 all -- * * 0.0.0.0/0 10.16.0.111 0 0 nova-compute-inst-17 all -- * * 0.0.0.0/0 10.16.0.131 Chain nova-compute-provider (2 references) pkts bytes target prot opt in out source destination Chain nova-compute-sg-fallback (2 references) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 Chain nova-filter-top (2 references) pkts bytes target prot opt in out source destination 2106K 284M nova-compute-local all -- * * 0.0.0.0/0 0.0.0.0/0 |
分析一下這些openstack自動生成的規則,能夠看到input,forword和output鏈默認都是accept狀態。分析每條鏈對數據包的跳轉和過濾,若是在虛擬機中配置新的地址,是不會被過濾的。
通過一番折騰,最終發現限制IP的緣由是ebtables在起做用
root@node1:~# ebtables -t nat -L Bridge table: nat Bridge chain: PREROUTING, entries: 2, policy: ACCEPT -i tap0678bf1d-41 -j libvirt-I-tap0678bf1d-41 -i tap496fa038-9e -j libvirt-I-tap496fa038-9e Bridge chain: OUTPUT, entries: 0, policy: ACCEPT Bridge chain: POSTROUTING, entries: 0, policy: ACCEPT Bridge chain: libvirt-I-tap0678bf1d-41, entries: 4, policy: ACCEPT -j I-tap0678bf1d-41-mac -p IPv4 -j I-tap0678bf1d-41-ipv4-ip -p ARP -j I-tap0678bf1d-41-arp-mac -p ARP -j I-tap0678bf1d-41-arp-ip Bridge chain: I-tap0678bf1d-41-mac, entries: 2, policy: ACCEPT -s fa:16:3e:a6:5f:70 -j RETURN -j DROP Bridge chain: I-tap0678bf1d-41-ipv4-ip, entries: 3, policy: ACCEPT -p IPv4 --ip-src 0.0.0.0 --ip-proto udp -j RETURN -p IPv4 --ip-src 10.16.0.131 -j RETURN -j DROP Bridge chain: I-tap0678bf1d-41-arp-mac, entries: 2, policy: ACCEPT -p ARP --arp-mac-src fa:16:3e:a6:5f:70 -j RETURN -j DROP Bridge chain: I-tap0678bf1d-41-arp-ip, entries: 2, policy: ACCEPT -p ARP --arp-ip-src 10.16.0.131 -j RETURN -j DROP Bridge chain: libvirt-I-tap496fa038-9e, entries: 4, policy: ACCEPT -j I-tap496fa038-9e-mac -p IPv4 -j I-tap496fa038-9e-ipv4-ip -p ARP -j I-tap496fa038-9e-arp-mac -p ARP -j I-tap496fa038-9e-arp-ip Bridge chain: I-tap496fa038-9e-mac, entries: 2, policy: ACCEPT -s fa:16:3e:58:1:ac -j RETURN -j DROP Bridge chain: I-tap496fa038-9e-ipv4-ip, entries: 3, policy: ACCEPT -p IPv4 --ip-src 0.0.0.0 --ip-proto udp -j RETURN -p IPv4 --ip-src 10.16.0.111 -j RETURN -j DROP Bridge chain: I-tap496fa038-9e-arp-mac, entries: 2, policy: ACCEPT -p ARP --arp-mac-src fa:16:3e:58:1:ac -j RETURN -j DROP Bridge chain: I-tap496fa038-9e-arp-ip, entries: 2, policy: ACCEPT -p ARP --arp-ip-src 10.16.0.111 -j RETURN -j DROP |
ebtables是linux專門作二層數據鏈路層過濾的。
在經過nova建立虛擬機後,會生成libvirt的一個xml配置文件
路徑在:/etc/libvirt/nwfilter/nova-base.xml
裏面定義瞭如下規則,這些規則限制了在虛擬機上的地址,在二層上就作了過濾
<filter name='nova-base' chain='root'> <uuid>12ec8693-253a-7db0-7cd3-f8cc0a1e1b02</uuid> <filterref filter='no-mac-spoofing'/> <filterref filter='no-ip-spoofing'/> <filterref filter='no-arp-spoofing'/> <filterref filter='allow-dhcp-server'/> </filter> |
而後爲每一個虛擬機建立一個xml文件,每一個虛擬機的xml配置中包含了nova-base.xml中的配置
打開其中一個虛擬機的xml配置,能夠看到,這個配置文件中只放行了指定IP在二層上能夠經過,因此其它手動配置的地址是不可用的。
cat /etc/libvirt/nwfilter/nova-instance-instance-0000000f-fa163e5801ac.xml
<filter name='nova-instance-instance-0000000f-fa163e5801ac' chain='root'> <uuid>972d18be-2db0-4bf2-2853-a0a61beac036</uuid> <filterref filter='nova-base'> <parameter name='DHCPSERVER' value='10.16.0.102'/> <parameter name='IP' value='10.16.0.111'/> <parameter name='PROJMASK' value='255.255.255.0'/> <parameter name='PROJNET' value='10.16.0.0'/> </filterref> </filter> |
libvirt能夠經過在這些xml配置的規則,去生成ebtables規則,最終是ebtables作出限制。
如何破解?
修改nova-base.xml文件
註釋掉如下三行
<filterref filter='no-mac-spoofing'/>
<filterref filter='no-ip-spoofing'/>
<filterref filter='no-arp-spoofing'/>
而後重啓libvirt進程,libvirt會從新讀取xml中的配置,生成新的ebtables規則。
修改後,我經過新建虛擬機,重啓nova-computer進程,或者直接重啓宿主機,這個base文件都不會發生變化了。
還有就是修改nova源碼(未測試)
源碼位置在
/usr/lib/python2.7/dist-packages/nova/virt/libvirt/firewall.py
第198行(G版本中)
去掉no-mac-spoofing,no-ip-spoofing,no-arp-spoofing這三行,之後生成nova-base.xml文件就能夠不包含這3個選項了。