如何在Openstack的控制节点使用命令手动创建一个虚拟机

虽然可以通过Dashbord创建新的虚拟机,可是通过命令来创建虚拟机也是一种很好的方法。

1.激活环境变量。

如果本来就激活了就没有必要进行这一步了。

新建文件novarc

export OS_TENANT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=123456
export OS_AUTH_URL="http://10.214.0.179:5000/v2.0/"

执行脚本

source novarc

2.下载或制作自己的虚拟机镜像。

可以参照vpsee的博客制作,或者到官方网站下载。http://cloud-images.ubuntu.com/lucid/current/


3.上传镜像。

glance add name="ubuntu rubis" is_public=true container_format=vof disk_format=vhd < /root/images/disk.img

ps:(可选)脚本:

新建文件upimage.sh

IMAGE_NAME='quantal-server-cloudimg-amd64'#改成你下载镜像的名字
echo "upload images"
mkdir -p /tmp/images
tar -zxf ./$IMAGE_NAME.tar.gz  -C /tmp/images
RVAL=`glance add name="ubuntu-10.04.2-kernel" is_public=true container_format=aki disk_format=aki < /tmp/images/$IMAGE_NAME-vmlinuz*`
KERNEL_ID=`echo $RVAL | cut -d":" -f2 | tr -d " "`
glance add  name="ubuntu-10.04.2" is_public=true container_format=ami disk_format=ami kernel_id=$KERNEL_ID < /tmp/images/$IMAGE_NAME.img

执行脚本:sh upimage.sh

说明:

镜像在物理机上的实际位置:

root@nova-controller:/var/lib/glance#ls
glance.sqlite  image-cache images

4.创建虚拟机密钥对。

root@nova-controller:/var/lib/stackops# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa2
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa2.
Your public key has been saved in /root/.ssh/id_rsa2.pub.
The key fingerprint is:
46:ef:fa:ee:46:f4:7f:82:37:b9:e4:fe:39:2e:3e:30 root@nova-controller
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        .        |
|       . ..      |
|        S...     |
|       . ..E.    |
|         .. oo.. |
|         .. .=*.o|
|        .=+ .+BOo|
+-----------------+
5.导入密钥

root@nova-controller:/var/lib/stackops# nova keypair-list
root@nova-controller:~# nova keypair-add --pub_key .ssh/id_rsa2.pub key2

6.查看镜像

root@nova-controller:~# nova image-list
+----+-----------------------+--------+
| ID |          Name         | Status |
+----+-----------------------+--------+
| 1  | ubuntu-10.04.2-kernel | ACTIVE |
| 2  | ubuntu-10.04.2        | ACTIVE |
+----+-----------------------+--------+

7.查看虚拟机规格

root@nova-controller:~# nova flavor-list
+----+-----------+-----------+------+----------+-------+------------+----------+
| ID |    Name   | Memory_MB | Swap | Local_GB | VCPUs | RXTX_Quota | RXTX_Cap |
+----+-----------+-----------+------+----------+-------+------------+----------+
| 1  | m1.tiny   | 512       | 0    | 0        | 1     | 0          | 0        |
| 2  | m1.small  | 2048      | 0    | 20       | 1     | 0          | 0        |
| 3  | m1.medium | 4096      | 0    | 40       | 2     | 0          | 0        |
| 4  | m1.large  | 8192      | 0    | 80       | 4     | 0          | 0        |
| 5  | m1.xlarge | 16384     | 0    | 160      | 8     | 0          | 0        |
+----+-----------+-----------+------+----------+-------+------------+----------+

8.创建虚拟机

root@nova-controller:~# nova boot --flavor  1 --image 2 --key_name key2 Secondvmzju
+--------------+--------------------------------------+
|   Property   |                Value                 |
+--------------+--------------------------------------+
| accessIPv4   |                                      |
| accessIPv6   |                                      |
| adminPass    | SgoPBBCaaN5BumLA                     |
| config_drive |                                      |
| created      | 2012-06-22T19:14:17Z                 |
| flavor       | m1.tiny                              |
| hostId       |                                      |
| id           | 1                                    |
| image        | ubuntu-10.04.2                       |
| key_name     | key2                                 |
| metadata     | {}                                   |
| name         | Secondvmzju                          |
| progress     | 0                                    |
| status       | BUILD                                |
| tenant_id    | 1                                    |
| updated      | 2012-06-22T19:14:17Z                 |
| user_id      | admin                                |
| uuid         | 4cf112db-5db2-4e1a-af5a-219689a11300 |
+--------------+--------------------------------------+

--flavor:指定虚拟机规格

--image:指定使用镜像

--key_name:指定使用key

“secondvmzju”为虚拟机名

(一个image可以创建多个虚拟机,但需要你的image,是采用qcow2的格式。)

9.查看新创建的虚拟机

