飞羽

CRIU进程热迁移
本文主要内容是通过CRIU工具,对linux进行进程的热迁移。
扫描右侧二维码阅读全文
13
2019/06

CRIU进程热迁移

本文主要内容是通过CRIU工具,对linux进行进程的热迁移。

CRIU进程热迁移

本文主要内容是通过CRIU工具,对linux进行进程的热迁移。首先对热迁移和CRIU进行简单的介绍,接着是使用CRIU对Linux的bash脚本进行热迁移。

CRIU进程热迁移.jpg

热迁移介绍

热迁移介绍

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

CRIU相关


CRIU介绍和编译请看本博客的这篇文章:

使用环境


本次实验使用Centos 7.6,架构为X86_64架构
Linux内核为3.10.0-957.12.2.el7.x86_64

进程热迁移步骤

创建一个脚本


创建一个脚本,名字为test.sh并运行

[root@localhost test]# cat test.sh 
i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done
bash ./test.sh

进程保存


通过xshell新建一个终端,并获取test.sh的pid进程,输入criu的进程迁移命令

  • 获取pid
[root@localhost ~]# pgrep -f test.sh
17105

本文输出的PID为17105

  • CRIU命令

CRIU命令

通过以下命令,进行进程保存,其中pid为进程ID,$imagedir为保存目录。

criu dump -t $pid --images-dir $imagedir --shell-job

  • 通过CRIU命令进行进程保存
criu dump -t 17105 --images-dir /root/1/test/checkpoint --shell-job

保存完毕,可以看以下images-dir里的文件

[root@localhost test]# ls ./checkpoint/
cgroup.img      fdinfo-2.img  fs-17105.img   ids-17793.img  mm-17793.img       pages-1.img  seccomp.img
core-17105.img  fdinfo-3.img  fs-17793.img   inventory.img  pagemap-17105.img  pages-2.img  stats-dump
core-17793.img  files.img     ids-17105.img  mm-17105.img   pagemap-17793.img  pstree.img   tty-info.img

进程迁移


保存的镜像可以打包放在其他服务器,也可以放在当前服务器进行运行。

  • 打包文件
[root@localhost 1]# tar -cvf test.tar.gz ./test/
./test/
./test/test.sh
./test/checkpoint/
./test/checkpoint/seccomp.img
./test/checkpoint/files.img
./test/checkpoint/ids-17105.img
./test/checkpoint/tty-info.img
./test/checkpoint/fdinfo-2.img
./test/checkpoint/pagemap-17105.img
./test/checkpoint/pages-1.img
./test/checkpoint/core-17105.img
./test/checkpoint/mm-17105.img
./test/checkpoint/fs-17105.img
./test/checkpoint/ids-17793.img
./test/checkpoint/fdinfo-3.img
./test/checkpoint/pagemap-17793.img
./test/checkpoint/pages-2.img
./test/checkpoint/core-17793.img
./test/checkpoint/mm-17793.img
./test/checkpoint/fs-17793.img
./test/checkpoint/pstree.img
./test/checkpoint/cgroup.img
./test/checkpoint/inventory.img
./test/checkpoint/stats-dump
  • 通过scp迁移到另一台服务器
[root@localhost 1]# scp ./test.tar.gz 192.168.1.98:/root/1/
root@192.168.1.98's password: 
test.tar.gz                                   100%  370KB   8.4MB/s   00:00
  • 在另一台服务器上解压
[root@localhost 1]# tar -xvf ./test.tar.gz 
./test/
./test/test.sh
./test/checkpoint/
./test/checkpoint/seccomp.img
./test/checkpoint/files.img
./test/checkpoint/ids-17105.img
./test/checkpoint/tty-info.img
./test/checkpoint/fdinfo-2.img
./test/checkpoint/pagemap-17105.img
./test/checkpoint/pages-1.img
./test/checkpoint/core-17105.img
./test/checkpoint/mm-17105.img
./test/checkpoint/fs-17105.img
./test/checkpoint/ids-17793.img
./test/checkpoint/fdinfo-3.img
./test/checkpoint/pagemap-17793.img
./test/checkpoint/pages-2.img
./test/checkpoint/core-17793.img
./test/checkpoint/mm-17793.img
./test/checkpoint/fs-17793.img
./test/checkpoint/pstree.img
./test/checkpoint/cgroup.img
./test/checkpoint/inventory.img
./test/checkpoint/stats-dump

进程恢复

通过以下命令进行进程恢复

 criu restore -t 23267 --images-dir /root/1/test/checkpoint --shell-job


如果最后一步报错,请看“报错以及改错”

其他命令说明


刚刚执行的是CRIU的脚本,如果要执行程序的话,需要另一个命令:

criu dump -D 你保存的路径 -j -t 进程PID #保存命令
criu   restore   -D  你还原的路径    -j #还原命令


使用方法与脚本还原的方法是一样的。

报错以及改错


CRIU迁移需要的环境非常苛刻,文件大小,内核版本,系统版本等不同,都会恢复失败,典型的失败信息如下:

[root@localhost bin]# criu restore -t 18280 --images-dir /root/1/test/checkpoint --shell-job
Warn  (criu/crtools.c:210): Using -t with criu restore is obsoleted
 18231: Error (criu/files-reg.c:1748): File usr/lib/locale/locale-archive has bad size 106075056 (expect 106070960)
 18231: Error (criu/mem.c:1284): `- Can't open vma
 18280: Error (criu/files-reg.c:1748): File usr/lib/locale/locale-archive has bad size 106075056 (expect 106070960)
 18280: Error (criu/mem.c:1284): `- Can't open vma
Error (criu/cr-restore.c:1415): 18231 exited, status=1
Error (criu/cr-restore.c:2294): Restoring FAILED.
[root@localhost bin]# criu restore --images-dir /root/1/test/checkpoint --shell-job
 18280: Error (criu/files-reg.c:1748): File usr/lib/locale/locale-archive has bad size 106075056 (expect 106070960)
 18280: Error (criu/mem.c:1284): `- Can't open vma
 18231: Error (criu/files-reg.c:1748): File usr/lib/locale/locale-archive has bad size 106075056 (expect 106070960)
 18231: Error (criu/mem.c:1284): `- Can't open vma
Error (criu/cr-restore.c:1418): 18231 killed by signal 9: Killed
Error (criu/cr-restore.c:2294): Restoring FAILED.


分析原因:因为依赖的库的大小不同,所以导致还原失败,请检查文件大小是否相同。

文章名: 《CRIU进程热迁移》

文章链接:https://blog.8086k.cn/archives/37/

联系方式:1412981048@qq.com

除特别注明外,文章均为飞羽小随笔原创,转载时请注明本文出处及文章链接
Last modification:June 13th, 2019 at 04:15 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment