[管理篇3]VMWare搭建Openstack——批量创建虚拟机实例

时间:2022-12-23 13:40:12

可能看到这里又会有人问到,既然是云环境,总不能让我老是手动创建虚拟机吧,我老是听别人说起他们的云环境,管理几十台,上百台,上千台虚拟机,手动创建简直是太低级了,怎么才能批量创建呢?


当然,可以批量创建,但是由于现在的环境是VM,也就是虚拟机中生成虚拟机,所以不能实际操作,但是我可以介绍相关的命令来创建虚拟机,大家循环这个命令,也就变相的实现了批量创建虚拟机了。


1、我们首先要创建一个SSH Key

sm@controller:~$ source admin-openrc.sh
sm@controller:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sm/.ssh/id_rsa):
Created directory '/home/sm/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sm/.ssh/id_rsa.
Your public key has been saved in /home/sm/.ssh/id_rsa.pub.
The key fingerprint is:
5b:94:e1:03:04:d6:01:0c:f1:db:2f:10:c9:3b:3d:ca sm@controller
The key's randomart image is:
+--[ RSA 2048]----+
|    o++=+..      |
|     +...o o     |
|      =   =      |
|       * . .     |
|      = S .      |
|     . + =       |
|      E o .      |
|         .       |
|                 |
+-----------------+
sm@controller:~$ nova keypair-add --pub-key ~/.ssh/id_rsa.pub sm-key

然后通过这个Key来批量创建虚拟机


注意:这个操作不应该添加sudo,如果使用sudo,就是在root用户下的key了,权限比较高!


2、查看相关资源信息

查看刚刚创建的sm-key

sm@controller:~$ nova keypair-list
+--------+-------------------------------------------------+
| Name   | Fingerprint                                     |
+--------+-------------------------------------------------+
| sm-key | 5b:94:e1:03:04:d6:01:0c:f1:db:2f:10:c9:3b:3d:ca |
+--------+-------------------------------------------------+


查看云主机类型信息,里面已经包含了我前面已经创建的my类型
sm@controller:~$ nova flavor-list
+--------------------------------------+-----------+-----------+------+---------
| ID                                   | Name      | Memory_MB | Disk | Ephemera
+--------------------------------------+-----------+-----------+------+---------
| 1                                    | m1.tiny   | 512       | 1    | 0
| 2                                    | m1.small  | 2048      | 20   | 0
| 3                                    | m1.medium | 4096      | 40   | 0
| 4                                    | m1.large  | 8192      | 80   | 0
| 5                                    | m1.xlarge | 16384     | 160  | 0
| 7de2df56-4fdf-40a2-97a6-75f78572e8f9 | my        | 2048      | 10   | 0
+--------------------------------------+-----------+-----------+------+---------

查看已有了镜像信息

sm@controller:~$ nova image-list
+--------------------------------------+---------------------+--------+--------+
| ID                                   | Name                | Status | Server |
+--------------------------------------+---------------------+--------+--------+
| f2b0f15b-bd5e-4766-973c-24547c1faf03 | cirros-0.3.2-x86_64 | ACTIVE |        |
+--------------------------------------+---------------------+--------+--------+

查看网络信息

sm@controller:~$ neutron net-list
+--------------------------------------+------------+---------------------------
| id                                   | name       | subnets
+--------------------------------------+------------+---------------------------
| 1dfa9da1-43fd-4128-b9a0-2ca76a664933 | sharednet1 | db1aad02-3890-44d4-93c4-c9
+--------------------------------------+------------+---------------------------

查看相关的安全信息

sm@controller:~$ nova secgroup-list
+--------------------------------------+---------+-------------+
| Id                                   | Name    | Description |
+--------------------------------------+---------+-------------+
| e92429c7-7d9c-47ac-8eab-9815e6d04ecb | default | default     |
+--------------------------------------+---------+-------------+

查看相关的虚拟机信息(我将刚才创建的虚拟机都删除了),所以查看的信息为空

sm@controller:~$ nova list
+----+------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+----+------+--------+------------+-------------+----------+
+----+------+--------+------------+-------------+----------+

3、设置安全规则

 nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0
   nova secgroup-add-rule default tcp 22 22 0.0.0.0/0

sm@controller:~$ nova secgroup-list-rules default
+-------------+-----------+---------+-----------+--------------+
| IP Protocol | From Port | To Port | IP Range  | Source Group |
+-------------+-----------+---------+-----------+--------------+
| icmp        | -1        | -1      | 0.0.0.0/0 |              |
| tcp         | 22        | 22      | 0.0.0.0/0 |              |
|             |           |         |           | default      |
|             |           |         |           | default      |
+-------------+-----------+---------+-----------+--------------+


3、创建虚拟机的命令为nova boot

nova boot --flavor m1.tiny --image cirros-0.3.2-x86_64 --nic net-id=1dfa9da1-43fd-4128-b9a0-2ca76a664933 --security-group default --key-name sm-key t1


通过我们前面提供的信息我们可以很容易的理解这个命令的意思,创建一个云主机,名称为t1,云主机类型为m1.tiny,镜像为cirros-0.3.2-x86_64,网络信息为:1dfa9da1-43fd-4128-b9a0-2ca76a664933,安全模式为default.

