OpenStack(1)-创建实例

目录

一、上传镜像

1.1 新建目录

1.2 上传至glance

1.3 查看镜像

二、新建实例

2.1 获取秘钥

2.2 新建实例

2.3 新建实例admin-vm

2.4 获取实例VNC的url

2.5 nova常用命令


一、上传镜像

1.1 新建目录

上传名为cirros-0.3.4-x86_64-disk.img的Linux测试镜像,cirros镜像通常用来做测试用,仅12M大小,方便测试。

新建用于存放镜像的目录,此处如果wget失败需要在宿主机下载后用xftp传入虚拟机/opt/images目录。

source /root/keystonerc_adminmkdir /opt/images
cd /opt/images
wget https://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img

1.2 上传至glance

 上传至glance服务,glance image-create --name cirros-0.3.4-x86_64 --disk-format qcow2 --container-format bare --file /opt/images/cirros-0.3.4-x86_64-disk.img  --visibility public --progress

 命令解释:

--name 镜像名称

--disk-format 镜像的磁盘格式,支持:ami, ari, aki, vhd, vmdk, raw, qcow2, vdi,iso 格式

--container-format 镜像容器格式,支持:ami, ari, aki, bare, ovf 格式

--file 镜像路径

--visibility 镜像是否可以被公共访问,此处设置为公共镜像

--progress 显示上传进度条

[root@openstack images(keystone_admin)]# glance image-create --name cirros-0.3.4-x86_64 --disk-format qcow2 --container-format bare --file /opt/images/cirros-0.3.4-x86_64-disk.img  --visibility public --progress
[=============================>] 100%
+------------------+--------------------------------------+
| Property         | Value                                |
+------------------+--------------------------------------+
| checksum         | ee1eca47dc88f4879d8a229cc70a07c6     |
| container_format | bare                                 |
| created_at       | 2023-06-07T13:38:26Z                 |
| disk_format      | qcow2                                |
| id               | 041c0a42-adc2-4694-b932-7f01ae198919 |
| min_disk         | 0                                    |
| min_ram          | 0                                    |
| name             | cirros-0.3.4-x86_64                  |
| owner            | 5de5c5079f4147a69f330e9ce20c7f1b     |
| protected        | False                                |
| size             | 13287936                             |
| status           | active                               |
| tags             | []                                   |
| updated_at       | 2023-06-07T13:38:26Z                 |
| virtual_size     | None                                 |
| visibility       | public                               |
+------------------+--------------------------------------+

1.3 查看镜像

使用 glance image-list 查看镜像列表或通过通过仪表盘查看,上传glance成功。不要使用 nova image-list,该命令已弃用。

[root@openstack ~(keystone_admin)]# glance image-list
+--------------------------------------+---------------------+
| ID                                   | Name                |
+--------------------------------------+---------------------+
| 5955b3dc-00b3-4dec-b21a-a9605275faf2 | cirros              |
| 041c0a42-adc2-4694-b932-7f01ae198919 | cirros-0.3.4-x86_64 |
+--------------------------------------+---------------------+

创建实例之前必须指定实例类型、镜像名、网络、安全组、密钥、实例名

查看可用实例类型 nova flavor-list ,默认五种类型

[root@openstack ~(keystone_admin)]# nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| ID | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | Description |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| 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      | -           |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+

查看可用网络 neutron net-list,默认一个公共网,一个内部网,外部网络表示与其他项目共用该网络。

[root@openstack ~(keystone_admin)]# neutron net-list
neutron CLI is deprecated and will be removed in the future. Use openstack CLI instead.
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+
| id                                   | name    | tenant_id                        | subnets                                            |
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+
| 39bf56a4-68a8-4f9c-af56-b7bf788a5c45 | public  | 5de5c5079f4147a69f330e9ce20c7f1b | 4bdac25f-a68e-4ef5-b638-c7f9500957ce 172.24.4.0/24 |
| 639cf33e-3ced-45a7-9d1e-542cd5aec015 | private | 2e506aef614242ee8f5fe824fdf74226 | 2034ab92-5372-47f5-89b0-88d17f9b26d2 10.0.0.0/24   |
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+

查看安全组 openstack security group list,不要使用 nova secgroup-list,该命令已弃用。

