Nova
libvirt
libvirt 是一套用 c 语言写的 API,旨在为各种虚拟机提供一套通用的编程接口, 而且支持与 java,python 等语言的绑定。基于 libvirt 的虚拟机管理工具也有很多: virt-manager(GUI 工具),virsh(命令行工具)
涉及到几个概念:
-
Domain:虚拟机的一个运行实例,简单的理解,就是一个虚拟机虚拟出来的操作系统。它的叫法可是多种多样:instance,guest OS,virsual machine, 其实都指的同一个概念
-
Hypervisor:指的就是虚拟机本身,比如 qemu, kvm, xen... libvirt 由几个不同的部分组成,其中包括应用程序编程接口 (API) 库、一个守护进程 (libvirtd),以及一个默认命令行实用工具 (virsh),libvirtd 守护进程负责对虚拟机的管理工作,在用各种工具对虚拟机进行管理的时候,这个守护进程一定要跑起来,而且这个进程可以分为两种,一种是 root 权限的 libvirtd,一种是普通用户权限的 libvirtd,前者权限大,可以虚拟计算机的各种设备
qemu
qemu 是一个仿真器,即可用于来宾操作系统的虚拟化,也可以作为完整的机器 仿真器使用,运行使用主机 CPU 或其他 CPU 架构的操作系统
qemu 支持两种操作模式:用户模式仿真和系统模式仿真。用户模式仿真 允许一 个 CPU 构建的进程在另一个 CPU 上执行(执行主机 CPU 指令的动态翻译并相 应地转换 Linux 系统调用)。系统模式仿真 允许对整个系统进行仿真,包括处理 器和配套的外围设备,这时就应该使用 root 权限的 libvirtd
qemu 相关的命令
-
创建一个名为 disk.img 的 128M 大小的 qcow 格式的磁盘映像文件,这个文件充当来宾操作系统的硬盘
# qemu-img create -f qcow disk.img 128M
-
-hda 指定磁盘映像文件,-cdrom指定光盘镜像文件,-boot 指定从哪里引导系统, d 是从 CD-ROM 引导,c 表示从硬盘引导
# qemu -hda disk.img -cdrom /root/cflinux-1.0.iso -boot d
-
开始启动来宾操作系统
# qemu -hda disk.img
qemu-img 命令行
qemu-img 命令行工具是 Xen 和 KVM 用来格式化各种文件系统的,可使用 qemu-img 格式化虚拟客户端映像、附加存储设备以及网络存储
-
创建一个 10M 的镜像文件:sina_kvm.img,文件格式为:raw
# qemu-img create [-6] [-e] [-b base_image] [-f format] filename [sina_kvm]
-
将现有映像转换成另一种格式,转换选项是将可识别格式转换为另一个映像格式
# qemu-img convert [-c] [-e] [-f format] filename [-O output_format]
output_filename
-
获得映像信息(info 参数显示磁盘映像信息)
# qemu-img info [-f format] filename
支持格式:
映像格式通常是自动获取的。支持以下格式
-
raw 磁盘映像格式(默认)。这个格式的优点是可以简单、容易地导出到其它模拟器中
-
如果您的文件系统支持中断(例如在 Linux 中的 ext2 或者 ext3 以及 Windows 中的 NTFS),那么只有写入的字段会占用空间。使用 qemu-img info 了解 Unix/Linux 中映像或者 ls -ls 使用的实际大小
-
-
qcow2 映像格式,最万能的格式。使用它可获得较小映像,额外的 AES 加密法、zlib 压缩以及 对多 VM 快照的支持。目前也是虚拟池一直在使用的镜像格式
-
qcow 旧的 QEMU 映像格式。只用于与旧版本兼容,目前虚拟池已无该格式镜像文件
-
cow 写入映像格式的用户模式 Linux 副本。包含 cow 格式的目的只是为了与前 面的版本兼容。它无法在 Windows 中使用,虚拟池已无该格式镜像文件
-
vmdk VMware 3 和 4 兼容映像格式,虚拟池无该格式镜像文件
-
cloop Linux 压缩回送映像,只有在重复使用直接压缩的 CD-ROM 映像时有用, 比如在 Knoppix CD-ROM 中
创建实例
创建镜像
[root@controller ~]# glance image-create --name cirros --disk-format qcow2 --container-format bare < cirros-0.3.4-x86_64-disk.img +------------------+--------------------------------------+ | Property | Value | +------------------+--------------------------------------+ | checksum | ee1eca47dc88f4879d8a229cc70a07c6 | | container_format | bare | | created_at | 2021-12-07T01:27:31Z | | disk_format | qcow2 | | id | c104aad4-604b-46d0-9e02-5d32e416286d | | min_disk | 0 | | min_ram | 0 | | name | cirros | | owner | 2f1b48e86a2f4e1ab59730d9fff7c584 | | protected | False | | size | 13287936 | | status | active | | tags | [] | | updated_at | 2021-12-07T01:27:31Z | | virtual_size | None | | visibility | shared | +------------------+--------------------------------------+
创建实例类型
[root@controller ~]# nova flavor-create test 1 2048 20 2 +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | Description | +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | 1 | test | 2048 | 20 | 0 | | 2 | 1.0 | True | - | +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
创建网络
[root@controller ~]# openstack network create --external --provider-physical-network provider --provider-network-type vlan extnet +---------------------------+--------------------------------------+ | Field | Value | +---------------------------+--------------------------------------+ | admin_state_up | UP | | availability_zone_hints | | | availability_zones | | | created_at | 2021-12-07T01:29:24Z | | description | | | dns_domain | None | | id | 67f46528-7152-4857-8df6-26fbfca13037 | | ipv4_address_scope | None | | ipv6_address_scope | None | | is_default | False | | is_vlan_transparent | None | | mtu | 1500 | | name | extnet | | port_security_enabled | True | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | provider:network_type | vlan | | provider:physical_network | provider | | provider:segmentation_id | 166 | | qos_policy_id | None | | revision_number | 5 | | router:external | External | | segments | None | | shared | False | | status | ACTIVE | | subnets | | | tags | | | updated_at | 2021-12-07T01:29:25Z | +---------------------------+--------------------------------------+ [root@controller ~]# openstack subnet create --network extnet --subnet-range 10.10.35.0/24 --gateway 10.10.35.1 extsubnet +-------------------+--------------------------------------+ | Field | Value | +-------------------+--------------------------------------+ | allocation_pools | 10.10.35.2-10.10.35.254 | | cidr | 10.10.35.0/24 | | created_at | 2021-12-07T01:30:20Z | | description | | | dns_nameservers | | | enable_dhcp | True | | gateway_ip | 10.10.35.1 | | host_routes | | | id | 6844910e-92ea-407f-afd5-a7caecc0f0e3 | | ip_version | 4 | | ipv6_address_mode | None | | ipv6_ra_mode | None | | name | extsubnet | | network_id | 67f46528-7152-4857-8df6-26fbfca13037 | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | revision_number | 0 | | segment_id | None | | service_types | | | subnetpool_id | None | | tags | | | updated_at | 2021-12-07T01:30:20Z | +-------------------+--------------------------------------+
创建安全组
[root@controller ~]# openstack security group create mytest +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | Field | Value | +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | created_at | 2021-12-07T01:31:06Z | | description | mytest | | id | 837da934-dc74-46bc-b0aa-4953755addac | | name | mytest | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | revision_number | 2 | | rules | created_at='2021-12-07T01:31:06Z', direction='egress', ethertype='IPv4', id='bcbc4c54-e91c-4d56-bf20-2c8277404def', updated_at='2021-12-07T01:31:06Z' | | | created_at='2021-12-07T01:31:06Z', direction='egress', ethertype='IPv6', id='eda37ae7-f8ae-41a8-ad7f-983e901e6ce5', updated_at='2021-12-07T01:31:06Z' | | updated_at | 2021-12-07T01:31:06Z | +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
创建规则
[root@controller ~]# openstack security group rule create --proto icmp mytest +-------------------+--------------------------------------+ | Field | Value | +-------------------+--------------------------------------+ | created_at | 2021-12-07T01:32:06Z | | description | | | direction | ingress | | ether_type | IPv4 | | id | efbd000b-5ee3-448d-bd64-ad9d9a86b63f | | name | None | | port_range_max | None | | port_range_min | None | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | protocol | icmp | | remote_group_id | None | | remote_ip_prefix | 0.0.0.0/0 | | revision_number | 0 | | security_group_id | 837da934-dc74-46bc-b0aa-4953755addac | | updated_at | 2021-12-07T01:32:06Z | +-------------------+--------------------------------------+ [root@controller ~]# openstack security group rule create --proto tcp mytest +-------------------+--------------------------------------+ | Field | Value | +-------------------+--------------------------------------+ | created_at | 2021-12-07T01:32:14Z | | description | | | direction | ingress | | ether_type | IPv4 | | id | 0d8bfac8-2537-4e75-bcf1-dd90052254dc | | name | None | | port_range_max | None | | port_range_min | None | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | protocol | tcp | | remote_group_id | None | | remote_ip_prefix | 0.0.0.0/0 | | revision_number | 0 | | security_group_id | 837da934-dc74-46bc-b0aa-4953755addac | | updated_at | 2021-12-07T01:32:14Z | +-------------------+--------------------------------------+ [root@controller ~]# openstack security group rule create --proto tcp --dst-port 22 mytest +-------------------+--------------------------------------+ | Field | Value | +-------------------+--------------------------------------+ | created_at | 2021-12-07T01:32:29Z | | description | | | direction | ingress | | ether_type | IPv4 | | id | 2fe64d87-b496-4d88-a27e-8361ade03936 | | name | None | | port_range_max | 22 | | port_range_min | 22 | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | protocol | tcp | | remote_group_id | None | | remote_ip_prefix | 0.0.0.0/0 | | revision_number | 0 | | security_group_id | 837da934-dc74-46bc-b0aa-4953755addac | | updated_at | 2021-12-07T01:32:29Z | +-------------------+--------------------------------------+
创建实例
- 查看镜像,网络,实例和安全组
[root@controller ~]# glance image-list +--------------------------------------+--------+ | ID | Name | +--------------------------------------+--------+ | c104aad4-604b-46d0-9e02-5d32e416286d | cirros | +--------------------------------------+--------+ [root@controller ~]# nova flavor-list +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | Description | +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ | 1 | test | 2048 | 20 | 0 | | 2 | 1.0 | True | - | +----+------+-----------+------+-----------+------+-------+-------------+-----------+-------------+ [root@controller ~]# openstack network list +--------------------------------------+--------+--------------------------------------+ | ID | Name | Subnets | +--------------------------------------+--------+--------------------------------------+ | 67f46528-7152-4857-8df6-26fbfca13037 | extnet | 6844910e-92ea-407f-afd5-a7caecc0f0e3 | +--------------------------------------+--------+--------------------------------------+ [root@controller ~]# openstack security group list +--------------------------------------+---------+------------------------+----------------------------------+ | ID | Name | Description | Project | +--------------------------------------+---------+------------------------+----------------------------------+ | 837da934-dc74-46bc-b0aa-4953755addac | mytest | mytest | 2f1b48e86a2f4e1ab59730d9fff7c584 | | 8a8017e5-b9fc-414e-ac18-a21090dd1e0c | default | Default security group | 2f1b48e86a2f4e1ab59730d9fff7c584 | +--------------------------------------+---------+------------------------+----------------------------------+
- 创建实例
[root@controller ~]# openstack server create --flavor test --image cirros --nic net-id=67f46528-7152-4857-8df6-26fbfca13037 --security-group mytest vm1 +-------------------------------------+-----------------------------------------------+ | Field | Value | +-------------------------------------+-----------------------------------------------+ | OS-DCF:diskConfig | MANUAL | | OS-EXT-AZ:availability_zone | | | OS-EXT-SRV-ATTR:host | None | | OS-EXT-SRV-ATTR:hypervisor_hostname | None | | OS-EXT-SRV-ATTR:instance_name | | | 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 | H3W7M9LHeMSh | | config_drive | | | created | 2021-12-07T01:34:52Z | | flavor | test (1) | | hostId | | | id | b5a3e477-33a0-49fe-af90-78c9b44d39bb | | image | cirros (c104aad4-604b-46d0-9e02-5d32e416286d) | | key_name | None | | name | vm1 | | progress | 0 | | project_id | 2f1b48e86a2f4e1ab59730d9fff7c584 | | properties | | | security_groups | name='837da934-dc74-46bc-b0aa-4953755addac' | | status | BUILD | | updated | 2021-12-07T01:34:52Z | | user_id | d4463a3c93fc41429ab215bf04418374 | | volumes_attached | | +-------------------------------------+-----------------------------------------------+
- 等待一会查看
[root@controller ~]# openstack server list +--------------------------------------+------+--------+--------------------+--------+--------+ | ID | Name | Status | Networks | Image | Flavor | +--------------------------------------+------+--------+--------------------+--------+--------+ | b5a3e477-33a0-49fe-af90-78c9b44d39bb | vm1 | ACTIVE | extnet=10.10.35.19 | cirros | test | +--------------------------------------+------+--------+--------------------+--------+--------+
查看实例信息
[root@controller ~]# openstack server list +--------------------------------------+------+--------+--------------------+--------+--------+ | ID | Name | Status | Networks | Image | Flavor | +--------------------------------------+------+--------+--------------------+--------+--------+ | b5a3e477-33a0-49fe-af90-78c9b44d39bb | vm1 | ACTIVE | extnet=10.10.35.19 | cirros | test | +--------------------------------------+------+--------+--------------------+--------+--------+
[root@controller ~]# nova list +--------------------------------------+------+--------+------------+-------------+--------------------+ | ID | Name | Status | Task State | Power State | Networks | +--------------------------------------+------+--------+------------+-------------+--------------------+ | b5a3e477-33a0-49fe-af90-78c9b44d39bb | vm1 | ACTIVE | - | Running | extnet=10.10.35.19 | +--------------------------------------+------+--------+------------+-------------+--------------------+
启动实例
[root@controller ~]# openstack server start vm1 [root@controller ~]# nova start vm1 Request to start server vm1 has been accepted.
停止实例
[root@controller ~]# openstack server stop vm1 [root@controller ~]# nova stop vm1 Request to stop server vm1 has been accepted.
重启实例
[root@controller ~]# openstack server reboot vm1
[root@controller ~]# nova reboot vm1
挂起实例
[root@controller ~]# openstack server unpause vm1
[root@controller ~]# nova unpause vm1
调整实例
[root@controller ~]# openstack server resize vm1
[root@controller ~]# nova resize vm1
重建实例
[root@controller ~]# nova rebuild vm1 --image <image_name>
删除实例
[root@controller ~]# nova delete vm1
[root@controller ~]# openstack server delete vm1
[root@controller ~]# openstack security group create <name>
查看安全组列表和详细
[root@controller ~]# openstack security group list +--------------------------------------+---------+-------------+----------------------------------+ | ID | Name | Description | Project | +--------------------------------------+---------+-------------+----------------------------------+ | 3e1101bf-311b-44fc-b75a-b54081f7fdbb | default | 缺省安全组 | 856f7fd215b04578af65a4fefdef4240 | | cbcd0fb2-d0d5-4643-834e-188d48a87fd6 | default | 缺省安全组 | c061000787a14725a9dff7078912426c | +--------------------------------------+---------+-------------+----------------------------------+ [root@controller ~]# openstack security group show <security_group_id>
创建安全组规则
[root@controller ~]# openstack security group rule create --src-ip <ipaddress> --dst-port <port> --proto <协议> <security_group_name> [root@controller ~]# openstack security group rule create --protoco tcp --dst-port 22 test +-------------------+--------------------------------------+ | Field | Value | +-------------------+--------------------------------------+ | created_at | 2021-10-07T15:38:28Z | | description | | | direction | ingress | | ether_type | IPv4 | | id | 6d833ce5-e256-41b4-abc8-dbf673c7b2ab | | name | None | | port_range_max | 22 | | port_range_min | 22 | | project_id | c061000787a14725a9dff7078912426c | | protocol | tcp | | remote_group_id | None | | remote_ip_prefix | 0.0.0.0/0 | | revision_number | 0 | | security_group_id | cdb214ee-a656-4a2d-8d94-ca79c1843295 | | updated_at | 2021-10-07T15:38:28Z | +-------------------+--------------------------------------+
删除安全组
[root@controller ~]# openstack security group delete test
计算节点管理
查看计算节点列表信息
[root@controller ~]# nova hypervisor-list +--------------------------------------+---------------------+-------+---------+ | ID | Hypervisor hostname | State | Status | +--------------------------------------+---------------------+-------+---------+ | d588177c-6351-463c-a9f6-936b3b1f1152 | controller | up | enabled | +--------------------------------------+---------------------+-------+---------+ [root@controller ~]# openstack hypervisor list +----+---------------------+-----------------+---------------+-------+ | ID | Hypervisor Hostname | Hypervisor Type | Host IP | State | +----+---------------------+-----------------+---------------+-------+ | 1 | controller | QEMU | 192.168.1.101 | up | +----+---------------------+-----------------+---------------+-------+
查看计算节点统计信息
[root@controller ~]# nova hypervisor-stats +----------------------+-------+ | Property | Value | +----------------------+-------+ | count | 1 | | current_workload | 0 | | disk_available_least | 1654 | | free_disk_gb | 1659 | | free_ram_mb | 47107 | | local_gb | 1699 | | local_gb_used | 40 | | memory_mb | 49667 | | memory_mb_used | 2560 | | running_vms | 1 | | vcpus | 8 | | vcpus_used | 2 | +----------------------+-------+ [root@controller ~]# openstack hypervisor stats show +----------------------+-------+ | Field | Value | +----------------------+-------+ | count | 1 | | current_workload | 0 | | disk_available_least | 1654 | | free_disk_gb | 1659 | | free_ram_mb | 47107 | | local_gb | 1699 | | local_gb_used | 40 | | memory_mb | 49667 | | memory_mb_used | 2560 | | running_vms | 1 | | vcpus | 8 | | vcpus_used | 2 | +----------------------+-------+
查看计算节点详细信息
[root@controller ~]# nova hypervisor-list +--------------------------------------+---------------------+-------+---------+ | ID | Hypervisor hostname | State | Status | +--------------------------------------+---------------------+-------+---------+ | d588177c-6351-463c-a9f6-936b3b1f1152 | controller | up | enabled | +--------------------------------------+---------------------+-------+---------+ [root@controller ~]# nova hypervisor-show d588177c-6351-463c-a9f6-936b3b1f1152 [root@controller ~]# openstack hypervisor d588177c-6351-463c-a9f6-936b3b1f1152
查看计算节点所有虚拟机列表
[root@controller ~]# nova hypervisor-servers controller +--------------------------------------+-------------------+--------------------------------------+---------------------+ | ID | Name | Hypervisor ID | Hypervisor Hostname | +--------------------------------------+-------------------+--------------------------------------+---------------------+ | 8ef02044-d9e1-4d5e-988b-5323f8a185ba | instance-00000001 | d588177c-6351-463c-a9f6-936b3b1f1152 | controller | +--------------------------------------+-------------------+--------------------------------------+---------------------+
查看计算节点更新时间
[root@controller ~]# nova hypervisor-uptime d588177c-6351-463c-a9f6-936b3b1f1152 +---------------------+---------------------------------------------------------------+ | Property | Value | +---------------------+---------------------------------------------------------------+ | hypervisor_hostname | controller | | id | d588177c-6351-463c-a9f6-936b3b1f1152 | | state | up | | status | enabled | | uptime | 23:04:54 up 2:03, 4 users, load average: 0.02, 0.10, 0.13 | | | | +---------------------+---------------------------------------------------------------+
查看可用域列表信息
[root@controller ~]# nova availability-zone-list +-----------------------+----------------------------------------+ | Name | Status | +-----------------------+----------------------------------------+ | internal | available | | |- controller | | | | |- nova-conductor | enabled :-) 2021-10-07T15:05:30.000000 | | | |- nova-consoleauth | enabled :-) 2021-10-07T15:05:27.000000 | | | |- nova-scheduler | enabled :-) 2021-10-07T15:05:31.000000 | | nova | available | | |- controller | | | | |- nova-compute | enabled :-) 2021-10-07T15:05:28.000000 | +-----------------------+----------------------------------------+ [root@controller ~]# openstack availability zone list +-----------+-------------+ | Zone Name | Zone Status | +-----------+-------------+ | internal | available | | nova | available | | nova | available | | nova | available | | nova | available | +-----------+-------------+
查看主机集合列表信息
[root@controller ~]# nova aggregate-list +----+------+-------------------+------+ | Id | Name | Availability Zone | UUID | +----+------+-------------------+------+ +----+------+-------------------+------+ [root@controller ~]# openstack aggregate list
配额管理
查看默认配额列表信息
[root@controller ~]# nova quota-defaults +----------------------+-------+ | Quota | Limit | +----------------------+-------+ | instances | 10 | | cores | 20 | | ram | 51200 | | metadata_items | 128 | | key_pairs | 100 | | server_groups | 10 | | server_group_members | 10 | +----------------------+-------+
更新类配额
[root@controller ~]# nova quota-class-update --instance 20 default [root@controller ~]# nova quota-defaults +----------------------+-------+ | Quota | Limit | +----------------------+-------+ | instances | 20 | | cores | 20 | | ram | 51200 | | metadata_items | 128 | | key_pairs | 100 | | server_groups | 10 | | server_group_members | 10 | +----------------------+-------+

浙公网安备 33010602011771号