openstack之创建虚拟机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了openstack之创建虚拟机相关的知识,希望对你有一定的参考价值。

1、创建网络和子网

[[email protected] ~]# source admin-openrc.sh 
[[email protected] ~]# neutron net-create flat --shared --provider:physical_network physnet1 --provider:network_type flat
Created a new network:
+---------------------------+--------------------------------------+
| Field                     | Value                                |
+---------------------------+--------------------------------------+
| admin_state_up            | True                                 |
| id                        | 61b74dc3-e39a-4a35-ad7a-64c71e2740d7 |
| mtu                       | 0                                    |
| name                      | flat                                 |
| port_security_enabled     | True                                 |
| provider:network_type     | flat                                 |
| provider:physical_network | physnet1                             |
| provider:segmentation_id  |                                      |
| router:external           | False                                |
| shared                    | True                                 |
| status                    | ACTIVE                               |
| subnets                   |                                      |
| tenant_id                 | 8a3b7f9f1b2c4f7eaf7780d268e672d1     |
+---------------------------+--------------------------------------+
[[email protected] ~]# 
[[email protected] ~]# neutron subnet-create flat 172.16.80.0/24 --name flat-subnet --allocation-pool start=172.16.80.180,end=172.16.80.230 --dns-nameserver 114.114.114.114 --gateway 172.16.80.2
Created a new subnet:
+-------------------+----------------------------------------------------+
| Field             | Value                                              |
+-------------------+----------------------------------------------------+
| allocation_pools  | {"start": "172.16.80.180", "end": "172.16.80.230"} |
| cidr              | 172.16.80.0/24                                     |
| dns_nameservers   | 114.114.114.114                                    |
| enable_dhcp       | True                                               |
| gateway_ip        | 172.16.80.2                                        |
| host_routes       |                                                    |
| id                | ec1e5cb6-e1c2-462c-865e-85383a97a434               |
| ip_version        | 4                                                  |
| ipv6_address_mode |                                                    |
| ipv6_ra_mode      |                                                    |
| name              | flat-subnet                                        |
| network_id        | 61b74dc3-e39a-4a35-ad7a-64c71e2740d7               |
| subnetpool_id     |                                                    |
| tenant_id         | 8a3b7f9f1b2c4f7eaf7780d268e672d1                   |
+-------------------+----------------------------------------------------+

查看创建的网络
[[email protected] ~]# neutron net-list
+--------------------------------------+------+-----------------------------------------------------+
| id                                   | name | subnets                                             |
+--------------------------------------+------+-----------------------------------------------------+
| 61b74dc3-e39a-4a35-ad7a-64c71e2740d7 | flat | ec1e5cb6-e1c2-462c-865e-85383a97a434 172.16.80.0/24 |
+--------------------------------------+------+-----------------------------------------------------+

2、创建公钥和私钥,添加到openstack中

[[email protected] ~]# source demo-openrc.sh 
[[email protected] ~]# ssh-keygen -q -N ""
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
[[email protected] ~]# nova keypair-add --pub-key .ssh/id_rsa.pub mykey
[[email protected] ~]# nova keypair-list
+-------+-------------------------------------------------+
| Name  | Fingerprint                                     |
+-------+-------------------------------------------------+
| mykey | 1c:e7:78:fe:de:e1:0d:b4:1f:55:b9:a1:01:4b:21:52 |
+-------+-------------------------------------------------+

3、创建一个安全组放行icmp和ssh

[[email protected] ~]# 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 |              |
+-------------+-----------+---------+-----------+--------------+
[[email protected] ~]# 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 |              |
+-------------+-----------+---------+-----------+--------------+
[[email protected] ~]# nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| ID | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| 1  | m1.tiny   | 512       | 1    | 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      |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
[[email protected] ~]# nova image-list
+--------------------------------------+--------+--------+--------+
| ID                                   | Name   | Status | Server |
+--------------------------------------+--------+--------+--------+
| 6772deca-7b31-4f97-affa-668518b22972 | cirros | ACTIVE |        |
+--------------------------------------+--------+--------+--------+
[[email protected] ~]# neutron net-list
+--------------------------------------+------+-----------------------------------------------------+
| id                                   | name | subnets                                             |
+--------------------------------------+------+-----------------------------------------------------+
| 61b74dc3-e39a-4a35-ad7a-64c71e2740d7 | flat | ec1e5cb6-e1c2-462c-865e-85383a97a434 172.16.80.0/24 |
+--------------------------------------+------+-----------------------------------------------------+
[[email protected] ~]# nova secgroup-list
+--------------------------------------+---------+------------------------+
| Id                                   | Name    | Description            |
+--------------------------------------+---------+------------------------+
| 4751243a-79a7-4629-b12e-e5f7030d1c85 | default | Default security group |
+--------------------------------------+---------+------------------------+