[root@openstack images(keystone_admin)]# openstack security group list
+--------------------------------------+---------+------------------------+----------------------------------+
| ID                                   | Name    | Description            | Project                          |
+--------------------------------------+---------+------------------------+----------------------------------+
| 18102e48-6a05-4901-ae9e-a0d43647a9f3 | default | Default security group |                                  |
| ec20a954-0572-428f-87a3-4916318fcadc | default | Default security group | 2e506aef614242ee8f5fe824fdf74226 |
| f18bcb15-5573-4dd1-8de5-c201b5472ceb | default | Default security group | 5de5c5079f4147a69f330e9ce20c7f1b |
+--------------------------------------+---------+------------------------+----------------------------------+

二、新建实例

2.1 获取秘钥

 获取密钥admin-key,获取后重启虚拟机秘钥仍然存在,秘钥跟账户是绑定的。

source /root/keystonerc_admin
nova keypair-add admin-key
openstack keypair list[root@openstack images(keystone_admin)]# openstack keypair list
+-----------+-------------------------------------------------+
| Name      | Fingerprint                                     |
+-----------+-------------------------------------------------+
| admin-key | c0:83:d6:02:35:39:a9:6e:a0:4e:46:5a:52:af:3d:ec |
+-----------+-------------------------------------------------+

查看nove列表,nova list,此时没有实例。

[root@openstack ~(keystone_admin)]# nova list
+----+------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+----+------+--------+------------+-------------+----------+
+----+------+--------+------------+-------------+----------+

2.2 新建实例

查看网络id,此处我使用的就是public网络,提前复制id号39bf56a4-68a8-4f9c-af56-b7bf788a5c45。

[root@openstack ~(keystone_admin)]# neutron net-list
neutron CLI is deprecated and will be removed in the future. Use openstack CLI instead.
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+
| id                                   | name    | tenant_id                        | subnets                                            |
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+
| 39bf56a4-68a8-4f9c-af56-b7bf788a5c45 | public  | 5de5c5079f4147a69f330e9ce20c7f1b | 4bdac25f-a68e-4ef5-b638-c7f9500957ce 172.24.4.0/24 |
| 639cf33e-3ced-45a7-9d1e-542cd5aec015 | private | 2e506aef614242ee8f5fe824fdf74226 | 2034ab92-5372-47f5-89b0-88d17f9b26d2 10.0.0.0/24   |
+--------------------------------------+---------+----------------------------------+----------------------------------------------------+

2.3 新建实例admin-vm

nova boot --flavor m1.tiny --image cirros-0.3.4-x86_64 --nic net-id=39bf56a4-68a8-4f9c-af56-b7bf788a5c45 --security-group default --key-name admin-key admin-vm

命令解释:

--flavor 实例类型

--image 镜像名,必须先上传至glance服务,通过 glance image-list 查看镜像列表

--nic 使用网络,net-id= 填写网络id号

-security-group 安全组

--key-name 选择秘钥

admin-vm 实例名,可随意取

[root@openstack images(keystone_admin)]# nova boot --flavor m1.tiny --image cirros-0.3.4-x86_64 --nic net-id=39bf56a4-68a8-4f9c-af56-b7bf788a5c45 --security-group default --key-name admin-key admin-vm
+--------------------------------------+------------------------------------------------------------+
| Property                             | Value                                                      |
+--------------------------------------+------------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                     |
| OS-EXT-AZ:availability_zone          |                                                            |
| OS-EXT-SRV-ATTR:host                 | -                                                          |
| OS-EXT-SRV-ATTR:hostname             | admin-vm                                                   |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                          |
| OS-EXT-SRV-ATTR:instance_name        |                                                            |
| OS-EXT-SRV-ATTR:kernel_id            |                                                            |
| OS-EXT-SRV-ATTR:launch_index         | 0                                                          |
| OS-EXT-SRV-ATTR:ramdisk_id           |                                                            |
| OS-EXT-SRV-ATTR:reservation_id       | r-1x8lssuk                                                 |
| OS-EXT-SRV-ATTR:root_device_name     | -                                                          |
| OS-EXT-SRV-ATTR:user_data            | -                                                          |
| 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                            | uirGY9o8kaL6                                               |
| config_drive                         |                                                            |
| created                              | 2023-06-07T15:54:32Z                                       |
| description                          | -                                                          |
| flavor:disk                          | 1                                                          |
| flavor:ephemeral                     | 0                                                          |
| flavor:extra_specs                   | {}                                                         |
| flavor:original_name                 | m1.tiny                                                    |
| flavor:ram                           | 512                                                        |
| flavor:swap                          | 0                                                          |
| flavor:vcpus                         | 1                                                          |
| hostId                               |                                                            |
| host_status                          |                                                            |
| id                                   | b5f47588-196a-40d6-a52b-7f2cd371f417                       |
| image                                | cirros-0.3.4-x86_64 (041c0a42-adc2-4694-b932-7f01ae198919) |
| key_name                             | admin-key                                                  |
| locked                               | False                                                      |
| metadata                             | {}                                                         |
| name                                 | admin-vm                                                   |
| os-extended-volumes:volumes_attached | []                                                         |
| progress                             | 0                                                          |
| security_groups                      | default                                                    |
| status                               | BUILD                                                      |
| tags                                 | []                                                         |
| tenant_id                            | 5de5c5079f4147a69f330e9ce20c7f1b                           |
| updated                              | 2023-06-07T15:54:32Z                                       |
| user_id                              | a6ecc9dfabd04180947bfad72c67e5d4                           |
+--------------------------------------+------------------------------------------------------------+

 查看实例列表nova list

