0
1
0
0
专栏/.../

通过TiOperator备份数据到共享存储

 清风明月  发表于  2024-03-19

Kubernetes 上 TiDB 集群的数据备份到​持久卷​​​上。本文描述的持久卷,指任何 ​ ​Kubernetes 支持的持久卷类型​​。本文以备份数据到网络文件系统存储为例。

常用场景

如果你对数据备份有以下要求,可考虑使用 BR 将 TiDB 集群数据以 ​ ​Ad-hoc 备份​​(只执行一次)或​ ​定时快照备份​​(执行多次)的方式备份至持久卷:

  • 需要备份的数据量较大,而且要求备份速度较快
  • 需要直接备份数据的 SST 文件(键值对)

注意

  • BR 只支持 TiDB v3.1 及以上版本。
  • 使用 BR 备份出的数据只能恢复到 TiDB 数据库中,无法恢复到其他数据库中

一、Ad-hoc 备份

d-hoc 备份支持快照备份与增量备份。Ad-hoc 备份通过创建一个自定义的 ​​Backup​​ custom resource (CR) 对象来描述一次备份。TiDB Operator 根据这个 ​​Backup​​ 对象来完成具体的备份过程。如果备份过程中出现错误,程序不会自动重试,此时需要手动处理。

本文档对K8S集群中命名空间为tidb下的lqb数据库集群进行备份,具体操作如下:

准备Ad-hoc备份环

1. 下载​ ​backup-rbac.yaml

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tidb-backup-manager
  labels:
    app.kubernetes.io/component: tidb-backup-manager
rules:
- apiGroups: [""]
  resources: ["events"]
  verbs: ["*"]
- apiGroups: ["pingcap.com"]
  resources: ["backups", "restores"]
  verbs: ["get", "watch", "list", "update"]

---
kind: ServiceAccount
apiVersion: v1
metadata:
  name: tidb-backup-manager

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tidb-backup-manager
  labels:
    app.kubernetes.io/component: tidb-backup-manager
subjects:
- kind: ServiceAccount
  name: tidb-backup-manager
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: tidb-backup-manager

2. 创建备份需要的RBAC相关资源在tidb命名空间下

[root@k8s-master]# kubectl apply -f backup-rbac.yaml -ntidb
role.rbac.authorization.k8s.io/tidb-backup-manager created
serviceaccount/tidb-backup-manager created
rolebinding.rbac.authorization.k8s.io/tidb-backup-manager created

3. 确保可以从K8S集群中访问用于存储备份数据的共享服务器,并且配置了TiKV挂载跟备份任务相同的本地目录

spec:
  tikv:
    baseImage: pingcap/tikv
    replicas: 4
    maxFailoverCount: 6

#下边是配置tikv挂载nfs
    additionalVolumes:
    - name: nfs
      nfs:
        server: 172.16.5.194
        path: /home/k8s-nfs
    additionalVolumeMounts:
    - name: nfs
      mountPath: /nfs

    requests:
      cpu: "6c"
      storage: 12Gi
      memory: "400Mi"
    limits:
      cpu: "6c"
      memory: "12Gi"
    mountClusterClientSecret: false
    storageClassName: "local-hostpath"

4. 备份数据到共享持久卷

1. 创建备份的自定义资源CR,将数据备份到共享存储卷

##以下以备份yz数据库为例
[root@k8s-master backup]# cat backup-nfs.yaml
---
apiVersion: pingcap.com/v1alpha1
kind: Backup
metadata:
  name: demo1-backup-nfs
  namespace: tidb
spec:
  tableFilter:
  - "yz.*"

  br:
    cluster: yz
    clusterNamespace: tidb

  local:
    prefix: backup-nfs
    volume:
      name: nfs
      nfs:
        server: 172.16.5.194
        path: /home/k8s-nfs
    volumeMount:
      name: nfs
      mountPath: /nfs

2. 创建应用配置
[root@k8s-master]# kubectl apply -f backup-nfs.yaml
backup.pingcap.com/demo1-backup-nfs created

3.查看备份状态和备份文件
[root@k8s-master]# kubectl get bk -n tidb -owide -w
NAME               TYPE   MODE       STATUS    BACKUPPATH                BACKUPSIZE   COMMITTS   LOGTRUNCATEUNTIL   STARTED   COMPLETED   AGE
demo1-backup-nfs          snapshot   Running   local:///nfs/backup-nfs                                                                    57s
demo1-backup-nfs          snapshot   Complete   local:///nfs/backup-nfs   2.8 GB       439178019024666631                      68s       0s          72s

