七、创建虚拟机实例

1.1.1创建网络(网络选择1: Provider networks )

[root@comtroller1 ~]# source admin-openrc.sh[root@comtroller1 ~]# neutron net-create public --shared --provider:physical_network public --provider:network_type flatCreated a new network:+---------------------------+--------------------------------------+| Field                     | Value                                |+---------------------------+--------------------------------------+| admin_state_up            | True                                 || id                        | e6aff6ca-0749-4c02-95e7-7ebdcd942880 || mtu                       | 0                                    || name                      | public                               || port_security_enabled     | True                                 |#对应linuxbridge_agent.ini文件中[linux_bridge]physical_interface_mappings = public:eth1| provider:network_type     | flat                                 |#对应ml2_conf.ini文件中[ml2_type_flat]flat_networks = public| provider:physical_network | public                               || provider:segmentation_id  |                                      || router:external           | False                                || shared                    | True                                 |#此选项允许所以项目使用虚拟网络| status                    | ACTIVE                               || subnets                   |                                      || tenant_id                 | b56d4cdbaea54df1b3cd3b769ce896a0     |+---------------------------+--------------------------------------+

1.1.2创建子网

[root@comtroller1 ~]# neutron subnet-create public 192.168.1.0/24 --name public --allocation-pool start=192.168.1.240,end=192.168.1.245 --dns-nameserver 114.114.114.114 --gateway 192.168.1.1Created a new subnet:+-------------------+----------------------------------------------------+| Field             | Value                                              |+-------------------+----------------------------------------------------+| allocation_pools  | {"start": "192.168.1.240", "end": "192.168.1.245"} || cidr              | 192.168.1.0/24                                     || dns_nameservers   | 114.114.114.114                                    || enable_dhcp       | True                                               || gateway_ip        | 192.168.1.1                                        || host_routes       |                                                    || id                | 7642ab78-88f9-42ae-bd9b-00a307f8db6f               || ip_version        | 4                                                  || ipv6_address_mode |                                                    || ipv6_ra_mode      |                                                    || name              | public                                             || network_id        | e6aff6ca-0749-4c02-95e7-7ebdcd942880               || subnetpool_id     |                                                    || tenant_id         | b56d4cdbaea54df1b3cd3b769ce896a0                   |+-------------------+----------------------------------------------------+

1.2.1创建网络(网络选择2: Self-service networks )

1.2.2必须先创建Provider networks网络,参考1.1.1

1.2.3创建private project network

[root@comtroller1 ~]# source demo-openrc.sh [root@comtroller1 ~]# neutron net-create privateCreated a new network:+-----------------------+--------------------------------------+| Field                 | Value                                |+-----------------------+--------------------------------------+| admin_state_up        | True                                 || id                    | 101f76c1-af60-44c4-a2e4-0808cb8c8237 || mtu                   | 0                                    || name                  | private                              || port_security_enabled | True                                 || router:external       | False                                || shared                | False                                || status                | ACTIVE                               || subnets               |                                      || tenant_id             | db6bcde12cc947119ecab8c211fa4f35     |+-----------------------+--------------------------------------+[root@comtroller1 ~]# vi /etc/neutron/plugins/ml2/ml2_conf.ini[ml2]tenant_network_types = vxlan[ml2_type_vxlan]vni_ranges = 1:1000[root@comtroller1 ~]# neutron subnet-create private 172.16.1.0/24 --name private --dns-nameserver 114.114.114.114 --gateway 172.16.1.1Created a new subnet:+-------------------+------------------------------------------------+| Field             | Value                                          |+-------------------+------------------------------------------------+| allocation_pools  | {"start": "172.16.1.2", "end": "172.16.1.254"} || cidr              | 172.16.1.0/24                                  || dns_nameservers   | 114.114.114.114                                || enable_dhcp       | True                                           || gateway_ip        | 172.16.1.1                                     || host_routes       |                                                || id                | 9941dd35-5ec6-497b-aca5-f99d0702501a           || ip_version        | 4                                              || ipv6_address_mode |                                                || ipv6_ra_mode      |                                                || name              | private                                        || network_id        | 101f76c1-af60-44c4-a2e4-0808cb8c8237           || subnetpool_id     |                                                || tenant_id         | db6bcde12cc947119ecab8c211fa4f35               |+-------------------+------------------------------------------------+

1.2.4创建路由