4、创建虚拟机

[[email protected] ~]# nova boot --flavor m1.tiny --image cirros --nic net-id=61b74dc3-e39a-4a35-ad7a-64c71e2740d7  --security-group default --key-name mykey martin
+--------------------------------------+-----------------------------------------------+
| Property                             | Value                                         |
+--------------------------------------+-----------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                        |
| OS-EXT-AZ:availability_zone          |                                               |
| 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                            | XxwtiCC7LWQr                                  |
| config_drive                         |                                               |
| created                              | 2016-11-02T14:20:44Z                          |
| flavor                               | m1.tiny (1)                                   |
| hostId                               |                                               |
| id                                   | a1d9a40f-ca6a-4e69-854f-671a16f3374c          |
| image                                | cirros (6772deca-7b31-4f97-affa-668518b22972) |
| key_name                             | mykey                                         |
| metadata                             | {}                                            |
| name                                 | martin                                        |
| os-extended-volumes:volumes_attached | []                                            |
| progress                             | 0                                             |
| security_groups                      | default                                       |
| status                               | BUILD                                         |
| tenant_id                            | 3653ec22551f472b94e9438bcd9097bf              |
| updated                              | 2016-11-02T14:20:45Z                          |
| user_id                              | da1ed7fb5f494091a633afd6da29f900              |
+--------------------------------------+-----------------------------------------------+

5、安装dashboard

[[email protected] ~]# yum install openstack-dashboard -y
[[email protected] openstack-dashboard]#vim /etc/openstack-dashboard/local_settings 
ALLOWED_HOSTS = [‘*‘, ‘localhost‘]
OPENSTACK_HOST = "172.16.80.130"
 OPENSTACK_KEYSTONE_DEFAULT_ROLE = "user"
 CACHES = {
    ‘default‘: {
         ‘BACKEND‘: ‘django.core.cache.backends.memcached.Memcac
    hedCache‘,
         ‘LOCATION‘: ‘172.16.80.130:11211‘,
     }
 }     
 TIME_ZONE = "Asia/ShangHai" 
 
 [[email protected] openstack-dashboard]# systemctl restart httpd


6、创建云主机

技术分享

中途有报错

2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637] Traceback (most recent call last):
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]   File "/usr/lib/python2.7/site-packages/nova/compute/manager.py", line 2156, in _build_resources
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]     yield resources
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]   File "/usr/lib/python2.7/site-packages/nova/compute/manager.py", line 2009, in _build_and_run_instance
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]     block_device_info=block_device_info)
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]   File "/usr/lib/python2.7/site-packages/nova/virt/libvirt/driver.py", line 2534, in spawn
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]     block_device_info=block_device_info)
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]   File "/usr/lib/python2.7/site-packages/nova/virt/libvirt/driver.py", line 4642, in _create_domain_and_network
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]     raise exception.VirtualInterfaceCreateException()
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637] VirtualInterfaceCreateException: Virtual Interface creation failed
2016-11-05 00:06:12.904 2412 ERROR nova.compute.manager [instance: 39942fb6-24d6-4d44-ba22-34e7676fd637]

解决办法,修改计算节点的配置文件

vif_plugging_is_fatal=false

vif_plugging_timeout=0


[[email protected] nova]# systemctl restart libvirtd openstack-nova-compute

7、登录测试

[[email protected] nova]# ssh [email protected]
The authenticity of host ‘172.16.80.184 (172.16.80.184)‘ can‘t be established.
RSA key fingerprint is b5:e8:9c:7c:15:22:26:09:6e:f4:aa:e0:61:79:25:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.16.80.184‘ (RSA) to the list of known hosts.
[email protected]‘s password: 
$ 
$ ifconfig
eth0      Link encap:Ethernet  HWaddr FA:16:3E:75:AF:5D  
          inet addr:172.16.80.184  Bcast:172.16.80.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fe75:af5d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:129 errors:0 dropped:0 overruns:0 frame:0
          TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:15498 (15.1 KiB)  TX bytes:15464 (15.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1516 (1.4 KiB)  TX bytes:1516 (1.4 KiB)

技术分享

本文出自 “厚德载物” 博客,谢绝转载!

以上是关于openstack之创建虚拟机的主要内容,如果未能解决你的问题,请参考以下文章

云计算管理平台之OpenStack启动虚拟机实例

openstack学习笔记一 虚拟机启动过程代码跟踪

nova创建虚拟机流程源码系列分析之二 wsgi模型

Openstack之Nova创建虚机流程分析

openstack部署之创建第一个实例

OpenStack之Nova模块