sm@controller:~$ nova boot --flavor m1.tiny --image cirros-0.3.2-x86_64 --nic net-id=1dfa9da1-43fd-4128-b9a0-2ca76a664933 --security-group default --key-name sm-key t1
+--------------------------------------+------------------------------------------------------------+
| Property                             | Value                                                      |
+--------------------------------------+------------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                     |
| OS-EXT-AZ:availability_zone          | nova                                                       |
| OS-EXT-SRV-ATTR:host                 | -                                                          |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                          |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000003                                          |
| OS-EXT-STS:power_state               | 0                                                          |
| OS-EXT-STS:task_state                | scheduling                                                 |
| OS-EXT-STS:vm_state                  | building                                                   |
| OS-SRV-USG:launched_at               | -                                                          |
| OS-SRV-USG:terminated_at             | -                                                          |
| accessIPv4                           |                                                            |
| accessIPv6                           |                                                            |
| adminPass                            | 5BVQG2oN4msE                                               |
| config_drive                         |                                                            |
| created                              | 2015-04-04T09:13:46Z                                       |
| flavor                               | m1.tiny (1)                                                |
| hostId                               |                                                            |
| id                                   | 180e098b-ab40-4194-b75e-ec0de8f4147f                       |
| image                                | cirros-0.3.2-x86_64 (f2b0f15b-bd5e-4766-973c-24547c1faf03) |
| key_name                             | sm-key                                                     |
| metadata                             | {}                                                         |
| name                                 | t1                                                         |
| os-extended-volumes:volumes_attached | []                                                         |
| progress                             | 0                                                          |
| security_groups                      | default                                                    |
| status                               | BUILD                                                      |
| tenant_id                            | b500268a41e34752aba847c7ea464870                           |
| updated                              | 2015-04-04T09:13:46Z                                       |
| user_id                              | 9b1cdfc2b5614eb39f042317f54a286c                           |
+--------------------------------------+------------------------------------------------------------+


我们查看一下nova list

sm@controller:~$ nova list
+--------------------------------------+------+--------+------------+-------------+-------------------------+
| ID                                   | Name | Status | Task State | Power State | Networks                |
+--------------------------------------+------+--------+------------+-------------+-------------------------+
| 180e098b-ab40-4194-b75e-ec0de8f4147f | t1   | ACTIVE | -          | Running     | sharednet1=192.168.3.53 |
+--------------------------------------+------+--------+------------+-------------+-------------------------+

或者直接在界面中查看实例信息

[管理篇3]VMWare搭建Openstack——批量创建虚拟机实例


当然,你可以指定你的虚拟机创建在哪个节点上

sm@controller:~$ sudo nova-manage service list
[sudo] password for sm:
Binary           Host                                 Zone             Status     State Updated_At
nova-cert        controller                           internal         enabled    :-)   2015-04-26 03:45:24
nova-consoleauth controller                           internal         enabled    :-)   2015-04-26 03:45:23
nova-scheduler   controller                           internal         enabled    :-)   2015-04-26 03:45:23
nova-conductor   controller                           internal         enabled    :-)   2015-04-26 03:45:23
nova-compute     computer                             nova             enabled    :-)   2015-04-26 03:45:23
nova-compute     controller                           nova             enabled    :-)   2015-04-26 03:45:25

然后指定我的虚拟机创建在Controller节点上

sm@controller:~$ nova boot --availability-zone nova:controller --flavor m1.tiny --image cirros-0.3.2-x86_64  --nic net-id=1dfa9da1-43fd-4128-b9a0-2ca76a664933   --security-group default --key-name sm-key a2
+--------------------------------------+------------------------------------------------------------+
| Property                             | Value                                                      |
+--------------------------------------+------------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                     |
| OS-EXT-AZ:availability_zone          | nova                                                       |
| OS-EXT-SRV-ATTR:host                 | -                                                          |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                          |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000018                                          |
| OS-EXT-STS:power_state               | 0                                                          |
| OS-EXT-STS:task_state                | scheduling                                                 |
| OS-EXT-STS:vm_state                  | building                                                   |
| OS-SRV-USG:launched_at               | -                                                          |
| OS-SRV-USG:terminated_at             | -                                                          |
| accessIPv4                           |                                                            |
| accessIPv6                           |                                                            |
| adminPass                            | pQ96QmEaCGbg                                               |
| config_drive                         |                                                            |
| created                              | 2015-04-26T03:33:35Z                                       |
| flavor                               | m1.tiny (1)                                                |
| hostId                               |                                                            |
| id                                   | ce74548e-a951-4d85-bb6d-8ff2a62bb82e                       |
| image                                | cirros-0.3.2-x86_64 (f2b0f15b-bd5e-4766-973c-24547c1faf03) |
| key_name                             | sm-key                                                     |
| metadata                             | {}                                                         |
| name                                 | a2                                                         |
| os-extended-volumes:volumes_attached | []                                                         |
| progress                             | 0                                                          |
| security_groups                      | default                                                    |
| status                               | BUILD                                                      |
| tenant_id                            | b500268a41e34752aba847c7ea464870                           |
| updated                              | 2015-04-26T03:33:35Z                                       |
| user_id                              | 9b1cdfc2b5614eb39f042317f54a286c                           |
+--------------------------------------+------------------------------------------------------------+

[管理篇3]VMWare搭建Openstack——批量创建虚拟机实例


我们可以编写Linux的Shell脚本来批量生成多台虚拟机实例

#!/bin/bash

for i in $(seq 1 10)
 do
nova boot  --flavor m1.tiny --image SuperMap7ciServer  --nic net-id=1dfa9da1-43fd-4128-b9a0-2ca76a664933   --security-group default --key-name sm-key iserver$i
        echo "Create VM Named iserver$i sucessful!"
done