Think Deep,Work Lean

openstack实战之创建云主机(6)

Posted on By zack

创建网络

[root@linux-node1 ~]# openstack network create  --share --external \
  --provider-physical-network provider \
  --provider-network-type flat provider

创建子网

[root@linux-node1 ~]# openstack subnet create --network provider \
  --allocation-pool start=192.168.56.100,end=192.168.56.200 \
  --dns-nameserver 223.5.5.5  --gateway 192.168.56.2 \
  --subnet-range 192.168.56.0/24 provider-subnet

创建云主机类型

[root@linux-node1 ~]# openstack flavor create --id 0 --vcpus 1 --ram 64 --disk 1 m1.nano

创建密钥对

[root@linux-node1 ~]# source demo-openstack.sh
[root@linux-node1 ~]# ssh-keygen -q -N ""
[root@linux-node1 ~]# openstack keypair create --public-key ~/.ssh/id_rsa.pub mykey
[root@linux-node1 ~]# openstack keypair list

添加安全组规则

 [root@linux-node1 ~]# openstack security group rule create --proto icmp default
 [root@linux-node1 ~]# openstack security group rule create --proto tcp --dst-port 22 default

启动实例

[root@linux-node1 ~]# source demo-openstack.sh
[root@linux-node1 ~]# openstack flavor list

1.查看可用的镜像

[root@linux-node1 ~]# openstack image list
| ID                                   | Name   | Status |
+--------------------------------------+--------+--------+
| 3a4552b1-712b-47f8-862e-c49c45d4bf41 | cirros | active |
| a621e740-22a4-458f-b532-92bf0caf6ef9 | cirros | active |

2.查看可用的网络

[root@linux-node1 ~]# openstack network list
+--------------------------------------+----------+--------------------------------------+
| ID                                   | Name     | Subnets                              |
+--------------------------------------+----------+--------------------------------------+
| a52fba78-4fd6-452d-a596-e2e8827ccbc5 | provider | 17516f96-1be0-487d-ac4e-bc52aff7373a |

3.查看可用的安全组

[root@linux-node1 ~]# openstack security group list
+--------------------------------------+---------+------------------------+----------------------------------
| ID                                   | Name    | Description            | Project                          
+--------------------------------------+---------+------------------------+----------------------------------
| aa6ad9cf-2376-4786-84b9-9f33670f5faf | default | Default security group | 1edd8c6320074fd6b635841d355050ea

4.创建虚拟机

[root@linux-node1 ~]# openstack server create --flavor m1.nano --image a621e740-22a4-458f-b532-92bf0caf6ef9 --nic net-id=a52fba78-4fd6-452d-a596-e2e8827ccbc5 --security-group default --key-name mykey demo-instance
#注意指定网络的时候需要使用ID,而不是名称
| Field                       | Value                                         |
+-----------------------------+-----------------------------------------------+
| OS-DCF:diskConfig           | MANUAL                                        |
| OS-EXT-AZ:availability_zone |                                               |
| OS-EXT-STS:power_state      | NOSTATE                                       |
| 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                   | dRQW6cgctQyW                                  |
| config_drive                |                                               |
| created                     | 2018-05-15T08:27:49Z                          |
| flavor                      | m1.nano (0)                                   |
| hostId                      |                                               |
| id                          | 9b231bf1-5c49-4180-ba55-8881d18a9903          |
| image                       | cirros (a621e740-22a4-458f-b532-92bf0caf6ef9) |
| key_name                    | mykey                                         |
| name                        | demo-instance                                 |
| progress                    | 0                                             |
| project_id                  | 1edd8c6320074fd6b635841d355050ea              |
| properties                  |                                               |
| security_groups             | name='aa6ad9cf-2376-4786-84b9-9f33670f5faf'   |
| status                      | BUILD                                         |
| updated                     | 2018-05-15T08:27:52Z                          |
| user_id                     | 246c61ba4f294608aa8316ff895ec9a0              |
| volumes_attached            |  

注意,这是通过network来创建虚机,若是要通过port来创建虚机,对应的就是 --nic port-id=port-uuid

5.查看虚拟机

[root@linux-node1 ~]# openstack server list
| ID                                   | Name          | Status | Networks                | Image  | Flavor  |
+--------------------------------------+---------------+--------+-------------------------+--------+---------+
| 9b231bf1-5c49-4180-ba55-8881d18a9903 | demo-instance | ACTIVE | provider=192.168.56.108 | cirros | m1.nano |
[root@linux-node1 ~]# openstack console url show demo-instance
| Field | Value                                                                              |
+-------+------------------------------------------------------------------------------------+
| type  | novnc                                                                              |
| url   | http://192.168.56.11:6080/vnc_auto.html?token=fa306e12-89a1-4062-a0b8-82afc5f581ad