一、创建虚拟网络

第一次创建虚拟机,使用命令行来创建


在控制节点上,加载 admin 凭证来获取管理员能执行的命令访问权限

[root@linux-node1 ~]# source admin-openstack.sh

创建提供者网络

[root@linux-node1 ~]# neutron net-create --shared --provider:physical_network public --provider:network_type flat public-net
Created a new network:
+---------------------------+--------------------------------------+
| Field                     | Value                                |
+---------------------------+--------------------------------------+
| admin_state_up            | True                                 |
| availability_zone_hints   |                                      |
| availability_zones        |                                      |
| created_at                | 2016-11-09T17:03:20                  |
| description               |                                      |
| id                        | 2cbfb54c-b760-41e1-ba69-83131220463b |
| ipv4_address_scope        |                                      |
| ipv6_address_scope        |                                      |
| mtu                       | 1500                                 |
| name                      | public-net                           |
| port_security_enabled     | True                                 |
| provider:network_type     | flat                                 |
| provider:physical_network | public                               |
| provider:segmentation_id  |                                      |
| router:external           | False                                |
| shared                    | True                                 |
| status                    | ACTIVE                               |
| subnets                   |                                      |
| tags                      |                                      |
| tenant_id                 | 53f72af1420a4d098d48f2c82d7e9ec7     |
| updated_at                | 2016-11-09T17:03:20                  |
+---------------------------+--------------------------------------+
#参数解释
#--provider:physical_network网络类型选项,在配置文件中设置的为public,public映射的是eth0端口
#--provider:network_type 提供者的网络类型flat
#public-net是提供者网络的名称,按照自己的意愿定义

查看已经创建的网络

[root@linux-node1 ~]# neutron net-list
+--------------------------------------+------------+---------+
| id                                   | name       | subnets |
+--------------------------------------+------------+---------+
| 2cbfb54c-b760-41e1-ba69-83131220463b | public-net |         |
+--------------------------------------+------------+---------+

创建子网

[root@linux-node1 ~]# neutron subnet-create --name public-subnet --allocation-pool start=192.168.56.100,end=192.168.56.200 --dns-nameserver 223.5.5.5 --gateway 192.168.56.2 public-net 192.168.56.0/24
Created a new subnet:
+-------------------+------------------------------------------------------+
| Field             | Value                                                |
+-------------------+------------------------------------------------------+
| allocation_pools  | {"start": "192.168.56.100", "end": "192.168.56.200"} |
| cidr              | 192.168.56.0/24                                      |
| created_at        | 2016-11-10T02:56:27                                  |
| description       |                                                      |
| dns_nameservers   | 223.5.5.5                                            |
| enable_dhcp       | True                                                 |
| gateway_ip        | 192.168.56.2                                         |
| host_routes       |                                                      |
| id                | 6e217cbe-3201-4b47-8765-84ba932afa93                 |
| ip_version        | 4                                                    |
| ipv6_address_mode |                                                      |
| ipv6_ra_mode      |                                                      |
| name              | public-subnet                                        |
| network_id        | 2cbfb54c-b760-41e1-ba69-83131220463b                 |
| subnetpool_id     |                                                      |
| tenant_id         | 53f72af1420a4d098d48f2c82d7e9ec7                     |
| updated_at        | 2016-11-10T02:56:27                                  |
+-------------------+------------------------------------------------------+

查看创建的子网

[root@linux-node1 ~]# neutron subnet-list
+--------------------------------------+---------------+-----------------+------------------------------------------------------+
| id                                   | name          | cidr            | allocation_pools                                     |
+--------------------------------------+---------------+-----------------+------------------------------------------------------+
| 6e217cbe-3201-4b47-8765-84ba932afa93 | public-subnet | 192.168.56.0/24 | {"start": "192.168.56.100", "end": "192.168.56.200"} |
+--------------------------------------+---------------+-----------------+------------------------------------------------------+

再次执行neutron net-list,可以看到创建的网络与子网已经关联在一起

