openstack nova源码分析--compute创建虚拟机(1)

博客作为学习笔记记录,若有理解或表述错误,欢迎指出

在OpenStack nova创建虚拟机过程中,nova-api收到虚拟机创建请求,然后nova-scheduler完成选择合适计算节点的任务,nova-conductor则开始调用build_instance()来创建虚机。

在conductor.manager.ComputeTaskManager.build_instance()中,通过rpc调用:self.compute_rpcapi.build_and_run_instance(),来调用compute节点上的build_and_run_instance()方法。而build_and_run_instance()方法就完成VM在计算节点上的创建和启动任务。

下面我们来看看Nova compute上的build_and_run_instance()的具体实现。

def build_and_run_instance(self, context, instance, image, request_spec,
                     filter_properties, admin_password=None,
                     injected_files=None, requested_networks=None,
                     security_groups=None, block_device_mapping=None,
                     node=None, limits=None, host_list=None):

        @utils.synchronized(instance.uuid)
        def _locked_do_build_and_run_instance(*args, **kwargs):
            # NOTE(danms): We grab the semaphore with the instance uuid
            # locked because we could wait in line to build this instance
            # for a while and we want to make sure that nothing else tries
            # to do anything with this instance while we wait.
            with self._build_semaphore:
                try:
                    result = self._do_build_and_run_instance(*args, **kwargs)
                except Exception:
                    # NOTE(mriedem): This should really only happen if
                    # _decode_files in _do_build_and_run_instance fails, and
                    # that's before a guest is spawned so it's OK to remove
                    # allocations for the instance for this node from Placement
                    # below as there is no guest consuming resources anyway.
                    # The _decode_files case could be handled more specifically
                    # but that's left for another day.
                    result = build_results.FAILED
                    raise
                finally:
                    if result == build_results.FAILED:
                        # Remove the allocation records from Placement for the
                        # instance if the build failed. The instance.host is
                        # likely set to None in _do_build_and_run_instance
                        # which means if the user deletes the instance, it
                        # will be deleted in the API, not the compute service.
                        # Setting the instance.host to None in
                        # _do_build_and_run_instance means that the
        
<think>嗯,用户这次问的是在OpenStack创建nova计算服务的具体操作方法和常见问题排查。从用户提供的多个引用来看,ta应该正在部署OpenStack环境,可能遇到了服务创建失败或验证不通过的情况。用户引用的资料很有针对性,其中引用[1][2][3]都提到了完全相同的命令行操作,这说明ta已经掌握了基础命令,但不确定操作细节或遇到了报错。引用[4][5]则显示用户可能还需要后续的启动配置和架构理解。特别注意到用户提到"排查该命令的相关问题",这暗示实际执行时可能出现了意外状况。从技术角度看,创建计算服务的关键点有几个:首先是必须预先完成keystone的认证配置,这是引用[2]提到的先决条件;其次要注意命令行中"compute"这个服务类型名称是OpenStack的固定参数;最后创建后的端点注册容易被忽略。用户引用的资料里[1]恰好演示了完整的端点创建流程,这个细节很关键。用户可能隐藏的痛点是:当命令输出没有返回类似引用[3]的标准字段(如缺少ID值)时,如何判断服务是否创建成功?或者遇到认证失败的错误提示该如何处理?考虑到OpenStack部署的高失败率,应该重点强调环境检查和权限验证。值得补充但引用中未明确的是:1)如何通过openstackservicelist验证服务状态2)服务创建后的nova组件启动顺序3)可能出现的主机名解析问题。特别是引用[4]提到的自启动配置,对生产环境很重要但用户可能还没进行到这一步。最后要提醒用户注意命令输出的细微差异。比如引用[1]和[3]的输出格式略有不同(后者少了description字段),这可能是OpenStack版本差异导致的正常现象,避免用户误以为是错误。</think> 在OpenStack创建名为`nova`的计算服务并排查相关问题,需按以下流程操作: --- ### 一、创建Nova计算服务 #### 1. **前置条件确认** - **Keystone认证就绪**:确保已加载管理员环境变量(如`admin-openrc`)[^2]: ```bash source admin-openrc.sh ``` - **用户与角色**:创建`nova`用户并赋予`admin`角色[^2]: ```bash openstack user create --domain default --password NOVA_PASS nova openstack role add --project service --user nova admin ``` #### 2. **创建计算服务实体** - 执行服务创建命令[^1][^2][^3]: ```bash openstack service create --name nova \ --description "OpenStack Compute" compute ``` - **成功输出示例**: ``` +-------------+----------------------------------+ | Field | Value | +-------------+----------------------------------+ | description | OpenStack Compute | | enabled | True | | id | 83721cda2dd94e8bbfad43e34657a6da | | name | nova | | type | compute | +-------------+----------------------------------+ ``` #### 3. **注册API端点** > 端点需指向Nova-API服务的主机名/IP和端口(默认端口`8774/v2.1`)[^1][^4]。 ```bash # 创建Public端点 openstack endpoint create --region RegionOne \ compute public http://<控制器IP>:8774/v2.1 # 创建Internal端点 openstack endpoint create --region RegionOne \ compute internal http://<控制器IP>:8774/v2.1 # 创建Admin端点 openstack endpoint create --region RegionOne \ compute admin http://<控制器IP>:8774/v2.1 ``` --- ### 二、常见问题排查 #### ❌ 错误1:认证失败(HTTP 401- **原因**:环境变量未加载或密码错误 - **解决**: ```bash # 重新加载认证信息 source admin-openrc.sh # 验证令牌有效性 openstack token issue ``` #### ❌ 错误2:服务类型`compute`不存在(HTTP 400) - **原因**:`compute`是固定服务类型,需确保拼写正确 - **解决**:检查命令末尾是否为**`compute`**(非`nova`): ```bash openstack service create --name nova compute # 正确 openstack service create --name nova nova # 错误 ``` #### ❌ 错误3:端口冲突(HTTP 503) - **原因**:Nova-API服务未启动或端口被占用 - **解决**: ```bash # 启动Nova服务 systemctl start openstack-nova-api # 检查端口监听 netstat -tnlp | grep 8774 ``` #### ❌ 错误4:端点注册失败 - **原因**:URL格式错误或Keystone未同步 - **解决**: - 确认URL包含协议(`http://`)和正确端口 - 重启Keystone服务: ```bash systemctl restart httpd ``` --- ### 三、服务验证 1. **检查服务列表**: ```bash openstack service list | grep nova ``` > 应显示`nova`服务状态为`Enabled`[^3]。 2. **验证端点**: ```bash openstack endpoint list | grep compute ``` > 确认`public`、`internal`、`admin`三类端点均存在。 3. **启动Nova组件**(部署后必做)[^4]: ```bash systemctl enable --now openstack-nova-{api,scheduler,conductor,novncproxy} ``` --- ### 关键架构原理 > Nova依赖其他服务协同工作[^5]: > - **Keystone**:身份认证 > - **Glance**:提供系统镜像 > - **Neutron**:网络管理 > - **Cinder**:块存储 > > ![](https://docs.openstack.org/nova/latest/_images/architecture.svg) > *Nova通过Driver调用Hypervisor(如KVM)实现虚拟机生命周期管理[^5]。* ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值