本帖最后由 pig2 于 2014-3-17 18:42 编辑
1.虚拟机操作
1.1. 从image启动虚拟机
需要参数 image id 和 flavor id
运行nova image-list 得到image id
# nova image-list
+--------------------------------------+-------------------------+--------+--------+
| ID | Name | Status | Server |
+--------------------------------------+-------------------------+--------+--------+
| 9ff7fcf4-f502-41c5-9a85-9a89ba2493f4 | centos_4M | ACTIVE | |
| 533a5c7b-2177-4db2-b128-b88ac1779b5b | precise | ACTIVE | |
+--------------------------------------+-------------------------+--------+--------+ 复制代码
运行nova flavor-list得到flavor id # nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | extra_specs |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| 1 | m1.tiny | 512 | 0 | 0 | | 1 | 1.0 | True | {} |
| 2 | m1.small | 2048 | 20 | 0 | | 1 | 1.0 | True | {} |
| 3 | m1.medium | 4096 | 40 | 0 | | 2 | 1.0 | True | {} |
| 4 | m1.large | 8192 | 80 | 0 | | 4 | 1.0 | True | {} |
| 5 | m1.xlarge | 16384 | 160 | 0 | | 8 | 1.0 | True | {} |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ 复制代码
运行nova boot命令,启动一台虚拟机,使用precise 的image,m1.tiny的flavor # nova boot --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 vm1 复制代码
1.2. 从volume启动虚拟机
主要参数和从image 启动一样,另外需要 block_device_mapping参数
使用nova volume-create 命令,从一个可启动的image 生成一个可启动的volume,image格式文件必须是raw格式 # nova volume-create --image-id 533a5c7b-2177-4db2-b128-b88ac1779b5b --display-name precise-vol 10
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| attachments | [] |
| availability_zone | nova |
| created_at | 2013-03-12T07:47:03.224001 |
| display_description | None |
| display_name | precise-vol |
| id | f18fc147-c16d-4a5a-a5cb-44cef08a6352 |
| image_id | 533a5c7b-2177-4db2-b128-b88ac1779b5b |
| metadata | {} |
| size | 10 |
| snapshot_id | None |
| status | creating |
| volume_type | None |
+---------------------+--------------------------------------+ 复制代码
使用 nova boot 命令,从这个volume启动
tips:虽然是从volume启动,但是任然需要–image 参数,openstack不会从这个image启动 这是folsome版的一个bug,这个bug已经在grizzly版中得到修正,参考 http://docs.openstack.org/trunk/ ... ot-from-volume.html https://bugs.launchpad.net/nova/+bug/1008622 # nova boot --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 --block_device_mapping vda=f18fc147-c16d-4a5a-a5cb-44cef08a6352:::0 vm2-vol 复制代码
1.3. 删除虚拟机
运行nova delete 删除虚拟机复制代码
运行nova list查看是否删除成功
1.4. 迁移虚拟机
使用nova-manage service list查看可用的nova-compute节点, :-)状态为正常节点,XXX状态为故障,需要修复 # nova-manage service list
Binary Host Zone Status State Updated_At
nova-cert controller2 nova enabled :-) 2013-03-12 08:04:46
nova-consoleauth controller2 nova enabled :-) 2013-03-12 08:04:46
nova-scheduler controller2 nova enabled :-) 2013-03-12 08:04:45
nova-compute compute1 nova enabled :-) 2013-03-12 08:04:44
nova-compute compute2 nova enabled XXX 2013-03-11 07:29:21 复制代码
运行 nova show 查看虚拟机运行在哪一个物理计算节点上 # nova show vm1
| OS-EXT-SRV-ATTR:hypervisor_hostname | compute1 复制代码
运行 nova live-migration ,迁移到compute2节点上 # nova live-migration vm1 compute2 复制代码
1.5. 对instance进行快照
使用 nova image-create 对运行的instance进行快照,得到的快照是一个image,可以使用nova image-list 查看 # nova image-create vm1 vm1-snap 复制代码
1.6. 对instance的volume进行快照
如果虚拟机是从volume启动,对虚拟机快照就是直接对虚拟机启动的volume进行快照。
运行cinder list查看vm启动的volume id ,attached to 是虚拟机id # cinder list
+--------------------------------------+-----------+--------------------------------------+------+-------------+--------------------------------------+
| ID | Status | Display Name | Size | Volume Type | Attached to |
| 444da795-8482-463d-aa27-d31b3b0c6b4f | in-use | ce87b5bf-8295-4569-821e-82f3e2c923bd | 10 | None | f18fc147-c16d-4a5a-a5cb-44cef08a6352 |
+--------------------------------------+-----------+--------------------------------------+------+-------------+--------------------------------------+ 复制代码
使用nova volume-snapshot-create 对虚拟机的volume进行快照# nova volume-snapshot-create --force True --display-name vol-snap1 444da795-8482-463d-aa27-d31b3b0c6b4f
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| created_at | 2013-03-12T09:25:16.669465 |
| display_description | None |
| display_name | vol-snap1 |
| id | 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 |
| size | 10 |
| status | creating |
| volume_id | 444da795-8482-463d-aa27-d31b3b0c6b4f |
+---------------------+--------------------------------------+ 复制代码
运行 cinder snapshot-list 查看 # cinder snapshot-list
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+
| ID | Volume ID | Status | Display Name | Size |
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+
| 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 | 444da795-8482-463d-aa27-d31b3b0c6b4f | available | vol-snap1 | 10 |
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+ 复制代码
1.7. 从volume的snapshot启动
先将volume的snapshot转换成一个新的volume ,大小为10G # nova volume-create --snapshot-id 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 --display-name vol-from-snap 10
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| attachments | [] |
| availability_zone | nova |
| created_at | 2013-03-12T09:34:53.498823 |
| display_description | None |
| display_name | vol-from-snap |
| id | f18b2df7-b3be-44ad-b521-c69f04ab8c49 |
| metadata | {} |
| size | 10 |
| snapshot_id | 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 |
| status | creating |
| volume_type | None |
+---------------------+--------------------------------------+ 复制代码
得到新的volume id 后,从volume启动 # nova boot --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 --block_device_mapping vda=f18b2df7-b3be-44ad-b521-c69f04ab8c49:::0 vm2 复制代码
1.8. 使用metadata
编写user data,参考cloudinit
更改主机名 # cat cloudconfig.yaml
#cloud-config
hostname: mynode
fqdn: mynode.example.com 复制代码
启动时,加上 --user-data 参数,如果metadata service工作正常,主机名会变成mynode,反之则证明metadata service工作不正常 # nova boot --user-data cloudconfig.yaml --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 vm3 复制代码
注入脚本 # cat myscript.sh
#!/bin/sh
echo "Hello World. The time is now $(date -R)!" | tee /root/output.txt 复制代码
这段脚本会记录被注入脚本运行的时间,记录在 /root/output.txt 路径下。# nova boot --user-data myscript.sh --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 --block_device_mapping vda=f18fc147-c16d-4a5a-a5cb-44cef08a6352:::0 vm4 复制代码
登录到虚拟机查看是否生效 root@vm4:~# cat output.txt
Hello World. The time is now Wed, 13 Mar 2013 02:58:12 +0000! 复制代码
2.存储操作
2.1. 镜像操作
创建镜像
自己制作镜像,或者去 cloud ubuntu 下载ubuntu官方镜像,ubuntu官方镜像已经集成cloudinit 可以直接使用metadata service
下载 precise的img文件 ,并上传到glance中 # wget http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64-disk1.img
# glance image-create --name precise --is-public true --container-format bare --disk-format qcow2 < precise-server-cloudimg-amd64-disk1.img
+------------------+--------------------------------------+
| Property | Value |
+------------------+--------------------------------------+
| checksum | 2cd973b67600cef111a2c951ef1084d2 |
| container_format | bare |
| created_at | 2013-03-13T05:10:06 |
| deleted | False |
| deleted_at | None |
| disk_format | qcow2 |
| id | 2fd11347-c951-4f81-ac54-205afd783791 |
| is_public | True |
| min_disk | 0 |
| min_ram | 0 |
| name | precise |
| owner | None |
| protected | False |
| size | 233832448 |
| status | active |
| updated_at | 2013-03-13T05:10:31 |
+------------------+--------------------------------------+ 复制代码
运行 glance image-list 查看镜像状态,active为可以使用。# glance image-list
+--------------------------------------+-------------------------+-------------+------------------+-------------+--------+
| ID | Name | Disk Format | Container Format | Size | Status |
+--------------------------------------+-------------------------+-------------+------------------+-------------+--------+
| 2fd11347-c951-4f81-ac54-205afd783791 | precise | qcow2 | bare | 233832448 | active |
+--------------------------------------+-------------------------+-------------+------------------+-------------+--------+ 复制代码
删除镜像
运行 glance image-delete 删除对应的image,参数为image id# glance image-delete 2fd11347-c951-4f81-ac54-205afd783791 复制代码
2.2. 卷操作
2.2.1 创建,删除卷
创建空卷
cinder create 需要一个参数size,这里指定10,默认单位为G;名称为empty-vol# cinder create --display-name empty-vol 10
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| attachments | [] |
| availability_zone | nova |
| created_at | 2013-03-13T05:19:05.765784 |
| display_description | None |
| display_name | empty-vol |
| id | eed6ef03-14f4-4cac-8182-3e19999f896d |
| metadata | {} |
| size | 10 |
| snapshot_id | None |
| status | creating |
| volume_type | None |
+---------------------+--------------------------------------+ 复制代码
从image 创建卷
使用glance image-list查看可用image id,运行cinder create命令创建volume # cinder create --image-id 533a5c7b-2177-4db2-b128-b88ac1779b5b --display-name vol-from-image 10
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| attachments | [] |
| availability_zone | nova |
| created_at | 2013-03-13T05:27:47.160512 |
| display_description | None |
| display_name | vol-from-image |
| id | 4f111593-12e9-471b-939b-d72fee230cbd |
| image_id | 533a5c7b-2177-4db2-b128-b88ac1779b5b |
| metadata | {} |
| size | 10 |
| snapshot_id | None |
| status | creating |
| volume_type | None |
+---------------------+--------------------------------------+ 复制代码
对卷进行snapshot
对卷进行snapshot,参数为volume id# cinder snapshot-create --display-name vol-snapshot1 4f111593-12e9-471b-939b-d72fee230cbd 复制代码
运行cinder snapshot-list查看snapshot # cinder snapshot-list
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+
| ID | Volume ID | Status | Display Name | Size |
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+
| 647e1bb6-6087-48e4-8310-1b3047893ae5 | 4f111593-12e9-471b-939b-d72fee230cbd | creating | vol-snapshot1 | 10 |
+--------------------------------------+--------------------------------------+-----------+------------------------------+------+ 复制代码
从volume snapshot创建卷
在虚拟机从volume snapshot启动相关章节有详细描述 # nova volume-create --snapshot-id 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 --display-name vol-from-snap 10
+---------------------+--------------------------------------+
| Property | Value |
+---------------------+--------------------------------------+
| attachments | [] |
| availability_zone | nova |
| created_at | 2013-03-12T09:34:53.498823 |
| display_description | None |
| display_name | vol-from-snap |
| id | f18b2df7-b3be-44ad-b521-c69f04ab8c49 |
| metadata | {} |
| size | 10 |
| snapshot_id | 8cd8c57a-5ffa-4c92-98a2-ec10ae899166 |
| status | creating |
| volume_type | None |
+---------------------+--------------------------------------+ 复制代码
2.2.2 在虚拟机中挂载卸载卷
向虚拟机中添加卷
使用nova volume-attach <server> <volume> <device>将卷添加到虚拟机中 # nova volume-attach b30c0914-b87b-41d4-a381-7168d6be4cf3 eed6ef03-14f4-4cac-8182-3e19999f896d /dev/vdb
+----------+--------------------------------------+
| Property | Value |
+----------+--------------------------------------+
| device | /dev/vdb |
| id | eed6ef03-14f4-4cac-8182-3e19999f896d |
| serverId | b30c0914-b87b-41d4-a381-7168d6be4cf3 |
| volumeId | eed6ef03-14f4-4cac-8182-3e19999f896d |
+----------+--------------------------------------+ 复制代码
到虚拟机中查看vdb是都添加成功 # fdisk -l | grep vdb
Disk /dev/vdb doesn't contain a valid partition table
Disk /dev/vdb: 10.7 GB, 10737418240 bytes 复制代码
将卷从虚拟机中卸载
使用nova volume-detach <server> <volume>将卷从虚拟机中卸载,卸载前请保证磁盘已经umount,否则数据无法保证数据一致性。 # nova volume-detach b30c0914-b87b-41d4-a381-7168d6be4cf3 eed6ef03-14f4-4cac-8182-3e19999f896d 复制代码
到虚拟机中查看vdb是都卸载成功复制代码
3. 网络操作
3.1 vlan 模式下的网络操作
vlan模式下的接口作用
3.1.1 创建网络
使用quantum net-create创建虚拟网络,quantum net-create参数中--provider:segmentation_id为vlan id ,这个vlan id在物理交换机中必须提前建立 # quantum net-create vlan-net --provider:network_type vlan --provider:physical_network physnet1 --provider:segmentation_id 1024
Created a new network:
+---------------------------+--------------------------------------+
| Field | Value |
+---------------------------+--------------------------------------+
| admin_state_up | True |
| id | 347039c5-bfd6-498b-999a-a577a998427d |
| name | vlan-net |
| provider:network_type | vlan |
| provider:physical_network | physnet1 |
| provider:segmentation_id | 1024 |
| router:external | False |
| shared | False |
| status | ACTIVE |
| subnets | |
| tenant_id | acbd5e5812c1475f9ec60104569710f4 |
+---------------------------+--------------------------------------+ 复制代码
3.1.2 创建子网
使用quantum subnet-create创建子网,第一个参数为网络名,第二个参数为网络cidr # quantum subnet-create vlan-net 10.10.11.0/24
Created a new subnet:
+------------------+------------------------------------------------+
| Field | Value |
+------------------+------------------------------------------------+
| allocation_pools | {"start": "10.10.11.2", "end": "10.10.11.254"} |
| cidr | 10.10.11.0/24 |
| dns_nameservers | |
| enable_dhcp | True |
| gateway_ip | 10.10.11.1 |
| host_routes | |
| id | a8d6005f-c051-491d-aa92-19322206a992 |
| ip_version | 4 |
| name | |
| network_id | 347039c5-bfd6-498b-999a-a577a998427d |
| tenant_id | acbd5e5812c1475f9ec60104569710f4 |
+------------------+------------------------------------------------+ 复制代码
使用 查看刚创建的网络,及其子网 # quantum net-list
+--------------------------------------+--------------+--------------------------------------+
| id | name | subnets |
+--------------------------------------+--------------+--------------------------------------+
| 347039c5-bfd6-498b-999a-a577a998427d | vlan-net | a8d6005f-c051-491d-aa92-19322206a992 |
+--------------------------------------+--------------+--------------------------------------+ 复制代码
3.1.3 启动虚拟机
网络为刚创建的网络# nova boot --nic net-id=347039c5-bfd6-498b-999a-a577a998427d --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 --block_device_mapping vda=f18fc147-c16d-4a5a-a5cb-44cef08a6352:::0 vm7 复制代码
3.2 gre 模式下的网络操作
3.2.1 创建gre网络
下载quantum script 运行脚本# bash quantum-networking.sh 复制代码
3.2.3 启动虚拟机
网络为刚创建的网络,这个过程和vlan模式一样 # nova boot --nic net-id=347039c5-bfd6-498b-999a-a577a998427d --image 533a5c7b-2177-4db2-b128-b88ac1779b5b --flavor 1 --block_device_mapping vda=f18fc147-c16d-4a5a-a5cb-44cef08a6352:::0 vm7 复制代码