备份和恢复应用程序
第 1 章 备份应用程序 复制链接链接已复制到粘贴板!
您可以将 OpenShift API 用于 Red Hat OpenShift Service on AWS (ROSA)集群的数据保护(OADP)来备份和恢复应用程序数据。在安装 OADP 前,您必须为 OADP 设置角色和策略凭证,以便可以使用 AWS API。
这是一个包括两个阶段的过程:
- 准备 AWS 凭证。
- 安装 OADP Operator,并将其提供给 IAM 角色。
1.1. 准备 AWS 凭证 复制链接链接已复制到粘贴板!
AWS 帐户必须准备好接受 OADP 安装。
流程
运行以下命令来创建以下环境变量:
注意更改集群名称来匹配您的 ROSA 集群,并确保以管理员身份登录到集群。在继续操作前,确保所有字段被正常输出。
$ export CLUSTER_NAME=my-cluster1 export ROSA_CLUSTER_ID=$(rosa describe cluster -c ${CLUSTER_NAME} --output json | jq -r .id) export REGION=$(rosa describe cluster -c ${CLUSTER_NAME} --output json | jq -r .region.id) export OIDC_ENDPOINT=$(oc get authentication.config.openshift.io cluster -o jsonpath='{.spec.serviceAccountIssuer}' | sed 's|^https://||') export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) export CLUSTER_VERSION=$(rosa describe cluster -c ${CLUSTER_NAME} -o json | jq -r .version.raw_id | cut -f -2 -d '.') export ROLE_NAME="${CLUSTER_NAME}-openshift-oadp-aws-cloud-credentials" export SCRATCH="/tmp/${CLUSTER_NAME}/oadp" mkdir -p ${SCRATCH} echo "Cluster ID: ${ROSA_CLUSTER_ID}, Region: ${REGION}, OIDC Endpoint: ${OIDC_ENDPOINT}, AWS Account ID: ${AWS_ACCOUNT_ID}"- 1
- 将
my-cluster替换为您的 ROSA 集群名称。
在 AWS 帐户上,创建一个 IAM 策略以允许访问 S3。
运行以下命令,检查策略是否存在:
$ POLICY_ARN=$(aws iam list-policies --query "Policies[?PolicyName=='RosaOadpVer1'].{ARN:Arn}" --output text)1 - 1
- 将
RosaOadp替换为您的策略名称。
使用以下命令来创建策略 JSON 文件,然后在 ROSA 中创建策略。
注意如果没有找到策略 ARN,该命令将创建策略。如果策略 ARN 已存在,则
if语句将跳过策略创建。$ if [[ -z "${POLICY_ARN}" ]]; then cat << EOF > ${SCRATCH}/policy.json1 { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:CreateBucket", "s3:DeleteBucket", "s3:PutBucketTagging", "s3:GetBucketTagging", "s3:PutEncryptionConfiguration", "s3:GetEncryptionConfiguration", "s3:PutLifecycleConfiguration", "s3:GetLifecycleConfiguration", "s3:GetBucketLocation", "s3:ListBucket", "s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:ListBucketMultipartUploads", "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts", "ec2:DescribeSnapshots", "ec2:DescribeVolumes", "ec2:DescribeVolumeAttribute", "ec2:DescribeVolumesModifications", "ec2:DescribeVolumeStatus", "ec2:CreateTags", "ec2:CreateVolume", "ec2:CreateSnapshot", "ec2:DeleteSnapshot" ], "Resource": "*" } ]} EOF POLICY_ARN=$(aws iam create-policy --policy-name "RosaOadpVer1" \ --policy-document file:///${SCRATCH}/policy.json --query Policy.Arn \ --tags Key=rosa_openshift_version,Value=${CLUSTER_VERSION} Key=rosa_role_prefix,Value=ManagedOpenShift Key=operator_namespace,Value=openshift-oadp Key=operator_name,Value=openshift-oadp \ --output text) fi- 1
SCRATCH是为环境变量创建的临时目录的名称。
运行以下命令来查看策略 ARN:
$ echo ${POLICY_ARN}
为集群创建 IAM 角色信任策略:
运行以下命令来创建信任策略文件:
$ cat <<EOF > ${SCRATCH}/trust-policy.json { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "Federated": "arn:aws:iam::${AWS_ACCOUNT_ID}:oidc-provider/${OIDC_ENDPOINT}" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "${OIDC_ENDPOINT}:sub": [ "system:serviceaccount:openshift-adp:openshift-adp-controller-manager", "system:serviceaccount:openshift-adp:velero"] } } }] } EOF运行以下命令来创建角色:
$ ROLE_ARN=$(aws iam create-role --role-name \ "${ROLE_NAME}" \ --assume-role-policy-document file://${SCRATCH}/trust-policy.json \ --tags Key=rosa_cluster_id,Value=${ROSA_CLUSTER_ID} Key=rosa_openshift_version,Value=${CLUSTER_VERSION} Key=rosa_role_prefix,Value=ManagedOpenShift Key=operator_namespace,Value=openshift-adp Key=operator_name,Value=openshift-oadp \ --query Role.Arn --output text)运行以下命令来查看角色 ARN:
$ echo ${ROLE_ARN}
运行以下命令,将 IAM 策略附加到 IAM 角色:
$ aws iam attach-role-policy --role-name "${ROLE_NAME}" \ --policy-arn ${POLICY_ARN}
后续步骤
- 继续安装 OADP Operator 并提供 IAM 角色。
1.2. 安装 OADP Operator 并提供 IAM 角色 复制链接链接已复制到粘贴板!
AWS 安全令牌服务 (AWS STS) 是一个全局 Web 服务,它为 IAM 或联邦用户提供简短凭证。使用 STS 的 Red Hat OpenShift Service on AWS (ROSA) 是 ROSA 集群的建议凭证模式。本文档论述了如何使用 AWS STS 在 ROSA 上安装 OpenShift API for Data Protection (OADP)。
Restic 不支持。
在备份没有 Container Storage Interface (CSI)快照支持的文件系统时,支持 Kopia 文件系统备份(FSB)。
文件系统示例包括:
- Amazon Elastic File System (EFS)
- 网络文件系统 (NFS)
-
emptyDir卷 - 本地卷
对于备份卷,使用 AWS STS 的 ROSA 上 OADP 仅支持原生快照和 Container Storage Interface (CSI) 快照。
在使用 STS 验证的 Amazon ROSA 集群中,不支持在不同的 AWS 区域中恢复备份数据。
目前,ROSA 集群不支持 Data Mover 功能。您可以使用原生 AWS S3 工具移动数据。
先决条件
-
具有所需访问和令牌的 Red Hat OpenShift Service on AWS ROSA 集群。具体步骤请查看为 OADP 准备 AWS 凭证。如果您计划使用两个不同的集群来备份和恢复,您必须为每个集群准备 AWS 凭证,包括
ROLE_ARN。
流程
输入以下命令,从 AWS 令牌文件创建 Red Hat OpenShift Service on AWS secret:
创建凭证文件:
$ cat <<EOF > ${SCRATCH}/credentials [default] role_arn = ${ROLE_ARN} web_identity_token_file = /var/run/secrets/openshift/serviceaccount/token EOF为 OADP 创建命名空间:
$ oc create namespace openshift-adp创建 Red Hat OpenShift Service on AWS secret:
$ oc -n openshift-adp create secret generic cloud-credentials \ --from-file=${SCRATCH}/credentials注意在 Red Hat OpenShift Service on AWS 版本 4.15 及更新的版本中,OADP Operator 通过 Operator Lifecycle Manager (OLM)和 Cloud Credentials Operator (CCO)支持新的标准化 STS 工作流。在此工作流中,您不需要创建上述 secret,您只需要在使用 Red Hat OpenShift Service on AWS Web 控制台安装 OLM 管理的 Operator 的过程中提供角色 ARN,请参阅使用 Web 控制台从 OperatorHub 安装。
前面的 secret 由 CCO 自动创建。
安装 OADP Operator:
- 在 Red Hat OpenShift Service on AWS Web 控制台中浏览 Operators → OperatorHub。
- 搜索 OADP Operator。
- 在 role_ARN 字段中,粘贴之前创建的 role_arn,再点 Install。
输入以下命令,使用 AWS 凭证创建 AWS 云存储:
$ cat << EOF | oc create -f - apiVersion: oadp.openshift.io/v1alpha1 kind: CloudStorage metadata: name: ${CLUSTER_NAME}-oadp namespace: openshift-adp spec: creationSecret: key: credentials name: cloud-credentials enableSharedConfig: true name: ${CLUSTER_NAME}-oadp provider: aws region: $REGION EOF输入以下命令检查应用程序的存储默认存储类:
$ oc get pvc -n <namespace>输出示例
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE applog Bound pvc-351791ae-b6ab-4e8b-88a4-30f73caf5ef8 1Gi RWO gp3-csi 4d19h mysql Bound pvc-16b8e009-a20a-4379-accc-bc81fedd0621 1Gi RWO gp3-csi 4d19h运行以下命令来获取存储类:
$ oc get storageclass输出示例
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE gp2 kubernetes.io/aws-ebs Delete WaitForFirstConsumer true 4d21h gp2-csi ebs.csi.aws.com Delete WaitForFirstConsumer true 4d21h gp3 ebs.csi.aws.com Delete WaitForFirstConsumer true 4d21h gp3-csi (default) ebs.csi.aws.com Delete WaitForFirstConsumer true 4d21h注意以下存储类可以正常工作:
- gp3-csi
- gp2-csi
- gp3
- gp2
如果要备份的应用程序或应用程序都使用带有 Container Storage Interface (CSI) 的持久性卷 (PV),建议在 OADP DPA 配置中包含 CSI 插件。
创建
DataProtectionApplication资源,以配置存储备份和卷快照的存储的连接:如果您只使用 CSI 卷,请输入以下命令部署数据保护应用程序:
$ cat << EOF | oc create -f - apiVersion: oadp.openshift.io/v1alpha1 kind: DataProtectionApplication metadata: name: ${CLUSTER_NAME}-dpa namespace: openshift-adp spec: backupImages: true1 features: dataMover: enable: false backupLocations: - bucket: cloudStorageRef: name: ${CLUSTER_NAME}-oadp credential: key: credentials name: cloud-credentials prefix: velero default: true config: region: ${REGION} configuration: velero: defaultPlugins: - openshift - aws - csi restic: enable: false EOF- 1
- ROSA 支持内部镜像备份。如果您不想使用镜像备份,请将此字段设置为
false。
如果使用 CSI 或非 CSI 卷,请输入以下命令来部署数据保护应用程序:
$ cat << EOF | oc create -f - apiVersion: oadp.openshift.io/v1alpha1 kind: DataProtectionApplication metadata: name: ${CLUSTER_NAME}-dpa namespace: openshift-adp spec: backupImages: true1 features: dataMover: enable: false backupLocations: - bucket: cloudStorageRef: name: ${CLUSTER_NAME}-oadp credential: key: credentials name: cloud-credentials prefix: velero default: true config: region: ${REGION} configuration: velero: defaultPlugins: - openshift - aws nodeAgent:2 enable: false uploaderType: restic snapshotLocations: - velero: config: credentialsFile: /tmp/credentials/openshift-adp/cloud-credentials-credentials3 enableSharedConfig: "true"4 profile: default5 region: ${REGION}6 provider: aws EOF现在,您可以备份和恢复 Red Hat OpenShift Service on AWS 应用程序,如 备份应用程序 中所述。
此配置中的 restic 的 enable 参数设置为 false,因为 OADP 不支持 ROSA 环境中的 Restic。
如果使用 OADP 1.2,请替换此配置:
nodeAgent:
enable: false
uploaderType: restic
使用以下配置:
restic:
enable: false
如果要使用两个不同的集群来备份和恢复,则两个集群必须在云存储 CR 和 OADP DataProtectionApplication 配置中具有相同的 AWS S3 存储名称。
1.3. 使用 ROSA STS 在 OADP 上备份工作负载 复制链接链接已复制到粘贴板!
1.3.1. 使用 OADP 和 ROSA STS 执行备份 复制链接链接已复制到粘贴板!
以下示例 hello-world 应用没有附加持久性卷 (PV)。使用 OpenShift API 对 Red Hat OpenShift Service on AWS (ROSA) STS 进行数据保护 (OADP) 进行备份。
数据保护应用程序 (DPA) 配置都将正常工作。
运行以下命令,创建一个工作负载来备份:
$ oc create namespace hello-world$ oc new-app -n hello-world --image=docker.io/openshift/hello-openshift运行以下命令来公开路由:
$ oc expose service/hello-openshift -n hello-world运行以下命令检查应用程序是否正常工作:
$ curl `oc get route/hello-openshift -n hello-world -o jsonpath='{.spec.host}'`输出示例
Hello OpenShift!运行以下命令来备份工作负载:
$ cat << EOF | oc create -f - apiVersion: velero.io/v1 kind: Backup metadata: name: hello-world namespace: openshift-adp spec: includedNamespaces: - hello-world storageLocation: ${CLUSTER_NAME}-dpa-1 ttl: 720h0m0s EOF等待备份完成,然后运行以下命令:
$ watch "oc -n openshift-adp get backup hello-world -o json | jq .status"输出示例
{ "completionTimestamp": "2022-09-07T22:20:44Z", "expiration": "2022-10-07T22:20:22Z", "formatVersion": "1.1.0", "phase": "Completed", "progress": { "itemsBackedUp": 58, "totalItems": 58 }, "startTimestamp": "2022-09-07T22:20:22Z", "version": 1 }运行以下命令来删除 demo 工作负载:
$ oc delete ns hello-world运行以下命令,从备份中恢复工作负载:
$ cat << EOF | oc create -f - apiVersion: velero.io/v1 kind: Restore metadata: name: hello-world namespace: openshift-adp spec: backupName: hello-world EOF运行以下命令等待 Restore 完成:
$ watch "oc -n openshift-adp get restore hello-world -o json | jq .status"输出示例
{ "completionTimestamp": "2022-09-07T22:25:47Z", "phase": "Completed", "progress": { "itemsRestored": 38, "totalItems": 38 }, "startTimestamp": "2022-09-07T22:25:28Z", "warnings": 9 }运行以下命令检查工作负载是否已恢复:
$ oc -n hello-world get pods输出示例
NAME READY STATUS RESTARTS AGE hello-openshift-9f885f7c6-kdjpj 1/1 Running 0 90s运行以下命令来检查 JSONPath:
$ curl `oc get route/hello-openshift -n hello-world -o jsonpath='{.spec.host}'`输出示例
Hello OpenShift!
有关故障排除提示的信息,请参阅 OADP 团队的 故障排除文档。
1.3.2. 使用 OADP 和 ROSA STS 的备份后清理集群 复制链接链接已复制到粘贴板!
如果您需要卸载 OpenShift API for Data Protection (OADP) Operator 以及本例中的备份和 S3 存储桶,请按照以下步骤操作。
流程
运行以下命令来删除工作负载:
$ oc delete ns hello-world运行以下命令来删除数据保护应用程序 (DPA):
$ oc -n openshift-adp delete dpa ${CLUSTER_NAME}-dpa运行以下命令来删除云存储:
$ oc -n openshift-adp delete cloudstorage ${CLUSTER_NAME}-oadp警告如果这个命令挂起,您可能需要通过运行以下命令来删除终结器:
$ oc -n openshift-adp patch cloudstorage ${CLUSTER_NAME}-oadp -p '{"metadata":{"finalizers":null}}' --type=merge如果不再需要 Operator,请运行以下命令删除它:
$ oc -n openshift-adp delete subscription oadp-operator从 Operator 中删除命名空间:
$ oc delete ns openshift-adp如果不再需要备份和恢复资源,请运行以下命令从集群中删除它们:
$ oc delete backups.velero.io hello-world要删除 AWS S3 中的备份、恢复和远程对象,请运行以下命令:
$ velero backup delete hello-world如果您不再需要自定义资源定义 (CRD),请运行以下命令从集群中删除它们:
$ for CRD in `oc get crds | grep velero | awk '{print $1}'`; do oc delete crd $CRD; done运行以下命令来删除 AWS S3 存储桶:
$ aws s3 rm s3://${CLUSTER_NAME}-oadp --recursive$ aws s3api delete-bucket --bucket ${CLUSTER_NAME}-oadp运行以下命令,将策略从角色分离:
$ aws iam detach-role-policy --role-name "${ROLE_NAME}" --policy-arn "${POLICY_ARN}"运行以下命令来删除角色:
$ aws iam delete-role --role-name "${ROLE_NAME}"
1.4. 已知问题 复制链接链接已复制到粘贴板!
- 不支持或推荐 Restic、Kopia 和 DataMover。
- CloudStorage: openshift-adp-controller-manager crashloop seg fault with Restic enabled。
- (仅影响 OADP 1.1.x_):Cloud Storage: bucket 在 CS CR 删除时被删除,尽管它没有 "oadp.openshift.io/cloudstorage-delete": "true"。
Legal Notice
复制链接链接已复制到粘贴板!
Copyright © Red Hat
OpenShift documentation is licensed under the Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0).
Modified versions must remove all Red Hat trademarks.
Portions adapted from https://github.com/kubernetes-incubator/service-catalog/ with modifications by Red Hat.
Red Hat, Red Hat Enterprise Linux, the Red Hat logo, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of the OpenJS Foundation.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation’s permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.