OpenStack虚拟机迁移到指定宿主机

2023-02-24 17:27:13 浏览数 (3)

说明:

openstack虚拟机迁移分为冷迁移和热迁移两种方式。
  • 冷迁移(cold migration),也叫静态迁移。关闭电源的虚拟机进行迁移。通过冷迁移,可以选择将关联的磁盘从一个数据存储移动到另一个数据存储。

​ 好处:虚拟机不需要位于共享存储器上,数据丢失率小。

​ 缺点:需要关闭电源,业务中断。

  • 热迁移(Live Migration),又叫动态迁移、实时迁移,即虚拟机保存/恢复,通常是将整个虚拟机的运行状态完整保存下来,同时可以快速的恢复到原有硬件平台甚至是不同硬件平台上。恢复以后,虚拟机仍旧平滑运行,用户不会察觉到任何差异。

​ 好处:软件和硬件系统的维护升级,不会影响用户的关键服务,提高了服务的高可用性和 用户的满意度。

​ 缺点:过程不可中断,操作复杂。

注意:此处只演示冷迁移方式

场景:

虚拟机实例:10.10.218.61

从计算节点 szsjhl-openstack-compute-test-10-10-214-38-belle.lan 迁移到 szsjhl-openstack-compute-test-10-10-214-39-belle.lan

迁移步骤

查看虚拟机实例ID

命令:nova list --all |grep 10.10.218.61

代码语言:javascript复制
# nova list --all |grep 10.10.218.61
| 1614d161-bc14-4d09-84fc-e5c4e4abf23c | test1 | 463600ca3a754a1a8eda96d2adf875ef | ACTIVE  | - | Running | 测试218段=10.10.218.61 |
查看实例信息

命令:nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c
 -------------------------------------- ------------------------------------------------------------------- 
| Property                             | Value                                                             |
 -------------------------------------- ------------------------------------------------------------------- 
| OS-DCF:diskConfig                    | AUTO                                                              |
| OS-EXT-AZ:availability_zone          | nova                                                              |
| OS-EXT-SRV-ATTR:host                 | szsjhl-openstack-compute-test-10-10-214-38-belle.lan              |
| OS-EXT-SRV-ATTR:hostname             | test1                                                             |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | szsjhl-openstack-compute-test-10-10-214-38-belle.lan              |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000264                                                 |
| 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-s80zgm02                                                        |
| OS-EXT-SRV-ATTR:root_device_name     | /dev/vda                                                          |
| OS-EXT-SRV-ATTR:user_data            | -                                                                 |
| OS-EXT-STS:power_state               | 1                                                                 |
| OS-EXT-STS:task_state                | -                                                                 |
| OS-EXT-STS:vm_state                  | active                                                            |
| OS-SRV-USG:launched_at               | 2022-10-27T02:03:56.000000                                        |
| OS-SRV-USG:terminated_at             | -                                                                 |
| accessIPv4                           |                                                                   |
| accessIPv6                           |                                                                   |
| config_drive                         |                                                                   |
| created                              | 2022-10-27T02:03:46Z                                              |
| description                          | -                                                                 |
| flavor:disk                          | 100                                                               |
| flavor:ephemeral                     | 0                                                                 |
| flavor:extra_specs                   | {}                                                                |
| flavor:original_name                 | c4m8d100                                                          |
| flavor:ram                           | 8192                                                              |
| flavor:swap                          | 0                                                                 |
| flavor:vcpus                         | 4                                                                 |
| hostId                               | 9277e4d0cadf5afae92f0175145655707f7063e53911904058174228          |
| host_status                          | UP                                                                |
| id                                   | 1614d161-bc14-4d09-84fc-e5c4e4abf23c                              |
| image                                | Linux-Centos7.9-2022.04.21 (8824ae73-1c3d-46ef-8b92-e401d16f750d) |
| key_name                             | -                                                                 |
| locked                               | False                                                             |
| metadata                             | {}                                                                |
| name                                 | test1                                                             |
| os-extended-volumes:volumes_attached | []                                                                |
| progress                             | 0                                                                 |
| security_groups                      | default                                                           |
| status                               | ACTIVE                                                            |
| tags                                 | []                                                                |
| tenant_id                            | 463600ca3a754a1a8eda96d2adf875ef                                  |
| updated                              | 2022-10-27T02:03:56Z                                              |
| user_id                              | 46f2707bd9d64419b9636e14bd127693                                  |
| 测试218段 network                     | 10.10.218.61                                                      |
 -------------------------------------- ------------------------------------------------------------------- 

