openstack学习之neutron_dhcp_agent

来源:互联网 发布:java多线程数据传递 编辑:程序博客网 时间:2024/05/28 19:23

dhcp agent用来给一个网络提供DHCP服务。网络的IP地址可以再创建subnet时指定。
dhcp agent启动命令dhcp_agent.py,启动是提供neutron.conf和dhcp_agent.ini配置文件。

重要的配置项
interface_driver ,dhcp agent使用interface_driver来创建tap设备。
dhcp_driver,实现dhcp服务的代码,默认配置是neutron.agent.linux.dhcp.Dnsmasq。dhcp agent默认使用dnsmasq来实现dhcp服务。
dhcp_agent_manager, RPC服务的manager,默认是neutron.agent.dhcp_agent.DhcpAgentWithStateReport

dhcp agent功能
主要有三个任务:
1)报告状态。
2)处理RPC API。
3)启动dhcp服务

1)报告状态
            self.heartbeat = loopingcall.FixedIntervalLoopingCall(
                self._report_state)
            self.heartbeat.start(interval=report_interval)

2)处理RPC API
RPC API处理代码在DhcpAgent中。dhcp agent提供了哪些RPC API可以查看它提供的client来查看。
client代码位于dhcp_rpc_agent_api.py 
DhcpAgentNotifyAPI
"""API for plugin to notify DHCP agent."""
主要有:
    VALID_METHOD_NAMES = ['network.create.end',
                          'network.update.end',
                          'network.delete.end',
                          'subnet.create.end',
                          'subnet.update.end',
                          'subnet.delete.end',
                          'port.create.end',
                          'port.update.end',
                          'port.delete.end']
dnsmasq如何分配IP地址?
通过2个参数文件来分配:
--addn-hosts
-dhcp-hostsfile

dhcp agent会生成这两个文件。其中一个根据mac地址给虚拟分配IP,另一个根据hosname分配。具体可参考dnsmasq使用。

这两个文件如何更新?如有新的虚拟机创建时
dhcp agent会调用dhcp_driver.reload_allocations来更新配置文件。reload_allocations会根据新网络配置重新生成配置文件,具体参看代码。
dhcp agent会在收到如下4种消息时调用reload_allocations
port_update_end
port_delete_end
subnet_update_end
subnet_delete_end

在收到network_create_end后,dhcp agent会在启动一个dhcp(dnsmasq)进程服务这个新网络。dhcp agent则由dhcp agent schduler选择。见后文。

上述消息何时发出?
通知消息在neturon rest API的前端实现中发出:neutron.api.v2.base.py
Controller
notifier_method = self._resource + '.create.end'
notifier_method = self._resource + '.delete.end'
notifier_method = self._resource + '.update.end'
可以看到针对每种resource(network, subnet, port),都会有相应的消息发出。dhcp agent根据这些消息来决定何时重新加载dnsmasp配置文件。

还有用来实现dhcp agent scheduler的API
network_removed_from_agent
network_added_to_agent
agent_updated

针对dhcp agent scheduler有如操作命令(在python-neutronclient中):
 neutron dhcp-agent-list-hosting-net List DHCP agents hosting a network.
 neutron  dhcp-agent-network-add Add a network to a DHCP agent.
 neutron  dhcp-agent-network-remove Remove a network from a DHCP agent.

dhcp agent scheduler
通过dhcp agent scheduler,系统中可以部署多个dhcp agent:
1)增加可用性(HA, high availability),避免单点失败(SOF, single point of failure)
2)增加性能,多个dhcp agent可以安装在多台机器上,同时服务。

如何调度dhcp agent
调度算法通过network_scheduler_driver配置,默认是neutron.scheduler.dhcp_agent_scheduler.ChanceScheduler
class ChanceScheduler(object):
    """Allocate a DHCP agent for a network in a random way.
    More sophisticated scheduler (similar to filter scheduler in nova?)
    can be introduced later.
    """
可以看出,这个实现采用了随机分配算法。

如何调用dhcp agent scheduler
通过neutron.api.rpc.agentnotifiers.dhcp_rpc_agent_api.py中
def notify(self, context, data, method_name): 发送消息时,会调用
self._notify_agents,有代码
            schedule_required = method == 'port_create_end'
            if schedule_required:
                agents = self._schedule_network(admin_ctx, network, agents)

可以看到当系统中有新的port创建后,会调用dhcp agent scheduler分配dhcp agent。


3)启动dhcp服务
代码逻辑在def run(self)中
    def after_start(self):
        self.run()
        LOG.info(_("DHCP agent started"))
after_start在manager中被调用。
    def run(self):
        """Activate the DHCP agent."""
        self.sync_state()
        self.periodic_resync()

sync_state()
根据系统中的网络配置,启动dhcp服务器进程(dnsmasq)。sync_state()在dhcp agent启动后运行一次。

periodic_resync
周期执行。
_periodic_resync_helper:高函数周期性调用sync_state()
dhcp agent会缓存一些网路的信息,通过该任务和neutron同步网络信息,更新本地缓存。


其他:
创建port, create_port
系统会分配一个MAC和IP地址给这个port(也可以由用户选定IP地址)
代码位于neutron.neutron.db.db_base_plugin_v2.py
mac_address = NeutronDbPluginV2._generate_mac
ips = self._allocate_ips_for_port(context, network, port)

neutron的各种插件实现(core plugin or service plugin)都继承于db包下的对象,这些object实现了对相应资源数据模型的操作。



0 0
原创粉丝点击