本文来自Rancher Labs
作者简介
鞠宏超,Rancher研发工程师。4年云计算领域经验,2018年加入Rancher Labs,先后参与了Longhorn产品研发,Rancher2.x产品研发,目前主要致力于Rancher企业版产品的设计与研发工作。
Longhorn是基于Kubernetes的轻量级分布式块存储系统,它完全开源,并且已经捐献给CNCF。随着云原生应用的普及,越来越多的服务提供容器运行时,数据的持久化存储问题渐渐显现出来,我们要做的不仅仅是数据的持久化,还要考虑备份的准确性,迁移的复杂性等。
Longhorn提供的分布式块存储可以在Kubernetes中直接使用持久化存储,它可以为数据卷在不同主机提供多副本服务,以保证数据的可靠性,它提供简洁的UI可以直接管理存储节点、数据卷,轻松实现数据卷的备份/定时备份,您还可以使用容灾备份功能,在不同集群创建容灾备份卷,并在发生紧急情况时及时进行故障转移。
在这篇文章中,我们将把MySQL作为Pod部署到集群A中,并使用Longhorn作为持久化存储卷,然后依托Rancher多集群管理的特性,结合Longhorn的容灾备份功能,演示一下如何优雅地将集群A中的MySQL应用及数据迁移到集群B中。

环境准备
-
Rancher (HA部署) - v2.4.8
-
Longhorn(通过Rancher UI Catalog部署) - v1.0.2
-
MySQL - 8
-
私有S3 - minio部署
-
业务集群A、B
部署longhorn
在Rancher中,进入集群A的system项目,在应用商店中可以一键启动Longhorn。


等待应用商店部署成功后,可以根据应用商店链接进入到Longhorn UI。


接下来,我们在集群B中以同样方式启动Longhorn,等待Longhorn正常运行。
创建s3访问的secret(用于longhorn备份使用)
访问Rancher,进入集群A的system项目,选择 资源 > 密文 菜单,在longhorn-system 命名空间中创建访问s3的secret,需要增加的参数包括:

访问Rancher,进入集群B的system项目,选择 资源 > 密文 菜单,在longhorn-system 命名空间中创建与上面相同的secret内容。

配置longhorn backup target
接下来我们需要在A和B两个集群的Longhorn中配置相同的backup target。
从应用商店的链接跳转到longhorn UI,点击 Setting > General 菜单,找到Backup分类,配置备份目标。

目前Longhorn支持NFS/S3两种备份方式,在本例中,我们使用minio搭建了一个私有S3服务,这里我们使用s3方式配置。
PS: 如果使用NFS,则需要保证NFS server支持NFSv4

部署MySQL应用
在集群A中部署一个MySQL应用,并在MySQL中创建测试表article并插入一条数据。

在集群A的Longhorn中对这个卷进行备份。

备份成功后,可以在集群B的Longhorn中看到备份信息(因为两个集群的Longhorn配置了相同的backup target,所以备份信息是共享的)。

创建容灾备份卷
访问集群B的Longhorn UI,使用集群A的Volume的备份,在集群B中创建一个容灾备份卷。


这里Volume的名称会根据备份卷自动填充,不建议手动修改。
保存后,访问 Volume 页面可以看到我们新创建的容灾备份卷。

在集群A的MySQL应用中再次插入几条数据。

访问集群A的Longhorn UI,对这个卷再次进行备份。这时可以看到集群B中的容灾备份卷图标变成了灰色,代表这个卷正在同步集群A中Volume的最新备份数据,此时无法激活和使用容灾备份卷。

等待一会,图标变成了蓝色,代表已经与集群A中Volume的最新备份同步成功。接下来我们就可以使用这个卷了。

如果在集群A中对MySQL卷设置了自动备份,集群B中的Longhorn会定时轮询最新的备份,将增量数据信息自动同步到容灾备份卷,以保持与集群A中Volume的数据一致。
在新集群中恢复mysql应用
假设此时我们的集群A已经无法使用了,我们可以在集群B使用最新的备份卷快速恢复MySQL应用。
首先,我们将集群B的容灾备份卷激活。

等待卷状态变成Detached以后,选择创建PV/PVC。

这里不建议修改PV和PVC的名称,namespace可根据实际集群B中的namespace名称进行填写。保存会,会在集群B的指定namespace中创建PVC。

访问Rancher可以看到PVC已经创建成功。

使用恢复的PVC创建MySQL应用,再查询一下数据,可以看到数据也恢复过来啦!大功告成!

总 结
随着云原生应用的普及,越来越多的服务可以依托Kubernetes运行,保证服务的稳定性和可靠性也渐渐成为难题,依托Longhorn的跨集群容灾备份功能,在Rancher中可以自动完成应用的编排、数据迁移,随时优雅的切换业务应用运行环境。
网友评论