从上面可以看出此实例现在所在宿主机是szsjhl-openstack-compute-test-10-10-214-38-belle.lan

关闭虚拟机实例

命令:nova stop 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova stop 1614d161-bc14-4d09-84fc-e5c4e4abf23c
Request to stop server 1614d161-bc14-4d09-84fc-e5c4e4abf23c has been accepted.
代码语言:javascript复制
# nova list --all |grep 10.10.218.61            
| 1614d161-bc14-4d09-84fc-e5c4e4abf23c | test1 | 463600ca3a754a1a8eda96d2adf875ef | SHUTOFF | - | Shutdown | 测试218段=10.10.218.61 |

再次查看实例,已经是SHUTOFF状态

迁移虚拟机实例

语法:nova migrate [–host ] [–poll]

命令:nova migrate --host szsjhl-openstack-compute-test-10-10-214-39-belle.lan --poll 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova migrate --host szsjhl-openstack-compute-test-10-10-214-39-belle.lan --poll 1614d161-bc14-4d09-84fc-e5c4e4abf23c

Server migrating... 100% complete
Finished
再次查看虚拟机实例详情

命令:nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c
 -------------------------------------- ------------------------------------------------------------------- 
| Property                             | Value                                                             |
 -------------------------------------- ------------------------------------------------------------------- 
| OS-DCF:diskConfig                    | AUTO                                                              |
| OS-EXT-AZ:availability_zone          | nova                                                              |
| OS-EXT-SRV-ATTR:host                 | szsjhl-openstack-compute-test-10-10-214-39-belle.lan              |
| OS-EXT-SRV-ATTR:hostname             | test1                                                             |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | szsjhl-openstack-compute-test-10-10-214-39-belle.lan              |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000264                                                 |
| 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-s80zgm02                                                        |
| 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                | -                                                                 |
| OS-EXT-STS:vm_state                  | resized                                                           |
| OS-SRV-USG:launched_at               | 2022-10-27T02:26:28.000000                                        |
| OS-SRV-USG:terminated_at             | -                                                                 |
| accessIPv4                           |                                                                   |
| accessIPv6                           |                                                                   |
| config_drive                         |                                                                   |
| created                              | 2022-10-27T02:03:46Z                                              |
| description                          | -                                                                 |
| flavor:disk                          | 100                                                               |
| flavor:ephemeral                     | 0                                                                 |
| flavor:extra_specs                   | {}                                                                |
| flavor:original_name                 | c4m8d100                                                          |
| flavor:ram                           | 8192                                                              |
| flavor:swap                          | 0                                                                 |
| flavor:vcpus                         | 4                                                                 |
| hostId                               | ab81571f25c2d3b079789ed8e26053c9fbcc4c9ae2a1e06cca8b5083          |
| host_status                          | UP                                                                |
| id                                   | 1614d161-bc14-4d09-84fc-e5c4e4abf23c                              |
| image                                | Linux-Centos7.9-2022.04.21 (8824ae73-1c3d-46ef-8b92-e401d16f750d) |
| key_name                             | -                                                                 |
| locked                               | False                                                             |
| metadata                             | {}                                                                |
| name                                 | test1                                                             |
| os-extended-volumes:volumes_attached | []                                                                |
| progress                             | 0                                                                 |
| security_groups                      | default                                                           |
| status                               | VERIFY_RESIZE                                                     |
| tags                                 | []                                                                |
| tenant_id                            | 463600ca3a754a1a8eda96d2adf875ef                                  |
| updated                              | 2022-10-27T02:26:28Z                                              |
| user_id                              | 46f2707bd9d64419b9636e14bd127693                                  |
| 测试218段 network                     | 10.10.218.61                                                      |
 -------------------------------------- ------------------------------------------------------------------- 

从上面可以看出现在status VERIFY_RESIZE 并且实例所在的宿主机已经变成了szsjhl-openstack-compute-test-10-10-214-39-belle.lan。说明迁移成功,否则就是失败

如果失败,在原宿主机上查看nova日志会看到以下报错: Command: ssh -o BatchMode=yes 10.10.214.39 mkdir -p /data/instances/1614d161-bc14-4d09-84fc-e5c4e4abf23c

