Openstack上虚拟机迁移

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openstack上虚拟机迁移相关的知识,希望对你有一定的参考价值。

1、迁移虚拟机可以进行冷迁移和热迁移

# 冷迁移
关闭电源的虚拟机进行迁移。通过冷迁移,可以选择将关联的磁盘从一个数据存储移动到另一个数据存储。
好处:虚拟机不需要位于共享存储器上,数据丢失率小。
缺点:需要关闭电源,业务中断。

# 热迁移
即虚拟机保存/恢复,通常是将整个虚拟机的运行状态完整保存下来,同时可以快速的恢复到原有硬件平台甚至是不同硬件平台上。恢复以后,虚拟机仍旧平滑运行,用户不会察觉到任何差异。
好处:软件和硬件系统的维护升级,不会影响用户的关键服务,提高了服务的高可用性和 用户的满意度。
缺点:过程不可中断,操作复杂。需要共享存储
冷迁移的操作方法:(采用的是第三种)
【1】dashbord页面进行操作
选取相应实例,服务器关机、选择进行迁移(无法确认迁移主机)
【2】命令行操作
https://docs.openstack.org/nova/queens/admin/migration.html
【3】主机手动拷贝磁盘文件操作
1、关闭虚拟机
2、拷贝相关虚拟机相关的磁盘文件到目标宿主机上,并修改相应的权限
3、修改数据库相关的配置,袖改host、node相关记录的主机
update instances set host=new_host, node=new_host where uuid=xxxxx*xxxx;
4、旧磁盘保存三天,无问题进行删除


OpenStack手动迁移虚拟机

基于OpenStack平台搭建完成后
迁移的步骤:
1、将关于虚拟机的数据移到目标主机(移动之前需要关闭虚拟机)
在OpenStack里云主机的数据存放目录是/var/lib/nova/instances/
2、修改数据库文件
在nova数据库的instances 表里有记录虚拟机的宿主机的名字,所以需要进入数据库修改该字段为controller

实战;
一、首先创建一个虚拟机
在这里插入图片描述
在管理员的虚拟机管理器界面可以看到虚拟机的主机是compute(我们要把虚拟机从compute节点迁移到controller节点)

在这里插入图片描述
点击云主机的名字可以进入查看云主机的详细信息,可以看到一开始他的主机是compute
在这里插入图片描述

二、开始把数据迁移到controller
云主机关上
在这里插入图片描述
在/var/lib/nova/instances/里,存放着云主机的数据,下面的b5f1929f-e51c-443d-be02-1854e26443cb目录就是我们测试的云主机的id
compute节点:

[root@compute instances]# cd /var/lib/nova/instances/
[root@compute instances]# ll
total 4
drwxr-xr-x. 2 nova nova 69 Jul 16 03:30 b5f1929f-e51c-443d-be02-1854e26443cb
drwxr-xr-x. 2 nova nova 53 Jul 11 18:43 _base
-rw-r--r--. 1 nova nova 30 Jul 16 02:50 compute_nodes
drwxr-xr-x. 2 nova nova 91 Jul 11 18:50 locks

把它复制到controller节点,把本主机的删除

[root@compute instances]# scp -r b5f1929f-e51c-443d-be02-1854e26443cb root@10.30.59.218:/var/lib/nova/instances
root@10.30.59.218's password: 
console.log                                                                                100%   37KB  37.2KB/s   00:00    
disk.info                                                                                  100%   79     0.1KB/s   00:00    
disk                                                                                       100%   14MB  14.0MB/s   00:01    
libvirt.xml 
[root@compute instances]# rm -rf  b5f1929f-e51c-443d-be02-1854e26443cb

controller节点:
把它的用户和用户组修改成nova

[root@controller instances]# ll
total 0
drwxr-xr-x. 2 root root 69 Jul 16 03:35 b5f1929f-e51c-443d-be02-1854e26443cb
[root@controller instances]# chown -R nova:nova b5f1929f-e51c-443d-be02-1854e26443cb/
[root@controller instances]# ll
total 0
drwxr-xr-x. 2 nova nova 69 Jul 16 03:35 b5f1929f-e51c-443d-be02-1854e26443cb

