本帖最后由 pig2 于 2014-2-24 01:57 编辑
可以带着下面问题来阅读
1.创建虚拟机的方式有几种?
2.如何查看镜像?
3.如何创建虚拟机?
4.如何查看新创建虚拟机?
创建Dashbord有两种方式:
1.通过Dashbord创建新的虚拟机
2.通过命令来创建虚拟机
下面介绍通过命令来创建虚拟机
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.下载或制作自己的虚拟机镜像
到官方网站下载。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 ubuntu@10.0.0.2
- 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:~[ DISCUZ_CODE_28 ]nbsp;
复制代码
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
复制代码
考题:
你能否回答什么是虚拟机镜像?虚拟机镜像用来干什么?
|