[root@linux-node1 ~]# neutron net-list
+--------------------------------------+------------+------------------------------------------------------+
| id                                   | name       | subnets                                              |
+--------------------------------------+------------+------------------------------------------------------+
| 2cbfb54c-b760-41e1-ba69-83131220463b | public-net | 6e217cbe-3201-4b47-8765-84ba932afa93 192.168.56.0/24 |
+--------------------------------------+------------+------------------------------------------------------+

二、创建m1.nano规格的主机

1、创建一个nano规格的主机

默认的最小规格的主机需要512 MB内存。对于环境中计算节点内存不足4 GB的,我们推荐创建只需要64 MB的m1.nano规格的主机。若单纯为了测试的目的,请使用m1.nano规格的主机来加载CirrOS镜像

[root@linux-node1 ~]# openstack flavor create --id 0 --vcpus 1 --ram 64 --disk 1 m1.nano
+----------------------------+---------+
| Field                      | Value   |
+----------------------------+---------+
| OS-FLV-DISABLED:disabled   | False   |
| OS-FLV-EXT-DATA:ephemeral  | 0       |
| disk                       | 1       |
| id                         | 0       |
| name                       | m1.nano |
| os-flavor-access:is_public | True    |
| ram                        | 64      |
| rxtx_factor                | 1.0     |
| swap                       |         |
| vcpus                      | 1       |
+----------------------------+---------+
#--id,主机类型的ID
#--vcpus,创建主机的cpu的数量
#--ram,内存,默认单位为M
#--disk,硬盘,默认单位为G
[root@linux-node1 ~]# openstack flavor list
+----+-----------+-------+------+-----------+-------+-----------+
| ID | Name      |   RAM | Disk | Ephemeral | VCPUs | Is Public |
+----+-----------+-------+------+-----------+-------+-----------+
| 0  | m1.nano   |    64 |    1 |         0 |     1 | True      |
| 1  | m1.tiny   |   512 |    1 |         0 |     1 | True      |
| 2  | m1.small  |  2048 |   20 |         0 |     1 | True      |
| 3  | m1.medium |  4096 |   40 |         0 |     2 | True      |
| 4  | m1.large  |  8192 |   80 |         0 |     4 | True      |
| 5  | m1.xlarge | 16384 |  160 |         0 |     8 | True      |
+----+-----------+-------+------+-----------+-------+-----------+
#ID为0的是刚才创建的,生产环境中可以依据自己的需要进行创建

2、生成一个键值对

(1)导入demo用户的环境变量

[root@linux-node1 ~]# cat demo-openstack.sh 
export OS_PROJECT_DOMAIN_NAME=default
export OS_USER_DOMAIN_NAME=default
export OS_PROJECT_NAME=demo
export OS_USERNAME=demo
export OS_PASSWORD=DEMO_PASS
export OS_AUTH_URL=http://controller:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2
[root@linux-node1 ~]# source demo-openstack.sh

生成密钥对

[root@linux-node1 ~]# ssh-keygen -q -N ""
Enter file in which to save the key (/root/.ssh/id_rsa):

在openstack上创建密钥对

[root@linux-node1 ~]# openstack keypair create --public-key ~/.ssh/id_rsa.pub mykey
+-------------+-------------------------------------------------+
| Field       | Value                                           |
+-------------+-------------------------------------------------+
| fingerprint | 76:e0:a8:ff:ec:20:43:40:5d:42:0a:f1:90:1f:78:38 |
| name        | mykey                                           |
| user_id     | 6762a6adffd140b1906bbe69dbf42518                |
+-------------+-------------------------------------------------+

验证密钥对是否创建成功

[root@linux-node1 ~]# openstack keypair list
+-------+-------------------------------------------------+
| Name  | Fingerprint                                     |
+-------+-------------------------------------------------+
| mykey | 76:e0:a8:ff:ec:20:43:40:5d:42:0a:f1:90:1f:78:38 |
+-------+-------------------------------------------------+

