为持久卷声明配置灾难恢复
要为 Application PVC 实现跨集群灾难恢复,请使用 Alauda Build of VolSync。
概述
Alauda Build of VolSync 是一个 operator,用于在集群内或跨集群对持久卷进行异步复制。VolSync 提供的复制与底层存储系统无关,因此可以对通常不支持远程复制的存储类型进行双向复制。此外,它还可以在不同类型(以及不同厂商)的存储之间进行复制。
术语
前提条件
- 下载与你的平台架构对应的 Alauda Build of VolSync 安装包。
- 使用上架软件包机制将 Alauda Build of VolSync 安装包上传到 Primary 和 Secondary 两个集群。
- 两个集群都已部署 快照管理。
- PVC 所使用的存储必须由 CSI 提供,并支持 snapshot 功能。
部署 Alauda Build of VolSync
-
登录后,进入 管理员 页面。
-
单击 Marketplace > OperatorHub,进入 OperatorHub 页面。
-
找到 Alauda Build of VolSync,单击 Install,进入 Install Alauda Build of VolSync 页面。
配置参数:
配置定时同步
完成 PVC 的定时同步配置后,VolSync 会按照指定的间隔,自动将数据从 ReplicationSource 同步到 ReplicationDestination。
下面配置的是从 Primary 集群到 Secondary 集群的同步。如果需要从 Secondary 集群同步到 Primary 集群,请通过交换集群角色来调整下面的示例。
创建 rsync-tls Data Mover Secret
在 Primary 和 Secondary 两个集群上都创建该 Secret;如果 Secret 已存在,则跳过此步骤。
apiVersion: v1
data:
psk.txt: <psk>
kind: Secret
metadata:
name: <name>
namespace: <namespace>
type: Opaque
apiVersion: v1
data:
# echo -n 1:23b7395fafc3e842bd8ac0fe142e6ad1 | base64
psk.txt: MToyM2I3Mzk1ZmFmYzNlODQyYmQ4YWMwZmUxNDJlNmFkMQ==
kind: Secret
metadata:
name: volsync-rsync-tls
namespace: default
type: Opaque
参数:
创建 ReplicationDestination 资源
在 Secondary 集群上创建 ReplicationDestination
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationDestination
metadata:
name: rd-<pvc-name>
namespace: <namespace>
spec:
rsyncTLS:
copyMethod: Snapshot
destinationPVC: <pvc-name>
keySecret: <key-secret>
serviceType: <service-type>
storageClassName: <storageclass-name>
volumeSnapshotClassName: <volumesnapshotclass-name>
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
EOF
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationDestination
metadata:
name: rd-pvc-01
namespace: default
spec:
rsyncTLS:
copyMethod: Snapshot
destinationPVC: pvc-01
keySecret: volsync-rsync-tls
serviceType: NodePort
storageClassName: sc-cephfs
volumeSnapshotClassName: csi-cephfs-snapshotclass
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
EOF
参数:
NOTE
关于 service type
如果指定 ClusterIP,Service 将接收从“集群网络”地址池分配的 IP 地址。默认情况下,这组地址无法从集群外部访问,因此不太适合跨集群复制。不过,诸如 Submariner 之类的网络附加组件可以打通集群网络,因此这是一个不错的选择。
如果指定 LoadBalancer,将分配一个可从外部访问的 IP 地址。这需要集群支持负载均衡器,例如云厂商提供的负载均衡器,或者在物理集群场景下使用 MetalLB。虽然这是在云环境中分配可访问地址的最简单方式,但负载均衡器通常会产生额外成本,并且数量有限。
创建 ReplicationSource 资源
在 Primary 集群上创建 ReplicationSource
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-<pvc-name>
namespace: <namespace>
spec:
rsyncTLS:
address: <address>
copyMethod: Snapshot
keySecret: <key-secret>
port: <port>
storageClassName: <storageclass-name>
volumeSnapshotClassName: <volumesnapshotclass-name>
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: <pvc-name>
trigger:
schedule: <schedule>
EOF
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-pvc-01
namespace: default
spec:
rsyncTLS:
address: 192.168.129.201
copyMethod: Snapshot
keySecret: volsync-rsync-tls
port: 30532
storageClassName: sc-cephfs
volumeSnapshotClassName: csi-cephfs-snapshotclass
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: pvc-01
trigger:
schedule: "*/10 * * * *"
EOF
参数:
检查同步状态
检查来自 ReplicationSource 的同步状态
kubectl -n <namespace> get ReplicationSource <rs-name> -o jsonpath='{.status}'
kubectl -n default get ReplicationSource rs-pvc-01 -o jsonpath='{.status}'
上一次同步完成于 .status.lastSyncTime,耗时 .status.lastSyncDuration 秒。
下一次计划同步时间为 .status.nextSyncTime。
配置一次性同步
一次性同步由手动触发。通过在 ReplicationSource 资源的 trigger 规范下为 manual 字段设置一个唯一字符串来控制。应用该配置后,同步任务会立即运行一次。
创建一次性 ReplicationSource 资源
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-<pvc-name>-latest
namespace: <namespace>
spec:
rsyncTLS:
address: <address>
copyMethod: Snapshot
keySecret: <key-secret>
port: <port>
storageClassName: <storageclass-name>
volumeSnapshotClassName: <volumesnapshotclass-name>
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: <pvc-name>
trigger:
manual: <manual-id>
EOF
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-pvc-01-latest
namespace: default
spec:
rsyncTLS:
address: 192.168.129.201
copyMethod: Snapshot
keySecret: volsync-rsync-tls
port: 30532
storageClassName: sc-cephfs
volumeSnapshotClassName: csi-cephfs-snapshotclass
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: pvc-01
trigger:
manual: latest
与 定时同步 的唯一区别是 .spec.trigger 应设置为 manual。
检查同步状态
kubectl -n <namespace> get ReplicationSource <rs-name> -o jsonpath='{.status.lastManualSync}'
kubectl -n default get ReplicationSource rs-pvc-01-latest -o jsonpath='{.status.lastManualSync}'
如果输出与 <manual-id> 匹配,则表示同步已完成。
为应用 PVC 启用灾难恢复
部署有状态应用
- 在 Primary 集群上部署有状态应用
单击查看
cat << EOF | kubectl create -f -
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-01
namespace: default
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
storageClassName: sc-cephfs
volumeMode: Filesystem
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ubuntu
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: ubuntu
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: ubuntu
spec:
affinity: {}
containers:
- command:
- sleep
- infinity
image: registry.alauda.cn:60070/ops/ubuntu:latest
imagePullPolicy: Always
name: ubuntu
resources: {}
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /data
name: data
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
terminationGracePeriodSeconds: 30
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-01
EOF
-
在 Secondary 集群上创建应用 PVC
cat << EOF | kubectl create -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-01
namespace: default
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
storageClassName: sc-cephfs
volumeMode: Filesystem
EOF
配置 PVC 灾难恢复
设置 Primary-to-Secondary 同步
参见 配置定时同步
计划迁移
用户场景:
在两个集群都正常运行时,将业务服务从 Primary 集群迁移到 Secondary 集群。
操作步骤
-
缩减应用 Pod
在 Primary 集群上缩减所有使用 dr PVC 的应用 Pod。
-
删除 ReplicationSource 资源
在 Primary 集群上删除 ReplicationSource
kubectl -n <namespace> delete ReplicationSource <rs-name>
kubectl -n default delete ReplicationSource rs-pvc-01
-
创建一次性同步
从 Primary 集群发起一次同步任务,确保 Secondary 集群中的数据是 up-to-date。
在 Primary 集群上创建 ReplicationSource
参见 配置一次性同步
-
删除一次性同步
一次性同步完成后,删除一次性 ReplicationSource 资源
kubectl -n <namespace> delete ReplicationSource <rs-name>
kubectl -n default delete ReplicationSource rs-pvc-01-latest
-
删除 ReplicationDestination 资源
在 Secondary 集群上删除 ReplicationDestination
kubectl -n <namespace> delete ReplicationDestination <rd-name>
kubectl -n default delete ReplicationDestination rd-pvc-01
-
扩容应用 Pod
在 Secondary 集群上扩容所有使用 dr PVC 的应用 Pod。
-
建立 Secondary-to-Primary 同步
通过在 Primary 集群上创建 ReplicationDestination,并在 Secondary 集群上创建 ReplicationSource,为 PVC 灾难恢复建立 Secondary-to-Primary 集群同步。
参见 配置定时同步
故障切换
用户场景:
Primary 集群突然关闭后,将服务切换到 Secondary 集群。
操作步骤
为确保数据完整性(以防 primary 集群在同步过程中发生故障),请在 Secondary 集群上执行本地同步。使用从应用 PVC 的最后一个 snapshot 恢复得到的 PVC 作为源端,使用应用当前的 PVC 作为目标端来执行数据同步。
-
恢复 PVC
在 Secondary 集群上从 ReplicationDestination 恢复 PVC
cat << EOF | kubectl create -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restored-<pvc-name>
namespace: <namespace>
spec:
accessModes: [<access-modes>]
dataSourceRef:
kind: ReplicationDestination
apiGroup: volsync.backube
name: <rd-name>
resources:
requests:
storage: <pvc-size>
storageClassName: <storageclass-name>
EOF
cat << EOF | kubectl create -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restored-pvc-01
namespace: default
spec:
accessModes: [ReadWriteMany]
dataSourceRef:
kind: ReplicationDestination
apiGroup: volsync.backube
name: rd-pvc-01
resources:
requests:
storage: 10Gi
storageClassName: sc-cephfs
EOF
-
创建本地 ReplicationSource 资源
在 Secondary 集群上创建 ReplicationSource 资源
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-<pvc-name>-local
namespace: <namespace>
spec:
rsyncTLS:
address: <address>
copyMethod: Snapshot
keySecret: <key-secret>
port: <port>
storageClassName: <storageclass-name>
volumeSnapshotClassName: <volumesnapshotclass-name>
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: restored-<pvc-name>
trigger:
manual: <manual-id>
EOF
cat << EOF | kubectl create -f -
apiVersion: volsync.backube/v1alpha1
kind: ReplicationSource
metadata:
name: rs-pvc-01-local
namespace: default
spec:
rsyncTLS:
address: 192.168.129.201
copyMethod: Snapshot
keySecret: volsync-rsync-tls
port: 30532
storageClassName: sc-cephfs
volumeSnapshotClassName: csi-cephfs-snapshotclass
moverSecurityContext:
fsGroup: 65534
runAsGroup: 65534
runAsNonRoot: true
runAsUser: 65534
seccompProfile:
type: RuntimeDefault
sourcePVC: restored-pvc-01
trigger:
manual: latest
EOF
参数参见 配置一次性同步
-
等待同步完成
kubectl -n <namespace> get ReplicationSource <rs-name> -o jsonpath='{.status.lastManualSync}'
kubectl -n default get ReplicationSource rs-pvc-01-local -o jsonpath='{.status.lastManualSync}'
如果输出与 <manual-id> 匹配,则表示同步已完成。
-
删除本地 ReplicationSource
在 Secondary 集群上删除本地 ReplicationSource
kubectl -n <namespace> delete ReplicationSource <rs-name>
kubectl -n default delete ReplicationSource rs-pvc-01-local
-
删除 ReplicationDestination
在 Secondary 集群上删除 ReplicationDestination
kubectl -n <namespace> delete ReplicationDestination <rd-name>
kubectl -n default delete ReplicationDestination rd-pvc-01
-
扩容应用 Pod
在 Secondary 集群上扩容所有应用 Pod。
故障恢复后回切
用户场景:
Primary 集群现已恢复并可正常运行,因此需要将服务切回该集群。
操作步骤
-
缩减 Primary 集群上的应用 Pod
当 primary 集群重新上线后,应用 Pod 会自动恢复。但是,必须先缩减服务以停止流量。在将 secondary 集群中的最新数据同步到 primary 集群后,再扩容应用以恢复正常运行。
-
删除 Primary 集群上的 ReplicationSource
需要先删除在 Primary 集群故障前创建的 ReplicationSource。
kubectl -n <namespace> delete ReplicationSource <rs-name>
kubectl -n default delete ReplicationSource rs-pvc-01
-
从 Secondary 集群同步最新数据
建立 Secondary-to-Primary 一次性同步。
在 Primary 集群上创建 ReplicationDestination,然后在 Secondary 集群上创建一次性 ReplicationSource
参见 配置一次性同步
-
删除 ReplicationDestination 和 ReplicationSource
数据同步完成后,删除一次性资源
在 Secondary 集群上删除 ReplicationSource
kubectl -n <namespace> delete ReplicationSource <rs-name>
kubectl -n default delete ReplicationSource rs-pvc-01-latest
在 Primary 集群上删除 ReplicationDestination
kubectl -n <namespace> delete ReplicationDestination <rd-name>
kubectl -n default delete ReplicationDestination rd-pvc-01
-
迁移应用
在 Secondary 集群上缩减应用 Pod
在 Primary 集群上扩容应用 Pod
-
建立 Primary-to-Secondary 同步
参见 配置定时同步