解决方法 这是因为原宿主机的nova用户不能免密登录目标宿主机创建文件夹,让原宿主机nova用户免密登录目标宿主机就能解决。配置步骤如下: # 下面两步在原宿主机和目标宿主机都要执行 # usermod -s /bin/bash nova # echo nova | passwd nova --stdin # 以下命令只需要在原宿主机上执行 # su nova # ssh-keygen -t rsa -N -f ~/.ssh/id_rsa # cd ~/.ssh/ # cp id_rsa.pub authorized_keys # scp -r ~/.ssh/ 10.10.214.39:/var/lib/nova

确认resize状态

这一步很重要,需要进行确认resize状态才会完成虚拟机实例的迁移

命令:nova resize-confirm 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova resize-confirm 1614d161-bc14-4d09-84fc-e5c4e4abf23c
再次查看虚拟机实例详情

命令:nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova show 1614d161-bc14-4d09-84fc-e5c4e4abf23c           
 -------------------------------------- ------------------------------------------------------------------- 
| Property                             | Value                                                             |
 -------------------------------------- ------------------------------------------------------------------- 
| OS-DCF:diskConfig                    | AUTO                                                              |
| OS-EXT-AZ:availability_zone          | nova                                                              |
| OS-EXT-SRV-ATTR:host                 | szsjhl-openstack-compute-test-10-10-214-39-belle.lan              |
| OS-EXT-SRV-ATTR:hostname             | test1                                                             |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | szsjhl-openstack-compute-test-10-10-214-39-belle.lan              |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000264                                                 |
| 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-s80zgm02                                                        |
| 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                | -                                                                 |
| OS-EXT-STS:vm_state                  | stopped                                                           |
| OS-SRV-USG:launched_at               | 2022-10-27T02:26:28.000000                                        |
| OS-SRV-USG:terminated_at             | -                                                                 |
| accessIPv4                           |                                                                   |
| accessIPv6                           |                                                                   |
| config_drive                         |                                                                   |
| created                              | 2022-10-27T02:03:46Z                                              |
| description                          | -                                                                 |
| flavor:disk                          | 100                                                               |
| flavor:ephemeral                     | 0                                                                 |
| flavor:extra_specs                   | {}                                                                |
| flavor:original_name                 | c4m8d100                                                          |
| flavor:ram                           | 8192                                                              |
| flavor:swap                          | 0                                                                 |
| flavor:vcpus                         | 4                                                                 |
| hostId                               | ab81571f25c2d3b079789ed8e26053c9fbcc4c9ae2a1e06cca8b5083          |
| host_status                          | UP                                                                |
| id                                   | 1614d161-bc14-4d09-84fc-e5c4e4abf23c                              |
| image                                | Linux-Centos7.9-2022.04.21 (8824ae73-1c3d-46ef-8b92-e401d16f750d) |
| key_name                             | -                                                                 |
| locked                               | False                                                             |
| metadata                             | {}                                                                |
| name                                 | test1                                                             |
| os-extended-volumes:volumes_attached | []                                                                |
| security_groups                      | default                                                           |
| status                               | SHUTOFF                                                           |
| tags                                 | []                                                                |
| tenant_id                            | 463600ca3a754a1a8eda96d2adf875ef                                  |
| updated                              | 2022-10-27T03:25:39Z                                              |
| user_id                              | 46f2707bd9d64419b9636e14bd127693                                  |
| 测试218段 network                     | 10.10.218.61                                                      |
 -------------------------------------- ------------------------------------------------------------------- 

注意:上面status 状态为SHUTOFF说明成功,否则失败。

重新启动虚拟机实例

命令:nova start 1614d161-bc14-4d09-84fc-e5c4e4abf23c

代码语言:javascript复制
# nova start 1614d161-bc14-4d09-84fc-e5c4e4abf23c          
Request to start server 1614d161-bc14-4d09-84fc-e5c4e4abf23c has been accepted.

验证

代码语言:javascript复制
# ping 10.10.218.61 
PING 10.10.218.61 (10.10.218.61) 56(84) bytes of data.
64 bytes from 10.10.218.61: icmp_seq=1 ttl=63 time=0.544 ms
64 bytes from 10.10.218.61: icmp_seq=2 ttl=63 time=0.484 ms

开机之后可以ping通ip地址,并且上面已经确认过所在宿主机已经在目标宿主机就说明迁移成功了。

0 人点赞