如何在OpenStack中从命令行创建可用区?
假设OpenStack中一个租户想要在生产环境和非生产环境中部署虚拟机,于是想在Openstack中创建这种类型的架构,我们先要确定哪些计算将被视为生产环境和非生产环境,然后我们要创建主机聚合组(host-aggregate group),我们会将计算添加到主机聚合组,然后我们将该主机聚合组映射到可用区。
我们在本教程中将演示如何通过命令行在Openstack中创建和使用计算可用区。
创建计算可用区
无论何时部署OpenStack,Nova都是自动创建的默认可用区(AZ),所有计算节点都属于Nova AZ。从控制器节点运行以下Openstack命令,列出可用区:
- ~# source openrc
- ~# openstack availability zone list
- +-----------+-------------+
- | Zone Name | Zone Status |
- +-----------+-------------+
- | internal | available |
- | nova | available |
- | nova | available |
- | nova | available |
- +-----------+-------------+
- ~#
想列出计算的可用区,运行以下Openstack命令:
- ~# openstack availability zone list --compute
- +-----------+-------------+
- | Zone Name | Zone Status |
- +-----------+-------------+
- | internal | available |
- | nova | available |
- +-----------+-------------+
- ~#
想列出映射到Nova可用区的所有计算节点,执行以下命令:
- ~# openstack host list | grep -E "Zone|nova"
- | Host Name | Service | Zone |
- | compute-0-1 | compute | nova |
- | compute-0-2 | compute | nova |
- | compute-0-4 | compute | nova |
- | compute-0-3 | compute | nova |
- | compute-0-8 | compute | nova |
- | compute-0-6 | compute | nova |
- | compute-0-9 | compute | nova |
- | compute-0-5 | compute | nova |
- | compute-0-7 | compute | nova |
- ~#
不妨创建两个主机聚合组,名为production和non-production,将计算4、5和6添加到production主机聚合组,将计算7、8和9添加到non-production主机聚合组。
使用以下OpenStack命令,创建Production和Non-Production主机聚合:
- ~# openstack aggregate create production
- +-------------------+----------------------------+
- | Field | Value |
- +-------------------+----------------------------+
- | availability_zone | None |
- | created_at | 2019-08-17T03:02:41.561259 |
- | deleted | False |
- | deleted_at | None |
- | id | 7 |
- | name | production |
- | updated_at | None |
- +-------------------+----------------------------+
- ~# openstack aggregate create non-production
- +-------------------+----------------------------+
- | Field | Value |
- +-------------------+----------------------------+
- | availability_zone | None |
- | created_at | 2019-08-17T03:02:53.806713 |
- | deleted | False |
- | deleted_at | None |
- | id | 10 |
- | name | non-production |
- | updated_at | None |
- +-------------------+----------------------------+
- ~#
现在创建可用区,并将它与各自的主机聚合组关联起来。
语法:
- # openstack aggregate set –zone <az_name> <host_aggregate_name>
- ~# openstack aggregate set --zone production-az production
- ~# openstack aggregate set --zone non-production-az non-production
最后,将计算主机添加到主机聚合群。
语法:
- # openstack aggregate add host
- ~# openstack aggregate add host production compute-0-4
- ~# openstack aggregate add host production compute-0-5
- ~# openstack aggregate add host production compute-0-6
同样,将计算主机添加到non-production主机聚合组:
- ~# openstack aggregate add host non-production compute-0-7
- ~# openstack aggregate add host non-production compute-0-8
- ~# openstack aggregate add host non-production compute-0-9
执行以下Openstack命令,验证主机聚合组及其可用区:
- ~# openstack aggregate list
- +----+----------------+-------------------+
- | ID | Name | Availability Zone |
- +----+----------------+-------------------+
- | 7 | production | production-az |
- | 10 | non-production | non-production-az |
- +----+----------------+-------------------+
- ~#
运行以下命令,列出与AZ和主机聚合组有关的计算:
- ~# openstack aggregate show production
- +-------------------+--------------------------------------------+
- | Field | Value |
- +-------------------+--------------------------------------------+
- | availability_zone | production-az |
- | created_at | 2019-08-17T03:02:42.000000 |
- | deleted | False |
- | deleted_at | None |
- | hosts | [u'compute-0-4', u'compute-0-5', u'compute-0-6'] |
- | id | 7 |
- | name | production |
- | properties | |
- | updated_at | None |
- +-------------------+--------------------------------------------+
- ~# openstack aggregate show non-production
- +-------------------+---------------------------------------------+
- | Field | Value |
- +-------------------+---------------------------------------------+
- | availability_zone | non-production-az |
- | created_at | 2019-08-17T03:02:54.000000 |
- | deleted | False |
- | deleted_at | None |
- | hosts | [u'compute-0-7', u'compute-0-8', u'compute-0-9'] |
- | id | 10 |
- | name | non-production |
- | properties | |
- | updated_at | None |
- +-------------------+---------------------------------------------+
- ~#
上述命令的输出证实:我们已成功地创建了主机聚合组和可用区。
在可用区内启动虚拟机
现在不妨在这两个可用区内创建两个虚拟机;想在某个特定的可用区内创建虚拟机,运行以下命令:
语法:
- # openstack server create –flavor –image –nic net-id= –security-group –key-name –availability-zone
例子如下所示:
- ~# openstack server create --flavor m1.small --image Cirros --nic net-id=37b9ab9a-f198-4db1-a5d6-5789b05bfb4c --security-group f8dda7c3-f7c3-423b-923a-2b21fe0bbf3c --key-name mykey --availability-zone production-az test-vm-prod-az
运行以下命令以验证虚拟机的细节:
- ~# openstack server show test-vm-prod-az
想在可用区下某个特定的计算节点内创建虚拟机,运行以下命令:
语法:
- # openstack server create –flavor –image –nic net-id= –security-group –key-name {Keypair-Name} –availability-zone :
假设我们想在特定计算(compute-0-6)上的生产可用区下启用虚拟机,为了执行这项操作,运行以下命令:
- ~# openstack server create --flavor m1.small --image Cirros --nic net-id=37b9ab9a-f198-4db1-a5d6-5789b05bfb4c --security-group f8dda7c3-f7c3-423b-923a-2b21fe0bbf3c --key-name mykey --availability-zone production-az:compute-0-6 test-vm-prod-az-host
执行以下命令以验证虚拟机的细节:
- ~# openstack server show test-vm-prod-az-host
上述命令的输出会类似如下:
同样,我们可以在非生产可用区内创建虚拟机,例子如下所示:
- ~# openstack server create --flavor m1.small --image Cirros --nic net-id=37b9ab9a-f198-4db1-a5d6-5789b05bfb4c --security-group f8dda7c3-f7c3-423b-923a-2b21fe0bbf3c --key-name mykey --availability-zone non-production-az vm-nonprod-az
使用以下命令以验证虚拟机的细节:
- ~# openstack server show vm-nonprod-az
上述命令的输出会类似如下:
删除主机聚合组和可用区
假设我们想删除上述创建的主机聚合组和可用区;为此,我们先得从主机聚合组删除主机,使用以下命令:
- ~# openstack aggregate show production
上述命令将为我们列出已添加到生产主机聚合组的计算主机。
使用以下命令从主机聚合机删除主机:
语法:
- # openstack aggregate remove host <host-aggregate-name> <compute-name>
- ~# openstack aggregate remove host production compute-0-4
- ~# openstack aggregate remove host production compute-0-5
- ~# openstack aggregate remove host production compute-0-6
- 一旦你从该组删除所有主机,随后重新运行以下命令:
- ~# openstack aggregate show production
- +-------------------+----------------------------+
- | Field | Value |
- +-------------------+----------------------------+
- | availability_zone | production-az |
- | created_at | 2019-08-17T03:02:42.000000 |
- | deleted | False |
- | deleted_at | None |
- | hosts | [] |
- | id | 7 |
- | name | production |
- | properties | |
- | updated_at | None |
- +-------------------+----------------------------+
我们可以从上述输出看出,没有与生产主机聚合组关联的计算主机,现在我们可以删除组了。
使用以下命令来删除主机聚合组及关联的可用区:
- ~# openstack aggregate delete production
运行以下命令,以核实可用区有没有删除:
- ~# openstack availability zone list | grep -i production-az
- ~#
同样,你可以参照上述步骤来删除非生产主机聚合组及其可用区。