3、增加安全组规则

默认情况下, default安全组适用于所有实例并且包括拒绝远程访问实例的防火墙规则。对诸如CirrOS这样的Linux镜像,至少允许ICMP (ping) 和安全shell(SSH)规则。

添加规则到default安全组

允许icmp
[root@linux-node1 ~]# openstack security group rule create --proto icmp default
+-----------------------+--------------------------------------+
| Field                 | Value                                |
+-----------------------+--------------------------------------+
| id                    | ca56bc9b-5f12-43ea-b72f-4638cfb55094 |
| ip_protocol           | icmp                                 |
| ip_range              | 0.0.0.0/0                            |
| parent_group_id       | a6670191-a77f-44f1-86b1-a617a5c7547d |
| port_range            |                                      |
| remote_security_group |                                      |
+-----------------------+--------------------------------------+
允许ssh连接
[root@linux-node1 ~]# openstack security group rule create --proto tcp --dst-port 22 default
+-----------------------+--------------------------------------+
| Field                 | Value                                |
+-----------------------+--------------------------------------+
| id                    | 5457a51f-a177-4db3-bac5-5b8d1cfb939d |
| ip_protocol           | tcp                                  |
| ip_range              | 0.0.0.0/0                            |
| parent_group_id       | a6670191-a77f-44f1-86b1-a617a5c7547d |
| port_range            | 22:22                                |
| remote_security_group |                                      |
+-----------------------+--------------------------------------+

4、创建实例

[root@linux-node1 ~]# openstack server create --flavor m1.nano --image cirros   --nic net-id=2cbfb54c-b760-41e1-ba69-83131220463b --security-group default   --key-name mykey provider-instance
+--------------------------------------+-----------------------------------------------+
| Field                                | 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               | None                                          |
| OS-SRV-USG:terminated_at             | None                                          |
| accessIPv4                           |                                               |
| accessIPv6                           |                                               |
| addresses                            |                                               |
| adminPass                            | Q9KXwLniQpoM                                  |
| config_drive                         |                                               |
| created                              | 2016-11-10T06:13:27Z                          |
| flavor                               | m1.nano (0)                                   |
| hostId                               |                                               |
| id                                   | 0006405c-db45-42b4-a65a-d4a2041e2e88          |
| image                                | cirros (29d99654-ba91-4d04-8808-e1b8d16861fb) |
| key_name                             | mykey                                         |
| name                                 | provider-instance                             |
| os-extended-volumes:volumes_attached | []                                            |
| progress                             | 0                                             |
| project_id                           | 81e76ab533b14b448b1c6394bc5e4d86              |
| properties                           |                                               |
| security_groups                      | [{u'name': u'default'}]                       |
| status                               | BUILD                                         |
| updated                              | 2016-11-10T06:13:28Z                          |
| user_id                              | 6762a6adffd140b1906bbe69dbf42518              |
+--------------------------------------+-----------------------------------------------+
#--nic 这里使用网络的ID

查看已创建的主机

[root@linux-node1 ~]# openstack server list
+--------------------------------------+-------------------+--------+---------------------------+
| ID                                   | Name              | Status | Networks                  |
+--------------------------------------+-------------------+--------+---------------------------+
| 0006405c-db45-42b4-a65a-d4a2041e2e88 | provider-instance | ACTIVE | public-net=192.168.56.101 |
+--------------------------------------+-------------------+--------+---------------------------+

5、连接虚拟机

默认的用户名是cirros,密码是cubswin:)

[root@linux-node2 ~]# ssh cirros@192.168.56.101
cirros@192.168.56.101's password: 
$ sudo su -
# ifconfig
eth0      Link encap:Ethernet  HWaddr FA:16:3E:C5:10:81  
          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fec5:1081/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:133 errors:0 dropped:0 overruns:0 frame:0
          TX packets:146 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:16003 (15.6 KiB)  TX bytes:15456 (15.0 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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)