然后修改数据库文件

[root@controller instances]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MariaDB connection id is 2151
Server version: 10.1.17-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| cinder             |
| glance             |
| information_schema |
| keystone           |
| mysql              |
| neutron            |
| nova               |
| nova_api           |
| performance_schema |
+--------------------+
9 rows in set (0.00 sec)

MariaDB [(none)]> use nova
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [nova]> show tables;
+--------------------------------------------+
| Tables_in_nova                             |
+--------------------------------------------+
| agent_builds                               |
| aggregate_hosts                            |
| aggregate_metadata                         |
| aggregates                                 |
| allocations                                |
| block_device_mapping                       |
| bw_usage_cache                             |
| cells                                      |
| certificates                               |
| compute_nodes                              |
| console_pools                              |
| consoles                                   |
| dns_domains                                |
| fixed_ips                                  |
| floating_ips                               |
| instance_actions                           |
| instance_actions_events                    |
| instance_extra                             |
| instance_faults                            |
| instance_group_member                      |
| instance_group_policy                      |
| instance_groups                            |
| instance_id_mappings                       |
| instance_info_caches                       |
| instance_metadata                          |
| instance_system_metadata                   |
| instance_type_extra_specs                  |
| instance_type_projects                     |
| instance_types                             |
| instances                                  |
| inventories                                |
| key_pairs                                  |
| migrate_version                            |
| migrations                                 |
| networks                                   |
| pci_devices                                |
| project_user_quotas                        |
| provider_fw_rules                          |
| quota_classes                              |
| quota_usages                               |
| quotas                                     |
| reservations                               |
| resource_provider_aggregates               |
| resource_providers                         |
| s3_images                                  |
| security_group_default_rules               |
| security_group_instance_association        |
| security_group_rules                       |
| security_groups                            |
| services                                   |
| shadow_agent_builds                        |
| shadow_aggregate_hosts                     |
| shadow_aggregate_metadata                  |
| shadow_aggregates                          |
| shadow_block_device_mapping                |
| shadow_bw_usage_cache                      |
| shadow_cells                               |
| shadow_certificates                        |
| shadow_compute_nodes                       |
| shadow_console_pools                       |
| shadow_consoles                            |
| shadow_dns_domains                         |
| shadow_fixed_ips                           |
| shadow_floating_ips                        |
| shadow_instance_actions                    |
| shadow_instance_actions_events             |
| shadow_instance_extra                      |
| shadow_instance_faults                     |
| shadow_instance_group_member               |
| shadow_instance_group_policy               |
| shadow_instance_groups                     |
| shadow_instance_id_mappings                |
| shadow_instance_info_caches                |
| shadow_instance_metadata                   |
| shadow_instance_system_metadata            |
| shadow_instance_type_extra_specs           |
| shadow_instance_type_projects              |
| shadow_instance_types                      |
| shadow_instances                           |
| shadow_key_pairs                           |
| shadow_migrate_version                     |
| shadow_migrations                          |
| shadow_networks                            |
| shadow_pci_devices                         |
| shadow_project_user_quotas                 |
| shadow_provider_fw_rules                   |
| shadow_quota_classes                       |
| shadow_quota_usages                        |
| shadow_quotas                              |
| shadow_reservations                        |
| shadow_s3_images                           |
| shadow_security_group_default_rules        |
| shadow_security_group_instance_association |
| shadow_security_group_rules                |
| shadow_security_groups                     |
| shadow_services                            |
| shadow_snapshot_id_mappings                |
| shadow_snapshots                           |
| shadow_task_log                            |
| shadow_virtual_interfaces                  |
| shadow_volume_id_mappings                  |
| shadow_volume_usage_cache                  |
| snapshot_id_mappings                       |
| snapshots                                  |
| tags                                       |
| task_log                                   |
| virtual_interfaces                         |
| volume_id_mappings                         |
| volume_usage_cache                         |
+--------------------------------------------+
109 rows in set (0.00 sec)
MariaDB [nova]> select host,node,uuid from instances;
+------------+------------+--------------------------------------+
| host       | node       | uuid                                 |
+------------+------------+--------------------------------------+
| compute    | compute    | 52aae669-86a9-4122-a1e4-dee2c4841456 |
| controller | controller | e6c4070f-3dd9-475c-9676-3c5108161fb9 |
| compute    | compute    | b5f1929f-e51c-443d-be02-1854e26443cb |            ## 这时候看它的主机是compute
+------------+------------+--------------------------------------+
3 rows in set (0.00 sec)
MariaDB [nova]> update instances set host='controller',node='controller' where uuid='b5f1929f-e51c-443d-be02-1854e26443cb';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [nova]> select host,node,uuid from instances;
+------------+------------+--------------------------------------+
| host       | node       | uuid                                 |
+------------+------------+--------------------------------------+
| compute    | compute    | 52aae669-86a9-4122-a1e4-dee2c4841456 |
| controller | controller | e6c4070f-3dd9-475c-9676-3c5108161fb9 |
| controller | controller | b5f1929f-e51c-443d-be02-1854e26443cb |          ## 修改完成后
+------------+------------+--------------------------------------+
3 rows in set (0.00 sec)

