【OpenStack】Quantum(Grizzly) L3 agent(OVS)工作流

Quantum(Grizzly) L3 agent(OVS)工作流

本博客欢迎转发,但请保留原作者信息
新浪微博:@孔令贤HW;
QQ:363210168
博客地址:http://blog.csdn.net/lynn_kong
内容系本人学习、研究和总结,如有雷同,实属荣幸! 

更新日志:
2013.4.10  添加系统逻辑视图
2013.4.11  增加iptables filter和nat表的图示

今天按照longgeek兄弟的Grizzly单节点安装文档,总算了把G版搭建起来,成功执行了创建虚拟机、关联外网IP、ping外网IP、SSH登录虚拟机等关键操作。在F版就一直对Quantum感兴趣,其实在Quantum上层丰富的网络逻辑功能下面,有着另一番景象。今天我就拨开表象,扒开衣服,先来看看Quantum中的l3 agent的裸体。
注:我这里用的是OVS plugin

我的系统中逻辑视图如下:
【OpenStack】Quantum(Grizzly) L3 agent(OVS)工作流_第1张图片


我的环境中建模如下:
【OpenStack】Quantum(Grizzly) L3 agent(OVS)工作流_第2张图片

我使用的Quantum命令:

EXTERNAL_NET_ID=$(quantum net-create external_net1 --router:external=True | awk'/ id / {print $4}')
SUBNET_ID=$(quantum subnet-create external_net1 182.168.61.0/24 --name=external_subnet1 --allocation-pool start=182.168.61.249,end=182.168.61.253 --gateway_ip 182.168.61.1 --enable_dhcp=False | awk '/ id / {print $4}')
INTERNAL_NET_ID=$(quantum net-create demo_net1 | awk '/ id / {print $4}')
DEMO_SUBNET_ID=$(quantum subnet-create demo_net1 10.1.1.0/24 --name=demo_subnet1 --gateway_ip 10.1.1.1 | awk '/ id / {print $4}')
DEMO_ROUTER_ID=$(quantum router-create demo_router1 | awk '/ id / {print $4}')
quantum router-interface-add $DEMO_ROUTER_ID $DEMO_SUBNET_ID
quantum router-gateway-set $DEMO_ROUTER_ID $EXTERNAL_NET_ID
DEMO_PORT_ID=$(quantum port-create --fixed-ip subnet_id=$DEMO_SUBNET_ID,ip_address=10.1.1.13 demo_net1 | awk '/ id / {print $4}')
nova keypair-add mykey > mykey.pem
nova boot myvm --image <image_id> --flavor 2 --key_name mykey --nic port-id=$DEMO_PORT_ID
quantum floatingip-create external_net1
quantum floatingip-associate <floatingip_id> $DEMO_PORT_ID


一、初始化

l3 agent初始化主要是清除所有与router相关的设备

获取namespace名称:
root@openstack:/etc/init.d# ip netns list
qdhcp-c4d8b48b-6ff7-43b6-a203-8f1192a16f07
qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967

获取以qrouter开头的namespace内的设备:
root@openstack:/etc/init.d# ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -o link list
14: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN\    link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
28: qr-aff7e122-3b: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue state UNKNOWN \    link/etherfa:16:3e:50:1d:0d brd ff:ff:ff:ff:ff:ff
29: qg-282f4d8c-81: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdiscnoqueue state UNKNOWN \    link/etherfa:16:3e:d7:9f:87 brd ff:ff:ff:ff:ff:ff

确定br-int存在(因为qr-XXX端口是在br-int上,所以需要操作br-int删除端口):
root@openstack:/etc/init.d# ip -o link show br-int
10: br-int: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN \    link/ether 86:7e:c0:44:ff:42 brdff:ff:ff:ff:ff:ff

删除qr-XXX设备:
ovs-vsctl --timeout=2 -- --if-exists del-port br-int qr-aff7e122-3b

同样的,确定br-ex存在(因为qg-XXX端口是在br-ex上,所以需要操作br-ex删除端口):
root@openstack:/etc/init.d# ip -o link show br-ex
11: br-ex: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue stateUNKNOWN \    link/ether 28:6e:d4:f0:c5:4bbrd ff:ff:ff:ff:ff:ff