[root@comtroller1 ~]# source admin-openrc.sh [root@comtroller1 ~]# neutron net-update public --router:externalUpdated network: public[root@comtroller1 ~]# source demo-openrc.sh [root@comtroller1 ~]# neutron router-create routerCreated a new router:+-----------------------+--------------------------------------+| Field                 | Value                                |+-----------------------+--------------------------------------+| admin_state_up        | True                                 || external_gateway_info |                                      || id                    | 6562f208-7cd3-4202-8c1e-f7469f400a24 || name                  | router                               || routes                |                                      || status                | ACTIVE                               || tenant_id             | db6bcde12cc947119ecab8c211fa4f35     |+-----------------------+--------------------------------------+[root@comtroller1 ~]# neutron router-interface-add router privateAdded interface e8297ca2-b59e-4e3d-954c-a467975e263c to router router.[root@comtroller1 ~]# neutron router-gateway-set router publicSet gateway for router router

1.2.5验证操作

[root@comtroller1 ~]# source admin-openrc.sh [root@comtroller1 ~]# ip netnsqrouter-6562f208-7cd3-4202-8c1e-f7469f400a24 (id: 2)qdhcp-101f76c1-af60-44c4-a2e4-0808cb8c8237 (id: 1)qdhcp-c3367ab4-521c-4f5d-97d3-1e2b12ddb229 (id: 0)[root@comtroller1 ~]# neutron router-port-list router+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+| id                                   | name | mac_address       | fixed_ips                                                                            |+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+| 824796a5-b542-4c61-82e0-49f73f731dfa |      | fa:16:3e:81:30:be | {"subnet_id": "5116aa85-6ce6-4ac8-a0c4-0b7376fec026", "ip_address": "192.168.1.241"} || e8297ca2-b59e-4e3d-954c-a467975e263c |      | fa:16:3e:f4:8e:60 | {"subnet_id": "9941dd35-5ec6-497b-aca5-f99d0702501a", "ip_address": "172.16.1.1"}    |+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+[root@comtroller1 ~]# ping 192.168.1.241PING 192.168.1.241 (192.168.1.241) 56(84) bytes of data.64 bytes from 192.168.1.241: icmp_seq=1 ttl=63 time=0.897 ms64 bytes from 192.168.1.241: icmp_seq=2 ttl=63 time=0.716 ms^C--- 192.168.1.241 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1000msrtt min/avg/max/mdev = 0.716/0.806/0.897/0.094 ms

2.1生成密钥对

[root@comtroller1 ~]# source demo-openrc.sh [root@comtroller1 ~]# ssh-keygen -q -N ""Enter file in which to save the key (/root/.ssh/id_rsa): [root@comtroller1 ~]# nova keypair-add --pub-key ~/.ssh/id_rsa.pub mykey[root@comtroller1 ~]# nova keypair-list+-------+-------------------------------------------------+| Name  | Fingerprint                                     |+-------+-------------------------------------------------+| mykey | cb:aa:6f:99:38:4f:1e:1a:4d:b8:18:be:f0:c3:b6:40 |+-------+-------------------------------------------------+

3.1添加安全组规则

[root@comtroller1 ~]# nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0  #允许PING+-------------+-----------+---------+-----------+--------------+| IP Protocol | From Port | To Port | IP Range  | Source Group |+-------------+-----------+---------+-----------+--------------+| icmp        | -1        | -1      | 0.0.0.0/0 |              |+-------------+-----------+---------+-----------+--------------+[root@comtroller1 ~]# nova secgroup-add-rule default tcp 22 22 0.0.0.0/0   #允许SSH+-------------+-----------+---------+-----------+--------------+| IP Protocol | From Port | To Port | IP Range  | Source Group |+-------------+-----------+---------+-----------+--------------+| tcp         | 22        | 22      | 0.0.0.0/0 |              |+-------------+-----------+---------+-----------+--------------+

4.1.1针对网络架构一:Provider启动实例

4.1.2查看资源信息

[root@comtroller1 ~]# source demo-openrc.sh[root@comtroller1 ~]# 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      |+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+[root@comtroller1 ~]# nova p_w_picpath-list+--------------------------------------+--------+--------+--------+| ID                                   | Name   | Status | Server |+--------------------------------------+--------+--------+--------+| ba4fd4dd-7717-4e11-8a64-5b063882b0aa | cirros | ACTIVE |        |+--------------------------------------+--------+--------+--------+[root@comtroller1 ~]# neutron net-list+--------------------------------------+--------+-----------------------------------------------------+| id                                   | name   | subnets                                             |+--------------------------------------+--------+-----------------------------------------------------+| e6aff6ca-0749-4c02-95e7-7ebdcd942880 | public | 7642ab78-88f9-42ae-bd9b-00a307f8db6f 192.168.1.0/24 |+--------------------------------------+--------+-----------------------------------------------------+[root@comtroller1 ~]# nova secgroup-list+--------------------------------------+---------+------------------------+| Id                                   | Name    | Description            |+--------------------------------------+---------+------------------------+| 6a7eba84-0ec7-4898-ad79-1937cd0f86f1 | default | Default security group |+--------------------------------------+---------+------------------------+