MariaDB [nova]> exit

重启nova服务

[root@controller instances]# systemctl restart openstack-nova-compute

开启虚拟机

[root@controller instances]# nova start b5f1929f-e51c-443d-be02-1854e26443cb
Request to start server b5f1929f-e51c-443d-be02-1854e26443cb has been accepted.
[root@controller instances]# nova show b5f1929f-e51c-443d-be02-1854e26443cb
+--------------------------------------+----------------------------------------------------------+
| Property                             | Value                                                    |
+--------------------------------------+----------------------------------------------------------+
| OS-DCF:diskConfig                    | AUTO                                                     |
| OS-EXT-AZ:availability_zone          | nova                                                     |
| OS-EXT-SRV-ATTR:host                 | controller                                               |
| OS-EXT-SRV-ATTR:hostname             | test                                                     |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | controller                                               |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000003                                        |
| 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-vmbh21l4                                               |
| OS-EXT-SRV-ATTR:root_device_name     | /dev/vda                                                 |
| OS-EXT-SRV-ATTR:user_data            | -                                                        |
| OS-EXT-STS:power_state               | 4                                                        |
| OS-EXT-STS:task_state                | powering-on                                              |
| OS-EXT-STS:vm_state                  | stopped                                                  |
| OS-SRV-USG:launched_at               | 2021-07-15T19:30:25.000000                               |
| OS-SRV-USG:terminated_at             | -                                                        |
| accessIPv4                           |                                                          |
| accessIPv6                           |                                                          |
| config_drive                         |                                                          |
| created                              | 2021-07-15T19:30:19Z                                     |
| description                          | test                                                     |
| flavor                               | m1.small (2)                                             |
| hostId                               | b4d8e7c2f999c39aa116d311d30396839ddd46175cb62be11c48faf5 |
| host_status                          |                                                          |
| id                                   | b5f1929f-e51c-443d-be02-1854e26443cb                     |
| image                                | centos7 (d108a3da-a0ed-48dd-9e1f-ec40b92c06b2)           |
| int-net network                      | 10.10.10.103                                             |
| key_name                             | -                                                        |
| locked                               | False                                                    |
| metadata                             | {}                                                       |
| name                                 | test                                                     |
| os-extended-volumes:volumes_attached | []                                                       |
| security_groups                      | default                                                  |
| status                               | SHUTOFF                                                  |
| tenant_id                            | f9d44679d8da4276b113fe99fe3b26c6                         |
| updated                              | 2021-07-15T19:40:01Z                                     |
| user_id                              | 1a81b852a6104c8d874d700cbfb1c705                         |
+--------------------------------------+----------------------------------------------------------+
[root@controller instances]# 

在图形界面里查看
在这里插入图片描述

以上是关于Openstack上虚拟机迁移的主要内容,如果未能解决你的问题,请参考以下文章

虚拟机迁移

openstack云主机冷迁移

openstack mitaka宿主机怎么可以访问虚拟机

Openstack迁移DDH最佳实践

OpenStack

Openstack 之 正常关闭一个物理宿主机