删除qg-XXX设备:
ovs-vsctl --timeout=2 -- --if-exists del-port br-ex qg-282f4d8c-81

至此,初始化时与设备相关的操作结束。


二、循环任务

在l3 agent的循环任务中,不断的向Quantum Plugin查询router信息,与上次维护的信息对比,对于新增的router,在br上新增设备(同时需要设置设备的属性,iptables规则等),而对于删除的router,即删除设备(同时删除对应的iptables规则)。下面的命令和输出都是以第一次启动为例

l3 agent使用时有一些细节需要注意,如果不启动namespace(不能使用IP重叠),一个l3 agent只能处理一个router,此时需要在创建external_net和router后,将router和external_net的ID写进配置文件的router_id选项和gateway_external_network_id选项;而如果启用namespace,则不需要对这两项进行配置。


2.1. 内部port处理

对连接到router的每个内部port,(执行router-interface-add后,quantum会创建一个对应到subnet网关的一个port(device_owner='network:router_interface')),此时生成一个设备名称qr-XXX,后面是port_id,先查看该设备是否已经存在:
root@openstack:/etc/init.d# ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -o link show qr-aff7e122-3b
Device "qr-aff7e122-3b" does not exist.

在br-int上新增端口,同时在OVS的db中设置端口的一些属性:
ovs-vsctl -- --may-exist add-port br-int qr-aff7e122-3b -- set Interface qr-aff7e122-3b type=internal -- set Interface qr-aff7e122-3b external-ids:iface-id=aff7e122-3b0c-4c7b-8e22-dbe63a84dfd6 -- set Interface qr-aff7e122-3b external-ids:iface-status=active -- set Interface qr-aff7e122-3b external-ids:attached-mac=fa:16:3e:50:1d:0d

设置端口的MAC地址:
ip link set qr-aff7e122-3b address fa:16:3e:50:1d:0d

将端口加入namespace:
ip link set qr-aff7e122-3b netns qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967

启动端口:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip link set qr-aff7e122-3b up

设置端口的IP地址
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip addr show qr-aff7e122-3b permanent scope global
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -4 addr add 10.1.1.1/24brd 10.1.1.255 scope global dev qr-aff7e122-3b
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 arping -A -U -I qr-aff7e122-3b -c 3 10.1.1.1

设置iptables规则,具体的iptables参见下面的输出


2.2. 外部port处理

同内部port一样,(在进行router-gateway-set后,quantum会创建一个port(device_owner='network:router_gateway')),此时生成一个设备名称qg-XXX,后面是port-id,先查看外部port是否已经存在:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -o link show qg-282f4d8c-81

在br-ex上新增端口,同时在OVS的db中设置端口的一些属性
ovs-vsctl -- --may-exist add-port br-ex qg-282f4d8c-81 -- set Interface qg-282f4d8c-81type=internal -- set Interface qg-282f4d8c-81 external-ids:iface-id=282f4d8c-8123-4260-ada2-4c7dd2dbe824 -- set Interface qg-282f4d8c-81 external-ids:iface-status=active -- set Interface qg-282f4d8c-81 external-ids:attached-mac=fa:16:3e:d7:9f:87

设置端口的MAC地址:
ip link set qg-282f4d8c-81 address fa:16:3e:d7:9f:87

将端口加入namespace:
ip link set qg-282f4d8c-81 netns qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967

启动端口:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip link set qg-282f4d8c-81 up

设置router的外网IP:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip addr show qg-282f4d8c-81 permanent scope global
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -4 addr add 182.168.61.249/24 brd 182.168.61.255 scope global dev qg-282f4d8c-81
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 arping -A -U -I qg-282f4d8c-81 -c 3 182.168.61.249

为系统增加一条默认路由,182.168.61.1是外网IP段的网关:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 route add default gw 182.168.61.1

设置iptables,具体的iptables参见下面的输出


2.3. 处理floatingip

我的系统中有两个虚拟机(上面的图中只画了一个),内网在IP分别为:10.1.1.11和10.1.1.13,分配了两个floatingip:182.168.61.250和182.168.61.251,分别关联两个虚拟机。

