Docker容器迁移实验

  • Post author:
  • Post category:其他



一、实验环境

原节点 迁移节点
192.168.172.133(系统版本:Ubuntu 18.04;Docker版本:19.03 ) 192.168.172.132(CentOS 7.5;Docker版本:19.03)


二、export命令



原节点:

root@docker:~# docker run --name  web01   -d   -p 8080:80   nginx:latest  
b9d3c33ee7f00089a2bdd95a9e72dfe4278fbc7999d4e0b86aaba8b2d5fe9b2b
root@docker:~# docker exec -it  web01   /bin/bash 
root@b9d3c33ee7f0:~# cd /usr/share/nginx/html/
root@b9d3c33ee7f0:/usr/share/nginx/html# echo "Hello Docker"  > index.html 
root@b9d3c33ee7f0:/usr/share/nginx/html# 
root@b9d3c33ee7f0:/usr/share/nginx/html# exit
exit
root@docker:~# docker stop web01 
web01
root@docker:~# docker export web01  > web01.tar
root@docker:~# ll -h web01.tar 
-rw-r--r-- 1 root root 123M 2月  18 16:17 web01.tar
root@docker:~# scp web01.tar  192.168.172.132:/root/
The authenticity of host '192.168.172.132 (192.168.172.132)' can't be established.
ECDSA key fingerprint is SHA256:nAwn3pSPK0xPv4KEWXV47uu6WxnKqufPmiXyI0bc9HI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.172.132' (ECDSA) to the list of known hosts.
root@192.168.172.132's password: 
web01.tar                                         100%  123MB  14.9MB/s   00:08    


迁移节点:

[root@docker ~]# ls web01.tar -hl 
-rw-r--r-- 1 root root 123M Feb 18 16:20 web01.tar
[root@docker ~]# cat web01.tar | docker import - web01 
sha256:517063cc74b64e3e55675c6f445753ba1ec2694b31cb74a3c44ffa15e5185373
[root@docker ~]# docker image ls -a 
[root@docker ~]# docker image ls -a
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
web01                         latest              97e927886249        28 seconds ago      125MB
[root@docker ~]# docker run   -it  -p 8080:80  web01   /bin/bash 
root@a5e5cce0d6ef:/# cat  /usr/share/nginx/html/index.html 
Hello Docker


三、save命令



原节点:

root@docker:~# docker run --name   web02   -d   -p 8080:80   nginx   
87ecfc8b948eea197982ed384a40faf0e689f1d09369166177b80937a7c8e71f
root@docker:~# docker exec -it    web02   /bin/bash 
root@87ecfc8b948e:/# echo "Hello Docker"   > /usr/share/nginx/html/index.html    
root@87ecfc8b948e:/# 
root@87ecfc8b948e:/# cat /usr/share/nginx/html/index.html 
Hello Docker
root@87ecfc8b948e:/# exit
exit
root@docker:~# docker stop web02 
web02
root@docker:~# docker   commit   -m "web02"    web02   web02 
sha256:07bdfcc4cf39ddffe4d38df00886843dd5ff946c9788f659669f46f6d617b7a6
root@docker:~# 
root@docker:~# docker save -o  web02.tar  web02   
root@docker:~# ll -h   web02.tar 
-rw------- 1 root root 125M 2月  18 16:43 web02.tar
root@docker:~# scp web02.tar  192.168.172.132:/root/
root@192.168.172.132's password: 
web02.tar                                                                           100%  125MB  22.0MB/s   00:05 


迁移节点:

[root@docker ~]# ls   -lh  web02.tar    
-rw------- 1 root root 125M Feb 18 16:44 web02.tar
[root@docker ~]# docker load  --input   web02.tar   
bdf8fff1f649: Loading layer [==================================================>]   12.8kB/12.8kB
Loaded image: web02:latest
[root@docker ~]# docker image ls -a
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
web02                         latest              07bdfcc4cf39        4 minutes ago       127MB
[root@docker ~]# docker run   -it  -p 8080:80  web02   /bin/bash 
root@3f4c6a4026b2:/#       
root@3f4c6a4026b2:/# 
root@3f4c6a4026b2:/# cat /usr/share/nginx/html/index.html 
Hello Docker


四、容器卷数据迁移



原节点:

root@docker:~# mkdir   /data/mydb01   -pv 
mkdir: created directory '/data'
mkdir: created directory '/data/mydb01'
root@docker:~# 
root@docker:~# docker run --name mydb01 -d -p 3336:3306 -v /data/mydb01/:/var/lib/mysql -e "MYSQL_ROOT_PASSWORD=redhat"  mysql:5.7
c371141c91536af453be4904ece80e4267e8926ab1051ec06b5a096f4794db0d
root@docker:~# docker exec  -it   mydb01    /bin/bash 
root@c371141c9153:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database    test   charset utf8mb4;
Query OK, 1 row affected (0.01 sec)

mysql> use test;
Database changed
mysql> create   table     student(id  int(10),name varchar(20));
Query OK, 0 rows affected (0.11 sec)

