更新历史:2013.06.06增加自己对router和l3agent关系的理解,因为不像network和dhcpagent关系那样简单版本:Grizzlymaster分支2013.06.05约定:Rest消息的HTTP头:Accept:application/jsonContent-Type:application/j
更新历史:
2013.06.06 增加自己对router和l3 agent关系的理解,因为不像network和dhcp
agent关系那样简单
版本:Grizzly master分支 2013.06.05
约定:Rest消息的HTTP头:
Accept:application/json
Content-Type:application/json
X-Auth-Token:XXXXXX
继上次讲了quantum中的agent部署后(http://blog.csdn.net/lynn_kong/article/details/8709003),这次来深入代码看agent到底有哪些
操作,以及各个
操作的具体实现步骤,文章中涉及到的一些知识可以在我之前的博客中找到。
1. get agent GET /agents/{agent_id}
功能:获取agent详细信息
示例1:
Request:
GET http://192.168.82.231:9696/v2.0/agents/ce775b48-5525-4d67-97eb-16e588da3412
Response:
{
"agent": {
"binary": "quantum-dhcp-agent",
"description": null,
"admin_state_up": true,
"heartbeat_timestamp": "2013-06-05 11:53:55",
"alive": true,
"id": "ce775b48-5525-4d67-97eb-16e588da3412",
"topic": "dhcp_agent",
"host": "network232.openstack.org",
"agent_type": "DHCP agent",
"started_at": "2013-06-03 03:30:07",
"created_at": "2013-05-31 10:26:22",
"configurations": {
"subnets": 1,
"use_namespaces": false,
"dhcp_driver": "quantum.agent.linux.dhcp.Dnsmasq",
"networks": 1,
"dhcp_lease_time": 120,
"ports": 2
}
}
}
示例2:
Request:
GET http://192.168.82.231:9696/v2.0/agents/81e68b02-ffe8-4278-a52c-61bcd9a6f43e/l3-routers
Response:
{
"agent": {
"binary": "quantum-l3-agent",
"description": null,
"admin_state_up": true,
"heartbeat_timestamp": "2013-06-06 02:21:20",
"alive": true,
"id": "81e68b02-ffe8-4278-a52c-61bcd9a6f43e",
"topic": "l3_agent",
"host": "network232.openstack.org",
"agent_type": "L3 agent",
"started_at": "2013-06-06 02:16:36",
"created_at": "2013-05-31 10:26:22",
"configurations": {
"router_id": "86ca29ce-06fe-4f4b-8b19-a7e2f4959203",
"gateway_external_network_id": "",
"handle_internal_only_routers": true,
"use_namespaces": false,
"routers": 1,
"interfaces": 0,
"floating_ips": 0,
"interface_driver": "quantum.agent.linux.interface.OVSInterfaceDriver",
"ex_gw_ports": 1
}
}
}
2. create agent 目前不允许通过API创建agent
3. update agent PUT
/agents/{agent_id}
1. 更新DB中的agent的属性
2. 如果修改了admin_state_up,需要向对应的agent(DHCP/L3)发送消息
3. agent的处理
a. DHCP:修改标志位needs_resync,等待下一次的循环任务
b. L3: 同dhcp
agent一样,修改标志位fullsync,等待循环任务处理
其实这里可以看到,admin_state_up标识,会影响后续的到该agent调度,但不会影响agent目前的工作,即:admin_state_up只是一个是否可操作状态,而不是表示功能是否可用。
示例:
Request:
PUT http://192.168.82.231:9696/v2.0/agents/ce775b48-5525-4d67-97eb-16e588da3412
{
"agent": {
"admin_state_up": false,
"description":"added by kong"
}
}
Response:
{
"agent": {
"binary": "quantum-dhcp-agent",
"description": "added by kong",
"admin_state_up": false,
"heartbeat_timestamp": "2013-06-05 12:08:15",
"alive": true,
"id": "ce775b48-5525-4d67-97eb-16e588da3412",
"topic": "dhcp_agent",
"host": "network232.openstack.org",
"agent_type": "DHCP agent",
"started_at": "2013-06-03 03:30:07",
"created_at": "2013-05-31 10:26:22",
"configurations": {
"subnets": 1,
"use_namespaces": false,
"dhcp_driver": "quantum.agent.linux.dhcp.Dnsmasq",
"networks": 1,
"dhcp_lease_time": 120,
"ports": 2
}
}
}
4. delete_agent DELETE /agents/{agent_id}
功能:删除DB记录
5. add_network_to_dhcp_agent POST
/agents/{agent_id}/dhcp-networks
功能:把network关联到agent。当创建新的network时,系统不会自动选择dhcp
agent进行关联,而是留给管理员手动进行关联;只有当在这个network内创建port时,才会自动选择dhcp
agent进行关联。为了实现冗余以及agent的HA,一个network可以同时关联最多dhcp_agents_per_network(配置项)个agent。
步骤:
1. 校验。agent类型是否是DHCP
agent,admin_state_up是否是为True,抛异常InvalidDHCPAgent
2.
校验。从NetworkDhcpAgentBinding表中获取要添加的network所对应的agent,如果有的话,抛异常NetworkHostedByDHCPAgent
3. 增加NetworkDhcpAgentBinding表记录
4. 向dhcp agent发送消息network_create_end
5. agent的处理:
对于dhcp
agent来说,新关联一个network,就如同新创建一个network的处理。如果该network的admin_state_up为True且network内有subnet(enable_dhcp=True),则创建一个对应的dnsmasq进程。
示例:
Request:
POST http://192.168.82.231:9696/v2.0/agents/ce775b48-5525-4d67-97eb-16e588da3412/dhcp-networks
{
"network_id": "a6e8a041-fce8-4a95-bb90-b8f71454c294"
}
Response:
null(201 created)
6. remove_network_from_dhcp_agent
DELETE
/agents/{agentid}/dhcp-networks/{network_id}
功能:将一个network与dhcp agent解关联
步骤:
1.
校验。NetworkDhcpAgentBinding表中有无对应记录,抛异常NetworkNotHostedByDhcpAgent
2. 向dhcp agent发送消息network_delete_end
3. agent的处理
删除一个network,就要将之前与该network对应的dnsmasq进程kill掉。
示例:
Request:
DELETE http://192.168.82.231:9696/v2.0/agents/ce775b48-5525-4d67-97eb-16e588da3412/dhcp-networks/a6e8a041-fce8-4a95-bb90-b8f71454c294
Response:
204 No Content
7. list_networks_on_dhcp_agent
GET
/agents/{agentid}/dhcp-networks
功能:获取dhcp agent关联的network列表
示例:
Request:
GET http://192.168.82.231:9696/v2.0/agents/ce775b48-5525-4d67-97eb-16e588da3412/dhcp-networks
Response:
{
"networks": [
{
"status": "ACTIVE",
"subnets": [
"ea1170d1-8017-48c8-b98e-e75192588b84"
],
"name": "internalnet",
"provider:physical_network": "physnet1",
"admin_state_up": true,
"tenant_id": "fc921d0878204b7aa0751ee777f35f72",
"provider:network_type": "vlan",
"router:external": false,
"shared": false,
"id": "d8c065a6-e39e-498b-b74f-ec8db14f6291",
"provider:segmentation_id": 52
}
]
}
8. list_dhcp_agents_hosting_network
GET
/networks/{network_id}/dhcp-agents
功能:获取network关联的dhcp agent列表
示例:
Request:
GET http://192.168.82.231:9696/v2.0/networks/d8c065a6-e39e-498b-b74f-ec8db14f6291/dhcp-agents
Response:
{
"agents": [
{
"binary": "quantum-dhcp-agent",
"description": "added by kong",
"admin_state_up": false,
"heartbeat_timestamp": "2013-06-05 12:12:39",
"alive": true,
"topic": "dhcp_agent",
"host": "network232.openstack.org",
"agent_type": "DHCP agent",
"created_at": "2013-05-31 10:26:22",
"started_at": "2013-06-03 03:30:07",
"id": "ce775b48-5525-4d67-97eb-16e588da3412",
"configurations": {
"subnets": 0,
"use_namespaces": false,
"dhcp_driver": "quantum.agent.linux.dhcp.Dnsmasq",
"networks": 0,
"dhcp_lease_time": 120,
"ports": 0
}
}
]
}
9. add_router_to_l3_agent
POST /agents/{agent_id}/l3-routers
功能:将router与l3 agent关联。如果l3 agent的use_namespace=False,则一个l3
agent只能关联一个router,要在配置文件中指定,那么对这个agent也不能再调用该接口关联其他的router;而如果use_namespace=True,意味着一个l3
agent可以同时关联多个router。如果新创建了一个router,而没有调用add_router_to_l3_agent接口,那么这个router默认情况下不会被任何agent关联,当改变某一个agent的admin_state_up状态时,这个agent就会扫描目前有哪些router没有被关联,然后会说:“好,没人理你,我理你”,于是将其关联。比如后续又部署了一个l3
agent,想让router跟这个新创建的agent关联,那么可以先remove_router_from_l3_agent,然后再add_router_to_l3_agent
步骤:
1. 校验。agent类型是否是L3
agent,admin_state_up是否是为True,抛异常InvalidDHCPAgent
2. 校验。查询agent信息,根据agent四个属性(router_id/ use_namespaces/
handle_internal_only_routers/
gateway_external_network_id)判断agent能否关联router,抛异常InvalidL3Agent
3. 校验。RouterL3AgentBinding表中是否有对应的记录,抛异常RouterHostedByL3Agent
4. 依赖于配置项:router_scheduler_driver=
quantum.scheduler.l3_agent_scheduler.ChanceScheduler,目前也只有这一种可以使用。但在记录关联关系时有一点需要注意,关联时,在agent所在的主机上随机选取一个l3
agent,而并不是直接关联到该l3 agent。在RouterL3AgentBinding表增加记录。
5. 向agent所在host发送消息router_added_to_agent
6. agent的处理:
l3
agent调用_process_routers进行处理。
示例:
Request:
POST http://192.168.82.231:9696/v2.0/agents/81e68b02-ffe8-4278-a52c-61bcd9a6f43e/l3-routers
{
"router_id": "40be88ef-c258-488c-a49c-609cef442b76"
}
Response:
null(201 Created)
10. remove_router_from_l3_agent
DELETE
/agents/{agent_id}/l3-routers/{router_id}
功能:将router和agent解关联
步骤:
1. 校验。RouterL3AgentBinding表中有无记录,抛异常RouterNotHostedByL3Agent
2. 删除DB记录。向agent发送消息router_removed_from_agent
3. agent的处理
l3
agent调用_router_removed进行处理。
示例:
Request:
DELETE http://192.168.82.231:9696/v2.0/agents/81e68b02-ffe8-4278-a52c-61bcd9a6f43e/l3-routers/86ca29ce-06fe-4f4b-8b19-a7e2f4959203
Response:
204 No Content
11. list_routers_on_l3_agent
GET
/agents/{agent_id}/l3-routers
功能:获取l3 agent关联的router列表
12. list_l3_agents_hosting_router
GET /routers/{router_id}/l3-agents
功能:获取router关联的l3 agent列表