文章目錄
- 前言
- 一、本地環境
- 二、容器自身遷移
- 三、跨容器遷移
- 四、多個行程運行時容器遷移
- 開始進行容器熱遷移實作
- 回滾
前言
最近呢也是在一直研究docker容器熱遷移,在網上查閱了大量相關的資料,并且參照網上例子自己在去實作,這個程序中出現很多問題,就比如按照很多教程操作,最后沒有達到預期效果,所有在這寫了一篇總結,也是對自己學習的鞏固,
技術:Checkpoint/Restore
一、本地環境
1.系統環境
Centos7.6 $ lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.8.2003 (Core)
Release: 7.8.2003
Codename: Core
實測 CentOS Linux release 7.9.2009也行
2.內核版本
Centos7.6 $ uname -a
Linux bogon 5.10.2-1.el7.elrepo.x86_64 #1 SMP Sun Dec 20 09:53:23 EST 2020 x86_64
實測 3.10版本也行
3.Docker版本
Centos7.6 $ docker version
Client:
Version: 17.06.0-ce
API version: 1.30
Go version: go1.8.3
Git commit: 02c1d87
Built: Fri Jun 23 21:20:36 2017
OS/Arch: linux/amd64
Server:
Version: 17.06.0-ce
API version: 1.30 (minimum version 1.12)
Go version: go1.8.3
Git commit: 02c1d87
Built: Fri Jun 23 21:21:56 2017
OS/Arch: linux/amd64
Experimental: true
實測 17.12.1-ce不行
4.CRIU版本
Centos7.6 $ criu -V
Version: 3.12

此時CRIU有記憶體快照不可用的警告,最好可以將其解決,不過不影響本次熱遷移實作,
5.daemon.json
Centos7.6 $ cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] ,
"experimental": true
}
以上環境皆為本次實作所用環境,其他環境除上述提到過的都沒有去驗證,(軟體安裝或者內核更新可以自行查閱資料)
Docker卸載與重裝
Centos更新內核
重啟docker服務
systemctl daemon-reload
systemctl restart docker
二、容器自身遷移
先使用bash腳本進行驗證
//創建容器
docker run -d --name looper2 --security-opt seccomp:unconfined busybox /bin/sh -c 'i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done'

//新建檢查點
docker checkpoint create looper2 checkpoint1

此時容器狀態從Up變為Exited

此時程式已經運行到當前狀態

//還原檢查點
docker start --checkpoint checkpoint1 looper2
還原后日志能夠重新續接上次的列印,符合預期,


三、跨容器遷移
此時用相同的命令創建一個容器looper2-clone,創建之后馬上暫停,避免寫入太多日志
docker run -d --name looper2-clone --security-opt seccomp:unconfined busybox /bin/sh -c 'i=0; while true; do echo $i; i=$(expr $i + 1); sleep 1; done'
docker stop looper2-clone


將容器looper2的檢查點檔案,拷貝到looper2-clone,要不然looper2-clone找不到檢查點會遷移失敗

//遷移
docker start --checkpoint checkpoint1 looper2-clone
此時日志直接跳到了checkpoint1檢查點運行的地方,然后繼續運行,符合預期,

四、多個行程運行時容器遷移
先正常創建個容器,然后將在容器里寫幾個程式,為了方便查看熱遷移是否生效,我都是采用寫檔案的方式來觀察執行情況,
docker run -id --security-opt=seccomp:unconfined --name test -h test test:1222 /usr/sbin/init
/root/app.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int i = 0;
int j = 0;
char buff[128] = {'\0'};
char tmp[128] = {'\0'};
pid_t fpid;
fpid=fork();
if (fpid == 0) {
while(j < 100000)
{
memset(tmp,0,sizeof(tmp));
sprintf(tmp,"echo \"fork:%d\">>/root/1",j);
system(tmp);
j++;
sleep(1.5);
}
}
while(i < 200000)
{
memset(buff,0,sizeof(buff));
sprintf(buff,"echo \"count:%d\">>/root/1",i);
system(buff);
i++;
sleep(1);
}
return 0;
}
編譯:gcc -o app app.c
test.sh
#/bin/bash
/root/app &
for a in {1..1000000}
do
echo "count:$a">>/root/2
sleep 1
done
/etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
sleep 1
/root/test.sh &
/bin/bash
exit 0
chmod +x /etc/rc.d/rc.local
//提交成鏡像
docker commit -p test test:1122
先將當前運行容器全部洗掉,方便觀察
docker kill $(docker ps -q)
docker rm $(docker ps -a)"
開始進行容器熱遷移實作
//容器創建
docker run -id --security-opt=seccomp:unconfined --name test -h test test:1222 /etc/rc.local
進入容器查看運行情況:

//創建檢查點
docker checkpoint create test checkpoint2

實際操作程序中因為創建檢查點之前時程式依然還在執行,所有上上圖僅供參考
//遷移
docker start --checkpoint checkpoint2 test


遷移后程式能接著上次檢查點繼續執行,符合預期,
回滾
我們可以遷移后讓容器內程式繼續運行一段時間,然后重新用檢查點checkpoint2進行遷移,這樣可以實作程式運行狀態的回滾,
docker start --checkpoint checkpoint2 test
休息一段時間
docker stop test
//重新進行遷移
docker start --checkpoint checkpoint2 test
此時程式運行狀態:



遷移后容器實作了回滾的功能,程式運行狀態回滾到之前的運行狀態,符合預期,
至此,容器熱遷移基本實作,容器內行程加速,容器內行程回退滿足要求,
跨主機容器熱遷移我也驗證通過,和上面跨容器遷移類似,需要將檢查點檔案拷貝到另一個主機存放檢查點的目錄
需要保證兩個主機容器環境一下,宿主機環境一樣,避免出現其他問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/239609.html
標籤:其他