[root@openstack images(keystone_admin)]# nova list
+--------------------------------------+----------+--------+------------+-------------+--------------------+
| ID                                   | Name     | Status | Task State | Power State | Networks           |
+--------------------------------------+----------+--------+------------+-------------+--------------------+
| b5f47588-196a-40d6-a52b-7f2cd371f417 | admin-vm | ACTIVE | -          | Running     | public=172.24.4.17 |
+--------------------------------------+----------+--------+------------+-------------+--------------------+

 还可以通过virsh list查看

[root@localhost images(keystone_admin)]# virsh listId    Name                           State
----------------------------------------------------1     instance-00000001              running

还可以通过仪表盘查看 


 

2.4 获取实例VNC的url

实例新建成功,那么如何进入新建的实例呢?需要获取实例VNC的url ,nova get-vnc-console admin-vm novnc,每次使用该命令都会刷新url,url刷新后旧的url就会失效,实例重启后也需要重新获取url。

[root@openstack images(keystone_admin)]# nova get-vnc-console admin-vm novnc
+-------+-------------------------------------------------------------------------------------+
| Type  | Url                                                                                 |
+-------+-------------------------------------------------------------------------------------+
| novnc | http://192.168.136.56:6080/vnc_auto.html?token=6d463d80-8e49-4100-8cdf-f10d5fc94af2 |
+-------+-------------------------------------------------------------------------------------+

拿到url后再浏览器输入即可进入实例的一个终端,进入后需要等待大概5分钟,才会出现“cirrors login:”表示实例正式启动成功,cirros镜像根据提示,账号名为cirros,密码为cubswin:)。

若出现Failed connect to server(code:1006)报错

查看是否实例是否启动,实例状态必须为ACTIVE,我遇到过两次实例自动关机的状况,最后发现是内存不足导致,推荐内存至少10G。

如果实例开机还是不行,添加hosts并重启nova服务

vi /etc/hosts
192.168.136.56 openstack  #添加,注意是主机名,如果你的主机名不是openstack,自行更改,保证可以ping通主机名就可以
source /etc/hosts
systemctl restart openstack-nova-compute.service[root@openstack ~(keystone_admin)]# ping openstack
PING openstack (192.168.136.56) 56(84) bytes of data.
64 bytes from openstack (192.168.136.56): icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from openstack (192.168.136.56): icmp_seq=2 ttl=64 time=0.035 ms

除了使用url登录还以直接通过仪表盘的“控制台”直接控制实例。

2.5 nova常用命令

nova list #查看虚拟机nova show [vm-name]或[vm-id]     #查看虚拟机详细信息
nova reboot (--hard) [vm-name]或[vm-id]    #硬重启实例,失败可以添加属性--hard,强制硬重启nova start [vm-name]或[vm-id]    #启动虚拟机
nova stop [vm-name]或[vm-id]     #关闭虚拟机
nova suspend [vm-name]或[vm-id]  #暂停虚拟机
nova resume [vm-name]或[vm-id]   #启动暂停的虚拟机
nova delete [vm-name]或[vm-id]   #删除虚拟机openstack compute service list   #查看nova相关服务

本文链接:https://my.lmcjl.com/post/14241.html

展开阅读全文

4 评论

留下您的评论.