从某国内知名共有云平台迁移至企鹅云存在着种种问题, 无法直接迁, 手动部署苦不堪言, 因此考虑采用镜像方式进行 copy.
首先各大公有云平台之间架构和实现方式稍有差异, 具体差异不做详细阐述, 因此如果想要云平台之间的镜像互用的话就需要进行一系列的前提操做才能够顺利迁移, 具体案例记录如下:
首先准备一台公有云 (非企鹅云) 的 centos 服务器;
创建好服务器后首先检查下该平台是否支持以下步骤的验证:
(1)确认当前内核是否支持 virtio 驱动
grep -i virtio /boot/config-$(uname -r)
如下图所示: 当前内核包含了 virtio_blk 和 virtio_net 驱动, 并且是以模块形式编译的(CONFIG_VIRTIO_BLK=m, 表示编译成为内核模块, 等于 y 表示编译进内核), 如果这一步没有找到 virtio_net 或 virtio_blk 的驱动信息, 那么该镜像 不支持 导入企鹅云.
如果内核支持 virtio 驱动 (virtio_blk 和 virtio_net 都支持), 且 virtio_blk 驱动编译进入了内核(即 CONFIG_VIRTIO_BLK=y), 则该内核支持导入, 不需要后续确认, 如果 virtio_blk 驱动是编译成内核模块的(即 CONFIG_VIRTIO_BLK=m), 则还需要继续后续确认步骤, 确认 virtio_blk 驱动正确包含进了 initramfs(或 initrd) 文件中.
(2)确认 initramfs 中是否包含 virtio_blk 驱动
lsinitrd /boot/initramfs-$(uname -r).img | grep virtio
如下图所示, initramfs 中包含了 virtio_blk 驱动, 以及其所依赖的 virtio.ko,virtio_pci.ko,virtio_ring.ko, 这样 initramfs 包含驱动正常, 该镜像可以导入.
验证通过后: 使用脚本运行:
- #!/bin/bash
- #author:wun
- # 企鹅云镜像导入准备脚本
- # 执行安装 cloud-init 前需要先确认系统是否有 BLK 和 net 模块
- #grep -i virtio /boot/config-$(uname -r) #检查内核是否支持 BLK 和 NET
- #lsinitrd /boot/initramfs-$(uname -r).img | grep virtio
- #cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
- #mkinitrd -f --with=virtio_blk --with=virtio_pci /boot/initramfs-$(uname -r).img $(uname -r)
- # 安装 cloud-init-17
- mkdir /migrate_QQ
- cd /migrate_QQ
pip install setuptools jinja2 prettytable oauthlib configobj pyyaml requests jsonpatch jsonschema six
- wget https://launchpad.net/cloud-init/trunk/17.1/+download/cloud-init-17.1.tar.gz
- tar -zxvf cloud-init-17.1.tar.gz
- cd ./cloud-init-17.1
python setup.py build
- python setup.py install --init-system systemd
- # 新建系统用户
- useradd syslog
- # 备份原配置文件
- cp /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.bak
- # 替换配置文件
- echo "users:
- default
- disable_root: 0
- ssh_pwauth: 1
- datasource_list: [ ConfigDrive, None ]
- datasource:
- ConfigDrive:
- dsmode: local
- cloud_init_modules:
- migrator
- bootcmd
- write-files
- growpart
- resizefs
- set_hostname
- update_hostname
- update_etc_hosts
- rsyslog
- users-groups
- ssh
- cloud_config_modules:
- mounts
- locale
- set-passwords
- rh_subscription
- yum-add-repo
- package-update-upgrade-install
- ntp
- timezone
- resolv_conf
- puppet
- chef
- salt-minion
- mcollective
- disable-ec2-metadata
- runcmd
- unverified_modules: ['resolv_conf']
- cloud_final_modules:
- rightscale_userdata
- scripts-per-once
- scripts-per-boot
- scripts-per-instance
- scripts-user
- ssh-authkey-fingerprints
- keys-to-console
- phone-home
- final-message
- power-state-change
- system_info:
- default_user:
- name: centos
- lock_passwd: false
- gecos: Cloud User
- groups: [wheel, adm, systemd-journal]
- sudo: ['ALL=(ALL) NOPASSWD:ALL']
- shell: /bin/bash
- distro: rhel
- paths:
- cloud_dir: /var/lib/cloud
- templates_dir: /etc/cloud/templates
- ssh_svcname: sshd
- #vim:syntax=yaml">/etc/cloud/cloud.cfg
- # 版本判断
- release=
- cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
- if [ "$release" = "7" ]; then
- #centos7
- systemctl enable cloud-init-local.service
- systemctl enable cloud-init.service
- systemctl enable cloud-config.service
- systemctl enable cloud-final.service
- systemctl start cloud-init-local.service
- systemctl start cloud-init.service
- systemctl start cloud-config.service
- systemctl start cloud-final.service
- elif [ "$release" = "6" ]; then
- #centos6
- chkconfig --add cloud-init-local
- chkconfig --add cloud-init
- chkconfig --add cloud-config
- chkconfig --add cloud-final
- chkconfig cloud-init-local on
- chkconfig cloud-init on
- chkconfig cloud-config on
- chkconfig cloud-final on
- fi
- # 备份原配置文件
- cp /lib/systemd/system/cloud-init-local.service /lib/systemd/system/cloud-init-local.service.bak
- cp /lib/systemd/system/cloud-init.service /lib/systemd/system/cloud-init.service.bak
- # 替换配置文件
- echo "[Unit]
- Description=Initial cloud-init job (pre-networking)
- Wants=network-pre.target
- After=systemd-remount-fs.service
- Before=NetworkManager.service
- Before=network-pre.target
- Before=shutdown.target
- Conflicts=shutdown.target
- RequiresMountsFor=/var/lib/cloud
- [Service]
- Type=oneshot
- ExecStart=/usr/bin/cloud-init init --local
- ExecStart=/bin/touch /run/cloud-init/network-config-ready
- RemainAfterExit=yes
- TimeoutSec=0
- #Output needs to appear in instance console output
- StandardOutput=journal+console
- [Install]
- WantedBy=cloud-init.target">/.service
- # 替换配置文件
- echo "[Unit]
- Description=Initial cloud-init job (metadata service crawler)
- Wants=cloud-init-local.service
- Wants=sshd-keygen.service
- Wants=sshd.service
- After=cloud-init-local.service
- After=systemd-networkd-wait-online.service
- After=networking.service
- After=systemd-hostnamed.service
- Before=network-online.target
- Before=sshd-keygen.service
- Before=sshd.service
- Before=systemd-user-sessions.service
- Conflicts=shutdown.target
- [Service]
- Type=oneshot
- ExecStart=/usr/bin/cloud-init init
- RemainAfterExit=yes
- TimeoutSec=0
- #Output needs to appear in instance console output
- StandardOutput=journal+console
- [Install]
- WantedBy=cloud-init.target">/.service
- # 初始化
- cloud-init init --local
- rm -rf /var/lib/cloud
导入以上脚本后执行脚本, 如果脚本未报错即证明准备工作已经完成, 接下来是通过共有云平台自带的镜像导出功能将镜像导出至对应的公有云平台的对象存储中......
至此源端的准备工作到此完成, 剩下的就是通过企鹅的 COS_migrate 工具将刚才导出的镜像进行 migrate, 具体步骤如下:
获取工具
下载链接: https://github.com/tencentyun/cos_migrate_tool_v5
解压缩工具包
Windows
解压并保存到某个目录, 例如
C:\Users\Administrator\Downloads\cos_migrate
Linux
解压并保存到某个目录
unzip cos_migrate_tool_v5-master.zip && cd cos_migrate_tool_v5-master
迁移工具结构
正确解压后的 COS Migration 工具目录结构如下所示:
- COS_Migrate_tool
- |--conf #配置文件所在目录
- | |--config.ini #迁移配置文件
- |--db #存储迁移成功的记录
- |--dep #程序主逻辑编译生成的 JAR 包
- |--log #工具执行中生成的日志
- |--opbin #用于编译的脚本
- |--src #工具的源码
- |--tmp #临时文件存储目录
- |--pom.xml #项目配置文件
- |--README #说明文档
- |--start_migrate.sh #Linux 下迁移启动脚本
- |--start_migrate.bat #Windows 下迁移启动脚本
说明:
db 目录主要记录工具迁移成功的文件标识, 每次迁移任务会优先对比 db 中的记录, 若当前文件标识已被记录, 则会跳过当前文件, 否则进行文件迁移.
log 目录记录着工具迁移时的所有日志, 若在迁移过程中出现错误, 请先查看该目录下的 error.log.
修改 config.ini 配置文件
在执行迁移启动脚本之前, 需先进行 config.ini 配置文件修改(路径:./conf/config.ini),config.ini 内容可以分为以下几部分:
3.1 配置迁移类型
type 表示迁移类型, 用户根据迁移需求填写对应的标识. 例如, 需要将本地数据迁移至 COS, 则 [migrateType] 的配置内容是 type=migrateLocal.
- [migrateType]
- type=migrateLocal
3.2 配置迁移任务
用户根据实际的迁移需求进行相关配置, 主要包括迁移至目标 COS 信息配置及迁移任务相关配置.
- # 迁移工具的公共配置分节, 包含了要迁移到得目标 COS 的账户信息
- [common]
- secretId=AKIDXXXXXXXXXXXXXXXXX
- secretKey=GYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
- bucketName=mybcket-1251668577
- region=ap-guangzhou
- storageClass=Standard
- cosPath=/
- https=off
- tmpFolder=./tmp
- smallFileThreshold=5242880
- smallFileExecutorNum=64
- bigFileExecutorNum=8
- entireFileMd5Attached=on
- daemonMode=off
- daemonModeInterVal=60
- executeTimeWindow=00:00,24:00
3.3 配置数据源信息
根据 [migrateType] 的迁移类型配置相应的分节. 例如 [migrateType] 的配置内容是 type=migrateLocal, 则用户只需配置 [migrateLocal] 分节即可.
3.3.1 配置本地数据源 migrateLocal
若从本地迁移至 COS, 则进行该部分配置, 具体配置项及说明如下:
- # 从本地迁移到 COS 配置分节
- [migrateLocal]
- localPath=E:\code\java\workspace\cos_migrate_tool\test_data
- exeludes=
- ignoreModifiedTimeLessThanSeconds=
运行迁移工具
Windows
双击 start_migrate.bat 即可运行.
Linux
1. 从 config.ini 配置文件读入配置, 运行命令为:
sh start_migrate.sh
2. 部分参数从命令行读入配置, 运行命令为:
sh start_migrate.sh -Dcommon.cosPath=/savepoint0403_10/
ok 如果一切顺利的话已经将镜像传输至 COS 中, 之后就只需要在平台内导入镜像即可完成, 镜像方式迁移.
来源: http://blog.51cto.com/13528213/2126325