4.1.3创建虚拟机实例

[root@comtroller1 ~]# nova boot --flavor m1.tiny --p_w_picpath cirros --nic net-id=e6aff6ca-0749-4c02-95e7-7ebdcd942880 --security-group default --key-name mykey public-instance+--------------------------------------+-----------------------------------------------+| 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                            | bLj4YU7Jtr43                                  || config_drive                         |                                               || created                              | 2016-07-14T09:21:58Z                          || flavor                               | m1.tiny (1)                                   || hostId                               |                                               || id                                   | 560dbae8-39a8-4454-80d1-1fcf119d8866          || p_w_picpath                                | cirros (ba4fd4dd-7717-4e11-8a64-5b063882b0aa) || key_name                             | mykey                                         || metadata                             | {}                                            || name                                 | public-instance                               || os-extended-volumes:volumes_attached | []                                            || progress                             | 0                                             || security_groups                      | default                                       || status                               | BUILD                                         || tenant_id                            | db6bcde12cc947119ecab8c211fa4f35              || updated                              | 2016-07-14T09:21:59Z                          || user_id                              | 3361e8c44fc94b63ac44049542129edc              |+--------------------------------------+-----------------------------------------------+

4.1.4查看实例状态

[root@comtroller1 ~]# nova list+--------------------------------------+-----------------+--------+------------+-------------+----------------------+| ID                                   | Name            | Status | Task State | Power State | Networks             |+--------------------------------------+-----------------+--------+------------+-------------+----------------------+| 560dbae8-39a8-4454-80d1-1fcf119d8866 | public-instance | ACTIVE | -          | Running     | public=192.168.1.241 |+--------------------------------------+-----------------+--------+------------+-------------+----------------------+

4.1.5使用浏览器访问虚拟机实例

[root@comtroller1 ~]# nova get-vnc-console public-instance novnc+-------+----------------------------------------------------------------------------------+| Type  | Url                                                                              |+-------+----------------------------------------------------------------------------------+| novnc | http://controller1:6080/vnc_auto.html?token=ddbcd055-5998-4fc5-96d6-4d5d47b49fd5 |+-------+----------------------------------------------------------------------------------+

4.1.6验证

PING测试

4.2.1针对网络架构二:Private启动实例

4.2.2查看资源信息

[root@comtroller1 ~]# source demo-openrc.sh[root@comtroller1 ~]# 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      |+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+[root@comtroller1 ~]# nova p_w_picpath-list+--------------------------------------+--------+--------+--------+| ID                                   | Name   | Status | Server |+--------------------------------------+--------+--------+--------+| ba4fd4dd-7717-4e11-8a64-5b063882b0aa | cirros | ACTIVE |        |+--------------------------------------+--------+--------+--------+[root@comtroller1 ~]# neutron net-list+--------------------------------------+---------+-----------------------------------------------------+| id                                   | name    | subnets                                             |+--------------------------------------+---------+-----------------------------------------------------+| c3367ab4-521c-4f5d-97d3-1e2b12ddb229 | public  | 5116aa85-6ce6-4ac8-a0c4-0b7376fec026 192.168.1.0/24 || 101f76c1-af60-44c4-a2e4-0808cb8c8237 | private | 9941dd35-5ec6-497b-aca5-f99d0702501a 172.16.1.0/24  |+--------------------------------------+---------+-----------------------------------------------------+[root@comtroller1 ~]# nova secgroup-list+--------------------------------------+---------+------------------------+| Id                                   | Name    | Description            |+--------------------------------------+---------+------------------------+| fb3e5884-efe2-4f9b-b2a8-a36ebfe5d305 | default | Default security group |+--------------------------------------+---------+------------------------+

4.2.3创建虚拟机实例

