OpenStack实践系列⑥构建虚拟机实例
四、创建一台虚拟机
图解网络,并创建一个真实的桥接网络
创建一个单一扁平网络(名字:flat),网络类型为flat,网络适共享的(share),网络提供者:physnet1,它是和eth0关联起来的(
/etc/neutron/plugins/ml2/linuxbridge_agent.ini配置文件中配置项中的内容physical_interface_mappings = physnet1:eth0) [root@node1 ~]# source admin-openrc.sh [root@node1 ~]# neutron net-create flat --shared --provider:physical_network physnet1 --provider:network_type flat
Created a new network:
+---------------------------+--------------------------------------+
| Field | Value |
+---------------------------+--------------------------------------+
| admin_state_up | True |
| id | 2cd04d33-60a1-4d16-af09-19ad71f56f75 |
| mtu | |
| 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 | eb5d7705ed7147e6beb4cd83a9f70a4a |
+---------------------------+--------------------------------------+
对上一步创建的网络创建一个子网,名字为:subnet-create flat,设置dns和网关
[root@node1 ~]# neutron subnet-create flat 192.168.3.0/ --name flat-subnet --allocation-pool start=192.168.3.100,end=192.168.3.200 --dns-nameserver 192.168.1.13 --gateway 192.168.3.1
Created a new subnet:
+-------------------+----------------------------------------------------+
| Field | Value |
+-------------------+----------------------------------------------------+
| allocation_pools | {"start": "192.168.3.100", "end": "192.168.3.200"} |
| cidr | 192.168.3.0/ |
| dns_nameservers | 192.168.1.13 |
| enable_dhcp | True |
| gateway_ip | 192.168.3.1 |
| host_routes | |
| id | c2bc3265-178d-48d9-bef9-0c3544d1b0c3 |
| ip_version | |
| ipv6_address_mode | |
| ipv6_ra_mode | |
| name | flat-subnet |
| network_id | 2cd04d33-60a1-4d16-af09-19ad71f56f75 |
| subnetpool_id | |
| tenant_id | eb5d7705ed7147e6beb4cd83a9f70a4a |
+-------------------+----------------------------------------------------+
查看创建的网络和子网
[root@node1 ~]# neutron net-list
+--------------------------------------+------+-----------------------------------------------------+
| id | name | subnets |
+--------------------------------------+------+-----------------------------------------------------+
| 2cd04d33-60a1-4d16-af09-19ad71f56f75 | flat | c2bc3265-178d-48d9-bef9-0c3544d1b0c3 192.168.3.0/ |
+--------------------------------------+------+-----------------------------------------------------+
注:创建虚拟机之前,由于一个网络下不能存在多个dhcp,所以一定关闭其他的dhcp选项
下面开始正式创建虚拟机,为了可以连上所创建的虚拟机,在这里要创建一对公钥和私钥,并添加到openstack中
[root@node1 ~]# source demo-openrc.sh
[root@node1 ~]# ssh-keygen -q -N ""
Enter file in which to save the key (/root/.ssh/id_rsa):
[root@node1 ~]# ls .ssh/
id_rsa id_rsa.pub [root@node1 ~]# nova keypair-add --pub-key .ssh/id_rsa.pub mykey
[root@node1 ~]# nova keypair-list
+-------+-------------------------------------------------+
| Name | Fingerprint |
+-------+-------------------------------------------------+
| mykey | :b2:ae::3c::c4:::d6::1d:b5:bc:4b:4d |
+-------+-------------------------------------------------+
[root@node1 ~]# ls .ssh/
id_rsa id_rsa.pub
创建一个安全组,打开icmp和开放22端口
[root@node1 ~]# nova secgroup-add-rule default icmp - - 0.0.0.0/
+-------------+-----------+---------+-----------+--------------+
| IP Protocol | From Port | To Port | IP Range | Source Group |
+-------------+-----------+---------+-----------+--------------+
| icmp | - | - | 0.0.0.0/ | |
+-------------+-----------+---------+-----------+--------------+
[root@node1 ~]# nova secgroup-add-rule default tcp 0.0.0.0/
+-------------+-----------+---------+-----------+--------------+
| IP Protocol | From Port | To Port | IP Range | Source Group |
+-------------+-----------+---------+-----------+--------------+
| tcp | | | 0.0.0.0/ | |
+-------------+-----------+---------+-----------+--------------+
创建虚拟机之前要进行的确认虚拟机类型flavor(相当于EC2的intance的type)、需要的镜像(EC2的AMI),需要的网络(EC2的VPC),安全组(EC2的sg)
列出虚拟机可选类型
[root@node1 ~]# nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| | m1.tiny | | | | | | 1.0 | True |
| | m1.small | | | | | | 1.0 | True |
| | m1.medium | | | | | | 1.0 | True |
| | m1.large | | | | | | 1.0 | True |
| | m1.xlarge | | | | | | 1.0 | True |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
列出可用镜像
[root@node1 ~]# nova image-list
+--------------------------------------+--------+--------+--------+
| ID | Name | Status | Server |
+--------------------------------------+--------+--------+--------+
| 6a806538---b79d-d69ea7bc478c | cirros | ACTIVE | |
+--------------------------------------+--------+--------+--------+
列出可用网络
[root@node1 ~]# neutron net-list
+--------------------------------------+------+-----------------------------------------------------+
| id | name | subnets |
+--------------------------------------+------+-----------------------------------------------------+
| 2cd04d33-60a1-4d16-af09-19ad71f56f75 | flat | c2bc3265-178d-48d9-bef9-0c3544d1b0c3 192.168.3.0/ |
+--------------------------------------+------+-----------------------------------------------------+
列出可用安全组
[root@node1 ~]# nova secgroup-list
+--------------------------------------+---------+------------------------+
| Id | Name | Description |
+--------------------------------------+---------+------------------------+
| cf68eabe--4bd4--4a0ae7295de0 | default | Default security group |
+--------------------------------------+---------+------------------------+
创建一台虚拟机,类型为m1.tiny,镜像为cirros(之前wget下载的),网络id为neutron net-list出来的,安全组就是默认的,选择刚开的创建的key-pair,虚拟机的名字为hello-instance
[root@node1 ~]# nova boot --flavor m1.tiny --image cirros --nic net-id=2cd04d33-60a1-4d16-af09-19ad71f56f75 --security-group default --key-name mykey hello-instance
+--------------------------------------+-----------------------------------------------+
| Property | Value |
+--------------------------------------+-----------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | |
| OS-EXT-SRV-ATTR:host | - |
| OS-EXT-SRV-ATTR:hypervisor_hostname | - |
| OS-EXT-SRV-ATTR:instance_name | instance- |
| OS-EXT-STS:power_state | |
| 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 | wsNS4n8iKLoB |
| config_drive | |
| created | --05T02::40Z |
| flavor | m1.tiny () |
| hostId | |
| id | 3a995e89-4d71-4f70-9ae6-de82b65ebb74 |
| image | cirros (6a806538---b79d-d69ea7bc478c) |
| key_name | mykey |
| metadata | {} |
| name | hello-instance |
| os-extended-volumes:volumes_attached | [] |
| progress | |
| security_groups | default |
| status | BUILD |
| tenant_id | eb5d7705ed7147e6beb4cd83a9f70a4a |
| updated | --05T02::40Z |
| user_id | 4f71615a44c8422cbc034323d0d4c9a7 |
+--------------------------------------+-----------------------------------------------+
查看虚拟机创建状态,状态为ACTIVE那台虚拟机已经成功创建
[root@node1 ~]# nova list
+--------------------------------------+----------------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+----------------+--------+------------+-------------+----------+
| b6ba588b-494d--ac8e-5c3978ba9150 | hello-instance | BUILD | spawning | NOSTATE | |
+--------------------------------------+----------------+--------+------------+-------------+----------+
[root@node1 ~]# nova list
+--------------------------------------+----------------+--------+------------+-------------+--------------------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+----------------+--------+------------+-------------+--------------------+
| b6ba588b-494d--ac8e-5c3978ba9150 | hello-instance | ACTIVE | - | Running | flat=192.168.3.102 |
+--------------------------------------+----------------+--------+------------+-------------+--------------------+
ssh连接虚拟机并简单测试
[root@node1 ~]# ssh cirros@192.168.3.102
$ ls
$ whoami
cirros $ ifconfig
eth0 Link encap:Ethernet HWaddr FA::3E::1A:
inet addr:192.168.3.102 Bcast:192.168.3.255 Mask:255.255.255.0
inet6 addr: fe80::f816:3eff:fe40:1a59/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (185.2 KiB) TX bytes: (27.6 KiB) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 B) TX bytes: (0.0 B)
通过vnc生成URL在web界面上链接虚拟机
[root@node1 ~]# nova get-vnc-console hello-instance novnc
+-------+------------------------------------------------------------------------------------+
| Type | Url |
+-------+------------------------------------------------------------------------------------+
| novnc | http://192.168.3.199:6080/vnc_auto.html?token=a9483376-e7d1-4f12-8c82-72ee861afd15 |
+-------+------------------------------------------------------------------------------------+