[root@k8s-master]ls /home/k8s-nfs/backup-nfs/de
1  20001  39461  4  backup.lock  backupmeta  checkpoint.meta  checkpoints

5. 备份实例

5.1 备份全部集群数据
---
apiVersion: pingcap.com/v1alpha1
kind: Backup
metadata:
  name: demo1-backup-nfs
  namespace: test1
spec:
  br:
    cluster: demo1
    clusterNamespace: test1
  local:
    prefix: backup-nfs
    volume:
      name: nfs
      nfs:
        server: ${nfs_server_ip}
        path: /home/k8s-nfs
    volumeMount:
      name: nfs
      mountPath: /nfs

5.2. 备份单个数据库的数据

---
apiVersion: pingcap.com/v1alpha1
kind: Backup
metadata:
  name: demo1-backup-nfs
  namespace: test1
spec:
  tableFilter:
  - "db1.*"
  br:
    cluster: demo1
    clusterNamespace: test1
  local:
    prefix: backup-nfs
    volume:
      name: nfs
      nfs:
        server: ${nfs_server_ip}
        path: /home/k8s-nfs
    volumeMount:
      name: nfs
      mountPath: /nfs

5.3. 备份单张表的数据

---
apiVersion: pingcap.com/v1alpha1
kind: Backup
metadata:
  name: demo1-backup-nfs
  namespace: test1
spec:
  tableFilter:
  - "db1.table1"
  br:
    cluster: demo1
    clusterNamespace: test1
  local:
    prefix: backup-nfs
    volume:
      name: nfs
      nfs:
        server: ${nfs_server_ip}
        path: /home/k8s-nfs
    volumeMount:
      name: nfs
      mountPath: /nfs

5.4. 使用表库过滤功能备份多张表的数据

---
apiVersion: pingcap.com/v1alpha1
kind: Backup
metadata:
  name: demo1-backup-nfs
  namespace: test1
spec:
  tableFilter:
  - "db1.table1"
  - "db1.table2"
  br:
    cluster: demo1
    clusterNamespace: test1
  local:
    prefix: backup-nfs
    volume:
      name: nfs
      nfs:
        server: ${nfs_server_ip}
        path: /home/k8s-nfs
    volumeMount:
      name: nfs
      mountPath: /nfs

二、查看备份状态和备份文件

[root@k8s-master backup]# kubectl get bks -n tidb -owide
NAME                        SCHEDULE      MAXBACKUPS   LASTBACKUP   LASTBACKUPTIME   AGE
demo1-backup-schedule-nfs   */2 * * * *                                              9s
NAME                        SCHEDULE      MAXBACKUPS   LASTBACKUP   LASTBACKUPTIME   AGE
demo1-backup-schedule-nfs   */2 * * * *                                              46s


demo1-backup-schedule-nfs   */2 * * * *                demo1-backup-schedule-nfs-2024-02-02t10-16-00   6s               89s
demo1-backup-schedule-nfs   */2 * * * *                demo1-backup-schedule-nfs-2024-02-02t10-18-00   6s               3m29s
demo1-backup-schedule-nfs   */2 * * * *                demo1-backup-schedule-nfs-2024-02-02t10-20-00   6s               5m29s
demo1-backup-schedule-nfs   */2 * * * *                demo1-backup-schedule-nfs-2024-02-02t10-22-00   6s               7m29s
demo1-backup-schedule-nfs   */2 * * * *                demo1-backup-schedule-nfs-2024-02-02t10-24-00   6s               9m29s

[root@k8s-master ~]# ls /home/k8s-nfs/backup-nfs/yz-pd.tidb-2379-2024-02-02t10-16-00/
1  20001  39461  4  backup.lock  backupmeta  checkpoint.meta  checkpoints

三、删除备份的 Backup CR

kubectl delete backup ${name} -n ${namespace}
kubectl delete backupschedule ${name} -n ${namespace}

0
1
0
0

版权声明:本文为 TiDB 社区用户原创文章,遵循 CC BY-NC-SA 4.0 版权协议,转载请附上原文出处链接和本声明。

评论
暂无评论