进入云架构时代以来,随着公有云成为越来越多客户的选择,云迁移成为必不可少的服务。
在当下,容器具有其跨平台、资源利用率高、敏捷、具有可持续部署和测试等诸多优势,被越来越多的企业接纳。容器结合公有云的弹性和云网络的优势,可谓是完美的结合,越来越多的企业会考虑在云上使用容器集群,本文就以企业自建kubernetes上云迁移至金山云kubernetes集群为例,进行容器迁移上云的方案介绍。
在迁移前,需确定数据传输方式,如果采用专线或VPN传输,需提前做好专线施工、网络调试,本文主要介绍源端和目标端网络连通后的迁移过程。具体过程介绍如下:
具有容器服务集群备份或者迁移需求的场景下,一般考采用Velero集成Restic工具实现,该工具适用以下场景:
Velero是一个提供 Kubernetes 集群和持久卷的备份、迁移以及灾难恢复等的开源工具。金山云开发了针对Velero的插件以及Velero集成Restic方案,可以实现容器服务的备份、迁移。本文主要介绍Velero集成Restic方案实现Kubernetes集群迁移。
Velero支持集成Restic工具来备份和还原存储卷,Restic除了支持块存储之外,还支持更多类型的存储,比如 NFS,Emptydir、Local以及其他不支持快照的任何存储类型。当前Restic不支持HostPath类型的存储,支持Local类型的PV存储卷。
请参考以下步骤在自建kubernetes集群及金山云kubernetes集群中部署velero。
wget https://ks3-cn-beijing.ksyun.com/velero/velero-v1.2.0-linux-amd64.tar.gz
解压:
tar -zxvf velero-v1.2.0-linux-amd64.tar.gz
将 velero二进制文件移到PATH环境变量定义的目录下:
cp velero /bin
验证是否安装成功:
velero -h
金山云KS3上创建2个bucket。
获取ak/sk,ak/sk的主要目的是实现对金山云对象存储、块存储、块存储快照的操作权限。
连接本地集群无需单独指定,如果在本地执行目标端velero安装,则进行配置。容器服务的config文件可以通过集群管理 > 集群详情页 > 集群基本信息 > 获取集群config获得。
说明:velero默认会从$HOME/.kube目录下查找文件名为 config 的文件,如果将在目标端安装,则需要指定 config的路径。命令中添加–kubeconfig
创建credentials-velero文件并设置ak/sk值。
vim /data/credentials-velero
[default]
ksyun_access_key_id = <your access_key>
ksyun_access_key_secret = <your secret_key>
velero服务安装:
velero install --provider ksyun \
--image hub.kce.ksyun.com/ksyun/velero:v1.3.0-beta.2 \
--plugins hub.kce.ksyun.com/ksyun/velero-plugin-ksyun:v1.2.0 \
--kubeconfig <KUBECONFIG> \
--bucket <YOUR_BUCKET> \
--backup-location-config region=<YOUR_REGION>,resticRepoPrefix=<resticRepoPrefix> \
--secret-file ./credentials-velero \
--use-volume-snapshots=false \
--use-restic
运行安装示例,注:示例中的两处bucket不能一样:
velero install --provider ksyun \
--image hub.kce.ksyun.com/ksyun/velero:v1.3.0-beta.2 \
--plugins hub.kce.ksyun.com/ksyun/velero-plugin-ksyun:v1.2.0 \
--kubeconfig /data/soukubeconfig \
--bucket test-b \
--backup-location-config region=cn-beijing-6,resticRepoPrefix=ks3:ks3-cn-beijing.ksyun.com/test-a \
--secret-file /data/credentials-velero \
--use-volume-snapshots=false \
--use-restic
执行:
kubectl logs deployment/velero -n velero
查看是否有错误。执行kubectl get pod -n velero -o wide查看namespace为velero的pod是否都启动并running正常。当restic-xxx pod显示CrashLoopBackOff,需要修改其中的存储卷位置信息:
kubectl get daemonset -n velero
执行:
kubectl edit ds restic -n velero
把 volumes 下的 kubelet 目录配置由 /var/lib/kubelet/pods 改成 /data/kubelet/pods,再次看pod运行是否正常。
使用restic备份时,需要明确指定需要备份的存储卷pod。velero通过pod的 annotation 来过滤需要备份存储卷的pod。对于需要备份存储卷的pod,我们需要执行如下命令设置 annotate。
kubectl -n YOUR_POD_NAMESPACE annotate pod/YOUR_POD_NAME backup.velero.io/backup-volumes=YOUR_VOLUME_NAME_1,YOUR_VOLUME_NAME_2,...
示例:
kubectl get pod -o yaml
查看volumes的名字。
kubectl -n default annotate pod/nginx-7bb7cd8db5-c9fjf backup.velero.io/backup-volumes=default-token-bs7q8
注:-n指定namespace,backup-volumes=volumes中的name。
velero backup create NAME OPTIONS...
示例:
velero backup create nginxbk20200313 --wait
恢复资源时,会启动一个 restic helper 容器来帮助数据的恢复,可以通过configmap来自定义配置该容器的配置,如需要配置则需要在执行恢复操作之前先部署如下 ConfigMap。
apiVersion: v1
kind: ConfigMap
metadata:
# any name can be used; Velero uses the labels (below)
# to identify it rather than the name
name: restic-restore-action-config
# must be in the velero namespace
namespace: velero
# the below labels should be used verbatim in your
# ConfigMap.
labels:
# this value-less label identifies the ConfigMap as
# config for a plugin (i.e. the built-in restic restore
# item action plugin)
velero.io/plugin-config: ""
# this label identifies the name and kind of plugin
# that this ConfigMap is for.
velero.io/restic: RestoreItemAction
data:
# The value for "image" can either include a tag or not;
# if the tag is *not* included, the tag from the main Velero
# image will automatically be used.
image: hub.kce.ksyun.com/ksyun/velero-restic-restore-helper:v1.2.0
# "cpuRequest" sets the request.cpu value on the restic init containers during restore.
# If not set, it will default to "100m". A value of "0" is treated as unbounded.
cpuRequest: 200m
# "memRequest" sets the request.memory value on the restic init containers during restore.
# If not set, it will default to "128Mi". A value of "0" is treated as unbounded.
memRequest: 128Mi
# "cpuLimit" sets the request.cpu value on the restic init containers during restore.
# If not set, it will default to "100m". A value of "0" is treated as unbounded.
cpuLimit: 200m
# "memLimit" sets the request.memory value on the restic init containers during restore.
# If not set, it will default to "128Mi". A value of "0" is treated as unbounded.
memLimit: 128Mi
velero restore create --from-backup BACKUP_NAME OPTIONS...
示例:
velero restore create restore-20200313 --from-backup bk20200313 --wait
velero restore get restore-20200313
注:
a、不指定create名,系统会自动生成,建议按照自己的规范命名,后面方便查看恢复的结果。
b、此处验证的是完全备份和完全恢复,恢复过程可能会提示部分失败,原因为目标端已经存在,可忽略。
kubectl get pod -n default -o wide
发现已经恢复并运行,可以通过访问pod应用进行验证。至此kubernetes应用及配置迁移完成。
kubectl delete namespace/velero clusterrolebinding/velero
kubectl delete crds -l component=velero
文档内容是否对您有帮助?
评价建议不能为空
非常感谢您的反馈,我们会继续努力做到更好!