[root@comtroller1 ~]# nova boot --flavor m1.tiny --p_w_picpath cirros --nic net-id=101f76c1-af60-44c4-a2e4-0808cb8c8237 --security-group default --key-name mykey private-instance+--------------------------------------+-----------------------------------------------+| 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                            | D3XvDmthW2ea                                  || config_drive                         |                                               || created                              | 2016-07-18T03:16:56Z                          || flavor                               | m1.tiny (1)                                   || hostId                               |                                               || id                                   | c77b55d0-7982-40da-a5a4-c708fcee98fe          || p_w_picpath                                | cirros (ba4fd4dd-7717-4e11-8a64-5b063882b0aa) || key_name                             | mykey                                         || metadata                             | {}                                            || name                                 | private-instance                              || os-extended-volumes:volumes_attached | []                                            || progress                             | 0                                             || security_groups                      | default                                       || status                               | BUILD                                         || tenant_id                            | db6bcde12cc947119ecab8c211fa4f35              || updated                              | 2016-07-18T03:16:56Z                          || user_id                              | 3361e8c44fc94b63ac44049542129edc              |+--------------------------------------+-----------------------------------------------+

4.2.4查看实例状态

[root@comtroller1 ~]# nova list+--------------------------------------+------------------+--------+------------+-------------+----------+| ID                                   | Name             | Status | Task State | Power State | Networks |+--------------------------------------+------------------+--------+------------+-------------+----------+| 786f1e34-86d3-4496-b94e-48c33040df9e | private-instance | ERROR  | -          | NOSTATE     |          |+--------------------------------------+------------------+--------+------------+-------------+----------+

4.2.5删除虚拟机

[root@comtroller1 ~]# nova delete 786f1e34-86d3-4496-b94e-48c33040df9eRequest to delete server 786f1e34-86d3-4496-b94e-48c33040df9e has been accepted.

4.2.6使用浏览器访问虚拟机实例

[root@comtroller1 ~]# nova get-vnc-console private-instance novnc+-------+----------------------------------------------------------------------------------+| Type  | Url                                                                              |+-------+----------------------------------------------------------------------------------+| novnc | http://controller1:6080/vnc_auto.html?token=bec3110e-490f-41f5-9bff-bd970653bcc8 |+-------+----------------------------------------------------------------------------------+

4.2.7验证

PING测试

4.2.8远程访问实例

[root@comtroller1 ~]# neutron floatingip-create publicCreated a new floatingip:+---------------------+--------------------------------------+| Field               | Value                                |+---------------------+--------------------------------------+| fixed_ip_address    |                                      || floating_ip_address | 192.168.1.242                        || floating_network_id | 93be9f32-e1f2-4748-9198-cc4b1807006f || id                  | edd5224a-7dd8-412a-aedc-69bc000aefa2 || port_id             |                                      || router_id           |                                      || status              | DOWN                                 || tenant_id           | db6bcde12cc947119ecab8c211fa4f35     |+---------------------+--------------------------------------+[root@comtroller1 ~]# nova floating-ip-associate private-instance 192.168.1.242[root@comtroller1 ~]# nova list+--------------------------------------+------------------+--------+------------+-------------+-----------------------------------+| ID                                   | Name             | Status | Task State | Power State | Networks                          |+--------------------------------------+------------------+--------+------------+-------------+-----------------------------------+| 4aa43e3a-c963-4a53-b500-78fa6a6872c5 | private-instance | ACTIVE | -          | Running     | private=172.16.1.3, 192.168.1.242 |+--------------------------------------+------------------+--------+------------+-------------+-----------------------------------+[c:\~]$ ping 192.168.1.242Ping 192.168.1.242 (使用 32 位元組的資料):回覆自 192.168.1.242: 位元組=32 時間=1ms TTL=63回覆自 192.168.1.242: 位元組=32 時間<1ms TTL=63192.168.1.242 的 Ping 統計資料:    封包: 已傳送 = 2,已收到 = 2, 已遺失 = 0 (0% 遺失),大約的來回時間 (毫秒):    最小值 = 0ms,最大值 = 1ms,平均 = 0ms[c:\~]$ ssh cirros@192.168.1.242Connecting to 192.168.1.242:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]'.$ ip a1: lo: 
 mtu 16436 qdisc noqueue     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo    inet6 ::1/128 scope host        valid_lft forever preferred_lft forever2: eth0: 
 mtu 1450 qdisc pfifo_fast qlen 1000    link/ether fa:16:3e:bc:64:17 brd ff:ff:ff:ff:ff:ff    inet 172.16.1.3/24 brd 172.16.1.255 scope global eth0    inet6 fe80::f816:3eff:febc:6417/64 scope link        valid_lft forever preferred_lft forever