root@nova-controller:~# nova show Secondvmzju
+-----------------+----------------------------------------------------------+
|     Property    |                          Value                           |
+-----------------+----------------------------------------------------------+
| accessIPv4      |                                                          |
| accessIPv6      |                                                          |
| config_drive    |                                                          |
| created         | 2012-06-22T19:14:17Z                                     |
| flavor          | m1.tiny                                                  |
| hostId          | 28aabad0d319fc4dc6bd5a1f0d496229ee46648ec74f45fd7084047d |
| id              | 1                                                        |
| image           | ubuntu-10.04.2                                           |
| key_name        | key2                                                     |
| metadata        | {}                                                       |
| name            | Secondvmzju                                              |
| progress        | 100                                                      |
| service network | 10.0.0.2                                                 |
| status          | ACTIVE                                                   |
| tenant_id       | 1                                                        |
| updated         | 2012-07-05T11:32:19Z                                     |
| user_id         | admin                                                    |
| uuid            | 4cf112db-5db2-4e1a-af5a-219689a11300                     |
+-----------------+----------------------------------------------------------+

10.网络配置

给虚拟机配置网络(可选)。

查看当前公网已使用的ip:
root@nova-controller:~# nova floating-ip-list
+-------------+-------------+----------+
|      Ip     | Instance Id | Fixed Ip |
+-------------+-------------+----------+
| 192.168.1.1 | None        | None     |
| 192.168.1.2 | 1           | 10.0.0.2 |
+-------------+-------------+----------+

创建公网地址
root@nova-controller:~# nova floating-ip-create
+-------------+-------------+----------+
|      Ip     | Instance Id | Fixed Ip |
+-------------+-------------+----------+
| 192.168.1.2 | None        | None     |
+-------------+-------------+----------+
绑定公网地址IP到虚拟机
root@nova-controller:~# nova add-floating-ip Secondvmzju 192.168.1.2
再次查看虚拟机,注意servicenetwork
root@nova-controller:~# nova show Secondvmzju
+-----------------+----------------------------------------------------------+
|     Property    |                          Value                           |
+-----------------+----------------------------------------------------------+
| accessIPv4      |                                                          |
| accessIPv6      |                                                          |
| config_drive    |                                                          |
| created         | 2012-06-22T19:14:17Z                                     |
| flavor          | m1.tiny                                                  |
| hostId          | 28aabad0d319fc4dc6bd5a1f0d496229ee46648ec74f45fd7084047d |
| id              | 1                                                        |
| image           | ubuntu-10.04.2                                           |
| key_name        | key2                                                     |
| metadata        | {}                                                       |
| name            | Secondvmzju                                              |
| progress        | 100                                                      |
| service network | 10.0.0.2, 192.168.1.2                                    |
| status          | ACTIVE                                                   |
| tenant_id       | 1                                                        |
| updated         | 2012-07-05T11:32:19Z                                     |
| user_id         | admin                                                    |
| uuid            | 4cf112db-5db2-4e1a-af5a-219689a11300                     |
+-----------------+----------------------------------------------------------+
防火墙设置:
允许ssh:
root@nova-controller:~# nova secgroup-add-rule default tcp 22 22 0.0.0.0/0
+-------------+-----------+---------+-----------+--------------+
| IP Protocol | From Port | To Port |  IP Range | Source Group |
+-------------+-----------+---------+-----------+--------------+
| tcp         | 22        | 22      | 0.0.0.0/0 |              |
+-------------+-----------+---------+-----------+--------------+
允许ping
root@nova-controller:~# nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0
+-------------+-----------+---------+-----------+--------------+
| IP Protocol | From Port | To Port |  IP Range | Source Group |
+-------------+-----------+---------+-----------+--------------+
| icmp        | -1        | -1      | 0.0.0.0/0 |              |
+-------------+-----------+---------+-----------+--------------+

查看防火墙规则:
root@nova-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 |              |
+-------------+-----------+---------+-----------+--------------+
11.连接虚拟机

使用ssh连接(进入)虚拟机
ssh –i 通过身份认证文件验证的方式登录。

root@nova-controller:~# ssh -i .ssh/id_rsa2 [email protected]
The authenticity of host '10.0.0.2 (10.0.0.2)' can't be established.
RSA key fingerprint is aa:fc:78:9a:bd:6d:f3:bb:67:cd:28:15:b5:06:fb:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.2' (RSA) to the list of known hosts.

可以查看虚拟机的名字,就是创建的时候定的名字。

ubuntu@secondvmzju:~$ hostname
secondvmzju
ubuntu@secondvmzju:~$


12.备注

在计算节点上可以通过virsh对虚拟机进行管理(不过建议是用nova)。

root@nova-compute-1:~# virsh list --all
 Id Name                 State
----------------------------------
  1 instance-00000001    running

instance在计算节点上的实际位置:
root@nova-compute-1:/var/lib/nova/instances# ls
_base  instance-00000001

可以把公网的ip与虚拟机解除绑定。

root@nova-controller:~# nova remove-floating-ip Secondvmzju 192.168.1.2
回收ip。
root@nova-controller:~# nova floating-ip-delete 192.168.1.2

参考文献:

http://www.slideshare.net/slmagicbox/stackops-step-bystep-guide
http://docs.stackops.org/display/doc03/Operating+Openstack+with+Stackops+Distro


你可能感兴趣的:(虚拟机,image,防火墙,ubuntu,ssh,NetWork)