为 Persistent Volume Claims 配置灾难恢复
要为 Application PVC 实现跨集群灾难恢复,请使用 Alauda Build of VolSync。
概述
Alauda Build of VolSync 是一个 operator,用于在集群内或跨集群对持久卷执行异步复制。VolSync 提供的复制功能独立于存储系统。这使得它能够向不通常支持远程复制的存储类型进行复制,或者从这些存储类型进行复制。此外,它还可以在不同类型(以及不同厂商)的存储之间进行复制。
术语
前提条件
- 下载与你的平台架构相对应的 Alauda Build of VolSync 安装包。
- 使用上架软件包机制将 Alauda Build of VolSync 安装包上传到 Primary 和 Secondary 集群。
- Alauda Container Platform Snapshot Management 已部署到 Primary 和 Secondary 集群。
- PVC 使用的存储必须由 CSI 提供,并支持 snapshot 功能。
部署 Alauda Build of VolSync
-
登录后,进入 Administrator 页面。
-
点击 Marketplace > OperatorHub,进入 OperatorHub 页面。
-
找到 Alauda Build of VolSync,点击 Install,然后进入 Install Alauda Build of VolSync 页面。
配置参数:
配置计划同步
为 PVC 配置计划同步后,VolSync 会按照指定的时间间隔,自动将数据从 ReplicationSource 同步到 ReplicationDestination。
本节说明如何将数据从 Primary 集群同步到 Secondary 集群。若要从 Secondary 同步到 Primary,请通过交换集群角色(primary 和 secondary)来调整下例。
创建 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 将获得一个从“cluster network”地址池分配的 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
与 Scheduled Synchronization 的唯一区别是,.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 同步
请参见 Configuring a Scheduled Synchronization
计划迁移
用户场景:
在两个集群都正常运行时,将业务服务从 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
请参见 Configuring a One-Time Synchronization
-
删除一次性同步
一次性同步完成后,删除一次性 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 集群同步。
请参见 Configuring a Scheduled Synchronization
故障切换
用户场景:
在 Primary 集群突然关闭后,将服务切换到 Secondary 集群。
操作步骤
为确保数据完整性(以防 primary 集群在同步期间发生故障),请在 Secondary 集群上执行一次本地同步。以从应用 PVC 的最后一个快照恢复出的 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
参数请参见 Configuring a One-Time Synchronization
-
等待同步完成
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。
故障恢复后回切(Failback)
用户场景:
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
请参见 Configuring a One-Time Synchronization
-
删除 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 同步
请参见 Configuring a Scheduled Synchronization