使用 BR 工具恢复 S3 兼容存储上的备份数据
本文使用的恢复方式基于 TiDB Operator 新版(v1.1 及以上)的 Custom Resource Definition (CRD) 实现。
以下示例将 Amazon S3 的存储(指定路径)上的备份数据恢复到 AWS Kubernetes 环境中的 TiDB 集群。
下载文件 ,并执行以下命令在 这个 namespace 中创建备份需要的 RBAC 相关资源:
创建
s3-secret
secret。该 secret 存放用于访问 S3 兼容存储的凭证。kubectl create secret generic s3-secret --from-literal=access_key=xxx --from-literal=secret_key=yyy --namespace=test2
创建
restore-demo2-tidb-secret
secret。该 secret 存放用于访问 TiDB 集群的 root 账号和密钥。kubectl create secret generic restore-demo2-tidb-secret --from-literal=password=${password} --namespace=test2
下载文件 backup-rbac.yaml,并执行以下命令在
test2
这个 namespace 中创建备份需要的 RBAC 相关资源:kubectl apply -f backup-rbac.yaml -n test2
创建
restore-demo2-tidb-secret
secret。该 secret 存放用于访问 TiDB 集群的 root 账号和密钥:kubectl create secret generic restore-demo2-tidb-secret --from-literal=password=${password} --namespace=test2
创建 IAM 角色:
可以参考 来为账号创建一个 IAM 角色,并且通过 AWS 官方文档为 IAM 角色赋予需要的权限。由于
Restore
需要访问 AWS 的 S3 存储,所以这里给 IAM 赋予了AmazonS3FullAccess
的权限。绑定 IAM 到 TiKV Pod:
kubectl edit tc demo2 -n test2
找到
spec.tikv.annotations
, 增加 annotationarn:aws:iam::123456789012:role/user
, 然后退出编辑, 等到 TiKV Pod 重启后,查看 Pod 是否加上了这个 annotation。
下载文件 ,并执行以下命令在
test2
这个 namespace 中创建备份需要的 RBAC 相关资源:创建
restore-demo2-tidb-secret
secret。该 secret 存放用于访问 TiDB 集群的 root 账号和密钥:kubectl create secret generic restore-demo2-tidb-secret --from-literal=password=${password} --namespace=test2
在集群上为服务帐户启用 IAM 角色:
可以参考 AWS 官方文档开启所在的 EKS 集群的 IAM 角色授权。
创建 IAM 角色:
可以参考 创建一个 IAM 角色,为角色赋予
AmazonS3FullAccess
的权限,并且编辑角色的Trust relationships
。绑定 IAM 到 ServiceAccount 资源上:
kubectl annotate sa tidb-backup-manager -n eks.amazonaws.com/role-arn=arn:aws:iam::123456789012:role/user --namespace=test2
将 ServiceAccount 绑定到 TiKV Pod:
kubectl edit tc demo2 -n test2
注意:
arn:aws:iam::123456789012:role/user
为步骤 4 中创建的 IAM 角色。
创建
Restore
CR,通过 accessKey 和 secretKey 授权的方式恢复集群:kubectl apply -f resotre-aws-s3.yaml
restore-aws-s3.yaml
文件内容如下:---
apiVersion: pingcap.com/v1alpha1
kind: Restore
metadata:
name: demo2-restore-s3
namespace: test2
spec:
br:
cluster: demo2
clusterNamespace: test2
# logLevel: info
# statusAddr: ${status_addr}
# rateLimit: 0
# timeAgo: ${time}
# sendCredToTikv: true
to:
host: ${tidb_host}
port: ${tidb_port}
user: ${tidb_user}
secretName: restore-demo2-tidb-secret
s3:
provider: aws
secretName: s3-secret
region: us-west-1
bucket: my-bucket
prefix: my-folder
创建
Restore
CR,通过 IAM 绑定 Pod 授权的方式备份集群:restore-aws-s3.yaml
文件内容如下:---
apiVersion: pingcap.com/v1alpha1
kind: Restore
metadata:
name: demo2-restore-s3
namespace: test2
annotations:
iam.amazonaws.com/role: arn:aws:iam::123456789012:role/user
spec:
br:
cluster: demo2
sendCredToTikv: false
clusterNamespace: test2
# logLevel: info
# statusAddr: ${status_addr}
# concurrency: 4
# rateLimit: 0
# timeAgo: ${time}
# checksum: true
to:
host: ${tidb_host}
port: ${tidb_port}
user: ${tidb_user}
secretName: restore-demo2-tidb-secret
s3:
region: us-west-1
bucket: my-bucket
创建
Restore
CR,通过 IAM 绑定 ServiceAccount 授权的方式备份集群:kubectl apply -f restore-aws-s3.yaml
restore-aws-s3.yaml
文件内容如下:---
apiVersion: pingcap.com/v1alpha1
kind: Restore
metadata:
name: demo2-restore-s3
namespace: test2
spec:
serviceAccount: tidb-backup-manager
br:
cluster: demo2
sendCredToTikv: false
clusterNamespace: test2
# logLevel: info
# statusAddr: ${status_addr}
# concurrency: 4
# rateLimit: 0
# timeAgo: ${time}
# checksum: true
to:
host: ${tidb_host}
port: ${tidb_port}
user: ${tidb_user}
secretName: restore-demo2-tidb-secret
s3:
provider: aws
region: us-west-1
bucket: my-bucket
prefix: my-folder
创建好 Restore
CR 后,可通过以下命令查看恢复的状态:
kubectl get rt -n test2 -o wide
更多 Restore
CR 字段的详细解释:
.spec.metadata.namespace
:Restore
CR 所在的 namespace。.spec.to.host
:待恢复 TiDB 集群的访问地址。.spec.to.port
:待恢复 TiDB 集群的访问端口。.spec.to.user
:待恢复 TiDB 集群的访问用户。.spec.to.tidbSecretName
:待恢复 TiDB 集群.spec.to.user
用户的密码所对应的 secret。.spec.to.tlsClient.tlsSecret
:指定恢复备份使用的存储证书的 Secret。kubectl create secret generic ${secret_name} --namespace=${namespace} --from-file=tls.crt=${cert_path} --from-file=tls.key=${key_path} --from-file=ca.crt=${ca_path}