为qg-282f4d8c-81添加一个公网地址:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -4 addr add 182.168.61.251/32 brd 182.168.61.251 scope global dev qg-282f4d8c-81
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 arping -A -U -I qg-282f4d8c-81 -c 3 182.168.61.251

为qg-282f4d8c-81添加第二个公网地址:
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 ip -4 addr add 182.168.61.250/32 brd 182.168.61.250 scope global dev qg-282f4d8c-81
ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 arping -A -U -I qg-282f4d8c-81 -c 3 182.168.61.250


2.4. iptable规则

以下是第一次循环任务执行后的iptalbes规则,懂iptables的朋友应该一眼就能知道l3 agent都做了什么,在此我就不班门弄斧了。
root@openstack:/etc/init.d# ip netns exec qrouter-afabf77d-ffe4-4ab2-a635-8ad33d58f967 iptables-save
# Generated by iptables-save v1.4.12 on Tue Apr 9 18:19:59 2013
*nat
:PREROUTING ACCEPT [93:28644]
:INPUT ACCEPT [93:28644]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:quantum-l3-agent-OUTPUT - [0:0]
:quantum-l3-agent-POSTROUTING - [0:0]
:quantum-l3-agent-PREROUTING - [0:0]
:quantum-l3-agent-float-snat - [0:0]
:quantum-l3-agent-snat - [0:0]
:quantum-postrouting-bottom - [0:0]
-A PREROUTING -j quantum-l3-agent-PREROUTING
-A OUTPUT -j quantum-l3-agent-OUTPUT
-A POSTROUTING -j quantum-l3-agent-POSTROUTING
-A POSTROUTING -j quantum-postrouting-bottom
-A quantum-l3-agent-OUTPUT -d 182.168.61.251/32 -j DNAT --to-destination10.1.1.13
-A quantum-l3-agent-OUTPUT -d 182.168.61.250/32 -j DNAT --to-destination10.1.1.11
-A quantum-l3-agent-POSTROUTING ! -i qg-282f4d8c-81 ! -o qg-282f4d8c-81 -mconntrack ! --ctstate DNAT -j ACCEPT
-A quantum-l3-agent-PREROUTING -d 169.254.169.254/32 -p tcp -m tcp --dport 80-j REDIRECT --to-ports 9697
-A quantum-l3-agent-PREROUTING -d 182.168.61.251/32 -j DNAT --to-destination10.1.1.13
-A quantum-l3-agent-PREROUTING -d 182.168.61.250/32 -j DNAT --to-destination10.1.1.11
-A quantum-l3-agent-float-snat -s 10.1.1.13/32 -j SNAT --to-source182.168.61.251
-A quantum-l3-agent-float-snat -s 10.1.1.11/32 -j SNAT --to-source182.168.61.250
-A quantum-l3-agent-snat -j quantum-l3-agent-float-snat
-A quantum-l3-agent-snat -s 10.1.1.0/24 -j SNAT --to-source 182.168.61.249
-A quantum-postrouting-bottom -j quantum-l3-agent-snat
COMMIT
# Completed on Tue Apr  9 18:19:59 2013
# Generated by iptables-save v1.4.12 on Tue Apr 9 18:19:59 2013
*filter
:INPUT ACCEPT [100:30818]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:quantum-filter-top - [0:0]
:quantum-l3-agent-FORWARD - [0:0]
:quantum-l3-agent-INPUT - [0:0]
:quantum-l3-agent-OUTPUT - [0:0]
:quantum-l3-agent-local - [0:0]
-A INPUT -j quantum-l3-agent-INPUT
-A FORWARD -j quantum-filter-top
-A FORWARD -j quantum-l3-agent-FORWARD
-A OUTPUT -j quantum-filter-top
-A OUTPUT -j quantum-l3-agent-OUTPUT
-A quantum-filter-top -j quantum-l3-agent-local
-A quantum-l3-agent-INPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 9697 -j ACCEPT
COMMIT
# Completed on Tue Apr  9 18:19:59 2013

下面图示更能清晰说明系统中的iptables filter表和nat表:
【OpenStack】Quantum(Grizzly) L3 agent(OVS)工作流_第3张图片

【OpenStack】Quantum(Grizzly) L3 agent(OVS)工作流_第4张图片

你可能感兴趣的:(openstack,agent,quantum,Grizzly)