首页 > 代码库 > 【openstack N版】——创建云主机
【openstack N版】——创建云主机
一.启动实例
1.1 已准备服务介绍
- MySql:为各个服务提供数据存储。
- RabbitMQ:为各个服务之间通信提供交通枢纽。
- keystone:为各个服务之间通信提供认证和服务注册。
- Glance:为虚拟机提供镜像管理。
- Nova:为虚拟机提供计算资源。
- Neutron:为虚拟机提供网络资源。
1.2 网络(flat)
1.2.1创建虚拟网络
1 #share 允许所有项目使用虚拟网络 2 [root@linux-node1 ~]# openstack network create --share 3 --provider-physical-network public 4 --provider-network-type flat public
+---------------------------+--------------------------------------+
| Field | Value |
+---------------------------+--------------------------------------+
| admin_state_up | UP |
| availability_zone_hints | |
| availability_zones | |
| created_at | 2017-03-01T03:10:38Z |
| description | |
| headers | |
| id | 0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc |
| ipv4_address_scope | None |
| ipv6_address_scope | None |
| mtu | 1500 |
| name | public |
| port_security_enabled | True |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| provider:network_type | flat |
| provider:physical_network | public |
| provider:segmentation_id | None |
| revision_number | 3 |
| router:external | Internal |
| shared | True |
| status | ACTIVE |
| subnets | |
| tags | [] |
| updated_at | 2017-03-01T03:10:38Z |
+---------------------------+--------------------------------------+
1.2.2查看已创建网络
1 #新版本命令 2 [root@linux-node1 ~]# openstack network list 3 #老版本命令 4 [root@linux-node1 ~]# neutron net-list 5 #结果都是一样的
+--------------------------------------+--------+---------+
| ID | Name | Subnets |
+--------------------------------------+--------+---------+
| 0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc | public | |
+--------------------------------------+--------+---------+
1.2.3创建子网
1 #创建子网
2 [root@linux-node1 ~]# openstack subnet create --network public 3 --allocation-pool start=192.168.56.100,end=192.168.56.200 4 --dns-nameserver 192.168.56.2 --gateway 192.168.56.2 5 --subnet-range 192.168.56.0/24 public-subnet
6 #注解
7 public:是之前配置文件中flat_networks自定义的名称
8 start:dhcp起始地址
9 end:dhcp终止地址
10 nameserver:DNS
11 gateway:网关
12 subnet:网段/子网
13 public-subnet:子网名称
+-------------------+--------------------------------------+
| Field | Value |
+-------------------+--------------------------------------+
| allocation_pools | 192.168.56.100-192.168.56.200 |
| cidr | 192.168.56.0/24 |
| created_at | 2017-03-01T03:12:36Z |
| description | |
| dns_nameservers | 192.168.56.2 |
| enable_dhcp | True |
| gateway_ip | 192.168.56.2 |
| headers | |
| host_routes | |
| id | 3b307841-211f-4c8e-91f5-a9f051bb1248 |
| ip_version | 4 |
| ipv6_address_mode | None |
| ipv6_ra_mode | None |
| name | public-subnet |
| network_id | 0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| revision_number | 2 |
| service_types | [] |
| subnetpool_id | None |
| updated_at | 2017-03-01T03:12:36Z |
+-------------------+--------------------------------------+
1.2.4检查子网
1 #新版本命令 2 [root@linux-node1 ~]# openstack subnet list
+--------------------------------------+---------------+--------------------------------------+-----------------+
| ID | Name | Network | Subnet |
+--------------------------------------+---------------+--------------------------------------+-----------------+
| 3b307841-211f-4c8e-91f5-a9f051bb1248 | public-subnet | 0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc | 192.168.56.0/24 |
+--------------------------------------+---------------+--------------------------------------+-----------------+ 3 #老版本命令 4 [root@linux-node1 ~]# neutron subnet-list
+--------------------------------------+---------------+-----------------+------------------------------------------------------+
| id | name | cidr | allocation_pools |
+--------------------------------------+---------------+-----------------+------------------------------------------------------+
| 3b307841-211f-4c8e-91f5-a9f051bb1248 | public-subnet | 192.168.56.0/24 | {"start": "192.168.56.100", "end": "192.168.56.200"} |
+--------------------------------------+---------------+-----------------+------------------------------------------------------+
1.3创建云主机类型
1 #先创建一个小的云主机类型,给官方提供镜像做测试 2 [root@linux-node1 ~]# openstack flavor create --id 0 --vcpus 1 --ram 64 --disk 1 m1.nano 3 #注解 4 id:主机类型ID 5 vcpus:虚拟cpu数量 6 ram:虚拟内存大小(MB) 7 disk:虚拟磁盘大小(GB) 8 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 |
| properties | |
| ram | 64 |
| rxtx_factor | 1.0 |
| swap | |
| vcpus | 1 |
+----------------------------+---------+
1.3.1查看云主机类型
1 [root@linux-node1 ~]# openstack flavor list
+----+---------+-----+------+-----------+-------+-----------+
| ID | Name | RAM | Disk | Ephemeral | VCPUs | Is Public |
+----+---------+-----+------+-----------+-------+-----------+
| 0 | m1.nano | 64 | 1 | 0 | 1 | True |
+----+---------+-----+------+-----------+-------+-----------+
1.4创建秘钥对
1 #创建ssh的秘钥对 2 [root@linux-node1 ~]# ssh-keygen -q -N "" 3 #直接敲回车 4 Enter file in which to save the key (/root/.ssh/id_rsa): 5 #用openstack使用此秘钥对 6 [root@linux-node1 ~]# openstack keypair create --public-key ~/.ssh/id_rsa.pub mykey
+-------------+-------------------------------------------------+
| Field | Value |
+-------------+-------------------------------------------------+
| fingerprint | f9:81:41:cc:48:f0:69:37:34:a6:52:08:6f:60:28:14 |
| name | mykey |
| user_id | 45b086bdc6b746c5b0bfd62f779fe6a5 |
+-------------+-------------------------------------------------+
1.4.1查看键值对
1 [root@linux-node1 ~]# openstack keypair list
+-------+-------------------------------------------------+
| Name | Fingerprint |
+-------+-------------------------------------------------+
| mykey | f9:81:41:cc:48:f0:69:37:34:a6:52:08:6f:60:28:14 |
+-------+-------------------------------------------------+
1.5创建安全组规则
1 #开启icmp协议,允许ping 2 [root@linux-node1 ~]# openstack security group rule create --proto icmp default
+-------------------+--------------------------------------+
| Field | Value |
+-------------------+--------------------------------------+
| created_at | 2017-03-01T03:49:22Z |
| description | |
| direction | ingress |
| ethertype | IPv4 |
| headers | |
| id | 044c66be-4db9-4f8c-8b6f-59c02c0c962c |
| port_range_max | None |
| port_range_min | None |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| protocol | icmp |
| remote_group_id | None |
| remote_ip_prefix | 0.0.0.0/0 |
| revision_number | 1 |
| security_group_id | a9c0f076-3e8a-4344-ad7b-e94b51798116 |
| updated_at | 2017-03-01T03:49:22Z |
+-------------------+--------------------------------------+ 3 #开启22端口,允许ssh 4 [root@linux-node1 ~]# openstack security group rule create --proto tcp --dst-port 22 default
+-------------------+--------------------------------------+
| Field | Value |
+-------------------+--------------------------------------+
| created_at | 2017-03-01T03:50:33Z |
| description | |
| direction | ingress |
| ethertype | IPv4 |
| headers | |
| id | 6b48884c-dfc5-444c-8fb2-4ec157945e4e |
| port_range_max | 22 |
| port_range_min | 22 |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| project_id | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
| protocol | tcp |
| remote_group_id | None |
| remote_ip_prefix | 0.0.0.0/0 |
| revision_number | 1 |
| security_group_id | a9c0f076-3e8a-4344-ad7b-e94b51798116 |
| updated_at | 2017-03-01T03:50:33Z |
+-------------------+--------------------------------------+
1.5.1查看安全组规则
1 [root@linux-node1 ~]# openstack security group list
+--------------------------------------+---------+------------------------+----------------------------------+
| ID | Name | Description | Project |
+--------------------------------------+---------+------------------------+----------------------------------+
| a9c0f076-3e8a-4344-ad7b-e94b51798116 | default | Default security group | d24a61dd3ecb43cb9e8a5f6539c6a2bb |
+--------------------------------------+---------+------------------------+----------------------------------+
1.6启动虚拟机
1 #启动之前首先要查看网络ID并记下 2 [root@linux-node1 ~]# openstack network list
+--------------------------------------+--------+--------------------------------------+
| ID | Name | Subnets |
+--------------------------------------+--------+--------------------------------------+
| 0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc | public | 3b307841-211f-4c8e-91f5-a9f051bb1248 |
+--------------------------------------+--------+--------------------------------------+ 3 #创建虚拟机,并启动 4 [root@linux-node1 ~]# openstack server create --flavor m1.nano --image cirros 5 --nic net-id=0cdd6694-d53d-4f85-b04a-bc3b6dfa6cbc --security-group default 6 --key-name mykey demo-instance 7 #注:如果只有一个网络,则不需要填写net-id,openstack会默认选择 8 #注解 9 flavor:云主机类型名称 10 image:镜像名称 11 nic net-id:网络ID 12 security-group:安全组 13 key-name:秘钥对 14 demo-instance:云主机名称
1.6.1查看云主机
1 [root@linux-node1 ~]# openstack server list
+--------------------------------------+---------------+--------+-----------------------+------------+
| ID | Name | Status | Networks | Image Name |
+--------------------------------------+---------------+--------+-----------------------+------------+
| 24202ab8-9f13-4555-b8e3-6ff799223e67 | demo-instance | ACTIVE | public=192.168.56.110 | cirros |
+--------------------------------------+---------------+--------+-----------------------+------------+
1.7获取VNC的url
1 [root@linux-node1 ~]# openstack console url show demo-instance
+-------+------------------------------------------------------------------------------------+
| Field | Value |
+-------+------------------------------------------------------------------------------------+
| type | novnc |
| url | http://192.168.56.11:6080/vnc_auto.html?token=844e8352-a2ca-4c3f-8690-753529d59b80 |
+-------+------------------------------------------------------------------------------------+
1.71访问VNC的url
打开浏览器输入:http://192.168.56.11:6080/vnc_auto.html?token=844e8352-a2ca-4c3f-8690-753529d59b80即可访问
注:以上操作,完全可以在dashboard上创建
二.创建云主机流程介绍
2.1创建云主机步骤
第一步:dashboard或者client带着用户名和密码请求keystone服务
第二步:keystone服务验证用户名和密码,验证成功后返回token给dashboard或者client
第三步:dashboard或者client带着keystone服务返回的token请求nova-api
第四步:nova-api带着token访问keystone服务验证token是否正确
第五步:keystone验证token正确后,返回结果给nova-api
第六步:nova-api将要创建的虚拟机资源信息写入DB
第七步:nova-api将要创建虚拟机的请求写入rabbitMQ消息队列,建立沟通枢纽
第八步:nova-scheduler发现消息队列中的创建虚拟机信息后访问DB
第九步:nova-scheduler从DB中获取到创建虚拟机所需资源并且计算,调度,决定将虚拟机创建在哪个计算节点上
第十步:将计算结果返回给消息队列
第十一步:nova-compute从消息队列中获取nova-scheduler计算的结果
第十二步:nova-compute通过nova-conductor(中间件)与DB交互来获取要创建的虚拟机所需要的资源信息
第十三步:nova-compute带着token去请求glance获取镜像资源,glance会将token交给keystone服务验证
第十四步:如果token验证成功,则将glance的镜像资源交给nova-compute
第十五步:nova-compute带着token去请求neutron获取网络资源,neutron会将token交给keystone服务验证
第十六步:如果token验证成功,则将neutron的网络资源交给nova-compute
第十七步:nova-compute带着token去请求cinder获取存储资源,cinder会将token交给keystone服务验证
第十八步:如果token验证成功,则将cinder的存储资源交给nova-compute
所有步骤都操作成功后,nova-compute就会将虚拟机创建在scheduler计算后的节点上。
2.2创建云主机流程图
【开源是一种精神,分享是一种美德】
— By GoodCook
— 笔者QQ:253097001
— 欢迎大家随时来交流
—原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。
【openstack N版】——创建云主机