mysql> insert into   student     values(1,"tangmu"),(2,"jierui");
Query OK, 2 rows affected (1.48 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+------+--------+
| id   | name   |
+------+--------+
|    1 | tangmu |
|    2 | jierui |
+------+--------+
2 rows in set (0.00 sec)
mysql> exit
Bye
root@c371141c9153:/# 
root@c371141c9153:/# exit
exit
root@docker:~# 
root@docker:~# ll /data/mydb01/
total 188488
drwxr-xr-x 6  999 root       4096 2月  18 16:59 ./
drwxr-xr-x 3 root root       4096 2月  18 16:54 ../
-rw-r----- 1  999 docker       56 2月  18 16:57 auto.cnf
-rw------- 1  999 docker     1680 2月  18 16:57 ca-key.pem
-rw-r--r-- 1  999 docker     1112 2月  18 16:57 ca.pem
-rw-r--r-- 1  999 docker     1112 2月  18 16:57 client-cert.pem
-rw------- 1  999 docker     1680 2月  18 16:57 client-key.pem
-rw-r----- 1  999 docker     1346 2月  18 16:58 ib_buffer_pool
-rw-r----- 1  999 docker 79691776 2月  18 17:01 ibdata1
-rw-r----- 1  999 docker 50331648 2月  18 17:01 ib_logfile0
-rw-r----- 1  999 docker 50331648 2月  18 16:57 ib_logfile1
-rw-r----- 1  999 docker 12582912 2月  18 16:58 ibtmp1
drwxr-x--- 2  999 docker     4096 2月  18 16:57 mysql/
drwxr-x--- 2  999 docker     4096 2月  18 16:57 performance_schema/
-rw------- 1  999 docker     1680 2月  18 16:57 private_key.pem
-rw-r--r-- 1  999 docker      452 2月  18 16:57 public_key.pem
-rw-r--r-- 1  999 docker     1112 2月  18 16:57 server-cert.pem
-rw------- 1  999 docker     1676 2月  18 16:57 server-key.pem
drwxr-x--- 2  999 docker    12288 2月  18 16:57 sys/
drwxr-x--- 2  999 docker     4096 2月  18 17:00 test/
root@docker:~# scp -r /data/mydb01/     192.168.172.132:/tmp/    
root@192.168.172.132's password: 
ib_buffer_pool                                 100% 1346   637.0KB/s   00:00    
auto.cnf                                       100%   56    27.2KB/s   00:00    
ib_logfile0                                    100%   48MB  12.8MB/s   00:03    
public_key.pem                                 100%  452   139.6KB/s   00:00    
help_keyword.frm                               100% 8612     2.3MB/s   00:00    
slow_log.CSV                                   100%    0     0.0KB/s   00:00    
time_zone_transition_type.frm                  100% 8748   889.5KB/s   00:00    
time_zone_leap_second.frm                      100% 8624     1.8MB/s   00:00    
procs_priv.frm                                 100% 8875     2.1MB/s   00:00    
func.MYI                                       100% 1024   103.6KB/s   00:00    
user.MYD                                       100%  512   154.0KB/s   00:00    
plugin.ibd                                     100%   96KB   1.3MB/s   00:00    
innodb_table_stats.frm                         100% 8830     2.0MB/s   00:00    
procs_priv.MYD                                 100%    0     0.0KB/s   00:00    
help_relation.ibd                              100% 2048   388.1KB/s   00:00    
slave_relay_log_info.ibd                       100%   96KB   6.1MB/s   00:00    
proc.frm                                       100% 9996   623.6KB/s   00:00    
proc.MYD                                       100%  294KB   4.7MB/s   00:00    


迁移节点:

[root@docker ~]# mkdir /data/mydb01 
[root@docker ~]# mv /tmp/*   /data/mydb01 
[root@docker ~]# ll /data/mydb01 
total 188484
-rw-r----- 1 root root       56 Feb 18 17:04 auto.cnf
-rw------- 1 root root     1680 Feb 18 17:04 ca-key.pem
-rw-r--r-- 1 root root     1112 Feb 18 17:04 ca.pem
-rw-r--r-- 1 root root     1112 Feb 18 17:04 client-cert.pem
-rw------- 1 root root     1680 Feb 18 17:04 client-key.pem
-rw-r----- 1 root root     1346 Feb 18 17:04 ib_buffer_pool
-rw-r----- 1 root root 79691776 Feb 18 17:04 ibdata1
-rw-r----- 1 root root 50331648 Feb 18 17:04 ib_logfile0
-rw-r----- 1 root root 50331648 Feb 18 17:04 ib_logfile1
-rw-r----- 1 root root 12582912 Feb 18 17:04 ibtmp1
drwxr-x--- 2 root root     4096 Feb 18 17:04 mysql
drwxr-x--- 2 root root     8192 Feb 18 17:04 performance_schema
-rw------- 1 root root     1680 Feb 18 17:04 private_key.pem
-rw-r--r-- 1 root root      452 Feb 18 17:04 public_key.pem
-rw-r--r-- 1 root root     1112 Feb 18 17:04 server-cert.pem
-rw------- 1 root root     1676 Feb 18 17:04 server-key.pem
drwxr-x--- 2 root root     8192 Feb 18 17:04 sys
drwxr-x--- 2 root root       58 Feb 18 17:04 test
[root@docker ~]# docker run --name mydb01 -d -p 3336:3306 -v /data/mydb01/:/var/lib/mysql/ -e "MYSQL_ROOT_PASSWORD=redhat" mysql:5.7 
4aa0488518f0a438e2aa027b90033dad21da8fe5070d58b6904c15644205c392
[root@docker ~]# docker exec  -it   /bin/bash 
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container
[root@docker ~]# docker exec  -it mydb01   /bin/bash 
root@4aa0488518f0:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use test;
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
mysql> select * from  student;
+------+--------+
| id   | name   |
+------+--------+
|    1 | tangmu |
|    2 | jierui |
+------+--------+
2 rows in set (0.01 sec)



版权声明:本文为Micky_Yang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。