文档

使用Terraform备份和恢复应用

更新时间:

您可以通过Terraform命令行在备份集群和恢复集群中部署备份仓库、应用备份、应用恢复的CR资源,以实现跨集群间应用的迁移。

前提条件

注意事项

  • 请勿通过kubectl的delete命令删除备份及恢复任务,避免相关云资源删除不彻底。备份相关的删除操作,请参见下文步骤五:删除集群备份中心的相关资源

  • 关注备份中心组件migrate-controller版本发布情况,并及时升级。具体操作,请参见管理组件

  • 请勿删除下文示例代码中出现的配置参数,以免备份恢复失败。

步骤一:关联集群

您可以通过Kubernetes Provider关联集群,关于Provider更多信息,请参见Kubernetes Provider

本示例以使用KubeConfig文件为例介绍如何关联集群。

  1. 创建Terraform工作目录。

  2. 在Terraform工作目录中创建名为csdr.tf配置文件,并在该配置文件中加入以下内容,用于关联KubeConfig文件。

    provider "kubernetes" {
      config_path = "~/.kube/config"
    }
  3. 执行以下命令,初始化Terraform运行环境。

    terraform init

    返回信息如下,表明Terraform初始化成功。

    展开查看返回信息

    Initializing the backend...
    
    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Reusing previous version of hashicorp/kubernetes from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.223.1
    - Using previously-installed hashicorp/kubernetes v2.30.0
    
    ╷
    │ Warning: Additional provider information from registry
    │
    │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
    │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please
    │ update your source in required_providers.
    ╵
    
    Terraform has been successfully initialized!
    

步骤二:创建备份仓库

  1. 在csdr.tf配置文件中加入以下内容,并根据实际需求修改。

    resource "kubernetes_manifest" "backuplocation-demo" {
      manifest = {
        apiVersion  = "csdr.alibabacloud.com/v1beta1"
        kind        = "BackupLocation"
        metadata = {
          name = "<yourBackuplocationName>"
          namespace = "csdr"
        }
      
        spec = {
          backupSyncPeriod = "0s"
          config = {
            network = "internal"
            region  = "cn-beijing"
          }
          objectStorage = {
            bucket 	= "<cnfs-oss-yourBucketName>"
            prefix 	= "<subDir>"
          }
          provider 	= "alibabacloud"  
        }
      }
      wait {
        fields = {
          "status.phase" = "Available"
        }
      }
    
      timeouts {
        create = "10m"
      }
    }

    参数

    是否必选

    说明

    name

    备份仓库的名称,须符合K8s命名规则。

    network

    OSS Bucket的网络访问方式。取值:

    • internal:内网访问模式,所有关联集群与OSS Bucket需处于同一地域。

    • public:公网访问模式,无地域限制。

    region

    OSS Bucket所在地域。

    bucket

    OSS Bucket名称,需提前创建,且名称需符合以cnfs-oss-****开头的命名规则。

    prefix

    OSS Bucket子目录。设定该参数后,实际备份内容存储在此子目录下。

  2. 执行以下命令,生成资源规划。

    terraform plan

    返回信息如下,表明资源规划生成成功。

    展开查看返回信息

      # kubernetes_manifest.backuplocation-demo will be created
      + resource "kubernetes_manifest" "backuplocation-demo" {
          + manifest = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "BackupLocation"
              + metadata   = {
                  + name      = "<yourBackuplocationName>"
                  + namespace = "csdr"
                }
              + spec       = {
                  + backupSyncPeriod = "0s"
                  + config           = {
                      + network = "internal"
                      + region  = "cn-beijing"
                    }
                  + objectStorage    = {
                      + bucket = "<cnfs-oss-yourBucketName>"
                      + prefix = "<subDir>"
                    }
                  + provider         = "alibabacloud"
                }
            }
          + object   = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "BackupLocation"
              + metadata   = {
                  + annotations                = (known after apply)
                  + creationTimestamp          = (known after apply)
                  + deletionGracePeriodSeconds = (known after apply)
                  + deletionTimestamp          = (known after apply)
                  + finalizers                 = (known after apply)
                  + generateName               = (known after apply)
                  + generation                 = (known after apply)
                  + labels                     = (known after apply)
                  + managedFields              = (known after apply)
                  + name                       = "<yourBackuplocationName>"
                  + namespace                  = "csdr"
                  + ownerReferences            = (known after apply)
                  + resourceVersion            = (known after apply)
                  + selfLink                   = (known after apply)
                  + uid                        = (known after apply)
                }
              + spec       = {
                  + accessMode          = (known after apply)
                  + backupSyncPeriod    = "0s"
                  + config              = {
                      + network = "internal"
                      + region  = "cn-beijing"
                    }
                  + objectStorage       = {
                      + bucket = "<cnfs-oss-yourBucketName>"
                      + caCert = (known after apply)
                      + prefix = "<subDir>"
                    }
                  + provider            = "alibabacloud"
                  + validationFrequency = (known after apply)
                }
            }
    
          + timeouts {
              + create = "10m"
            }
    
          + wait {
              + fields = {
                  + "status.phase" = "Available"
                }
            }
        }
  3. 执行以下命令,创建备份仓库。

    terraform apply

    返回信息如下,表明备份仓库创建成功。

    kubernetes_manifest.backuplocation-demo: Creating...
    kubernetes_manifest.backuplocation-demo: Creation complete after 1s
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

步骤三:创建备份任务

创建立即备份任务

  1. 在csdr.tf配置文件中加入以下内容,并根据实际需求修改。

    resource "kubernetes_manifest" "applicationbackup-demo" {
      manifest = {
        apiVersion  	= "csdr.alibabacloud.com/v1beta1"
        kind        	= "ApplicationBackup"
        metadata = {
          name		= "<yourApplicationBackupName>"
          namespace 	= "csdr"
          annotations = {
            "csdr.alibabacloud.com/backuplocations" = "{\"name\":\"<yourBackuplocationName>\",\"region\":\"cn-beijing\",\"bucket\":\"<cnfs-oss-yourBucketName>\",\"prefix\":\"<subDir>\",\"provider\":\"alibabacloud\"}"
        }
        spec = {
          includedNamespaces 	= ["default","default1"]
          includedResources 	= ["statefulset"]
          excludedResources 	= ["excludedResources"]
          labelSelector 		= {
            matchLabels 		= {
              "app" = "mysql-sts"
            }
          }
          pvBackup = {
            defaultPvBackup 	= "false"
          } 
          storageLocation 		= "<yourBackuplocationName>"
          ttl									= "720h0m0s"
          includeClusterResources = "false"
        }
      }
      
      #备份成功完成的标志为status.phase转化为Completed阶段
      #备份时长与集群应用数量、存储卷数据量有关
      #wait {
      #  fields = {
      #    "status.phase" = "Completed"
      #  }
      #}
    
      #timeouts {
      #  create = "60m"
      #}
     }
    }

    参数

    是否必选

    说明

    csdr.alibabacloud.com/backuplocations

    备份所在的备份仓库信息,需与备份仓库的配置一致。

    name

    立即备份任务的名称。

    includedNamespaces

    指定命名空间名称。

    includedResources

    指定集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。当两者均为空时,表示备份所有资源类型。

    excludedResources

    排除集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。当两者均为空时,表示备份所有资源类型。

    matchLabels

    指定标签,只备份符合的资源。

    includeClusterResources

    是否备份Cluster级别的资源,例如StorageClass、CRD、Webhook等。

    • true:备份所有Cluster级别资源。

    • false:仅备份被选择的命名空间中的Namespace级别资源引用的Cluster级别资源。例如,备份Pod时,引用的ServiceAccount授权了某ClusterRole,该ClusterRole将被自动备份;备份CR时,CRD将被自动备份。

    说明

    通过容器服务管理控制台创建的备份任务,默认IncludeClusterResources字段为false

    defaultPvBackup

    是否备份存储卷,开启数据备份。取值:

    • true:备份应用和存储卷。

    • false:只备份应用。

    storageLocation

    备份仓库名称。

    说明

    若您的集群已使用Velero,请加入钉钉用户群(钉钉群号:35532895)咨询。

    ttl

    备份有效期,即备份任务的数据存储有效期,过期之后数据将无法恢复。格式如720h0m0s,取值范围:24h0m0s~1572864h0m0s。

  2. 执行以下命令,生成资源规划。

    terraform plan

    返回信息如下,表明资源规划生成成功。

    展开查看返回信息

      # kubernetes_manifest.applicationbackup-demo will be created
      + resource "kubernetes_manifest" "applicationbackup-demo" {
          + manifest = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "ApplicationBackup"
              + metadata   = {
                  + annotations = {
                      + "csdr.alibabacloud.com/backuplocations" = "{name:<yourBackuplocationName>,region:cn-beijing,bucket:<cnfs-oss-yourBucketName>,prefix:<subDir>,provider:alibabacloud}"
                    }
                  + name        = "<yourApplicationBackupName>"
                  + namespace   = "csdr"
                }
              + spec       = {
                  + excludedResources  = [
                      + "excludedResources",
                    ]
                  + includedNamespaces = [
                      + "default",
                      + "default1",
                    ]
                  + includedResources  = [
                      + "statefulset",
                    ]
                  + labelSelector      = {
                      + matchLabels = {
                          + app = "mysql-sts"
                        }
                    }
                  + pvBackup           = {
                      + defaultPvBackup = "false"
                    }
                  + storageLocation    = "vault-a"
                  + ttl                = "720h0m0s"
                }
            }
          + object   = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "ApplicationBackup"
              + metadata   = {
                  + annotations                = (known after apply)
                  + creationTimestamp          = (known after apply)
                  + deletionGracePeriodSeconds = (known after apply)
                  + deletionTimestamp          = (known after apply)
                  + finalizers                 = (known after apply)
                  + generateName               = (known after apply)
                  + generation                 = (known after apply)
                  + labels                     = (known after apply)
                  + managedFields              = (known after apply)
                  + name                       = "<yourApplicationBackupName>"
                  + namespace                  = "csdr"
                  + ownerReferences            = (known after apply)
                  + resourceVersion            = (known after apply)
                  + selfLink                   = (known after apply)
                  + uid                        = (known after apply)
                }
              + spec       = {
                  + backupType              = (known after apply)
                  + excludedNamespaces      = (known after apply)
                  + excludedResources       = [
                      + "excludedResources",
                    ]
                  + includeClusterResources = (known after apply)
                  + includedNamespaces      = [
                      + "default",
                      + "default1",
                    ]
                  + includedResources       = [
                      + "statefulset",
                    ]
                  + labelSelector           = {
                      + matchExpressions = (known after apply)
                      + matchLabels      = {
                          + app = "mysql-sts"
                        }
                    }
                  + orderedResources        = (known after apply)
                  + pvBackup                = {
                      + defaultPvBackup  = false
                      + pvcList          = (known after apply)
                      + pvcSelector      = {
                          + matchExpressions = (known after apply)
                          + matchLabels      = (known after apply)
                        }
                      + snapshotPostRule = (known after apply)
                      + snapshotPreRule  = (known after apply)
                    }
                  + storageLocation     = "<yourBackuplocationName>"
                  + ttl                 = "720h0m0s"
                }
            }
        }
    
  3. 执行以下命令,创建立即备份任务。

    terraform apply

    返回信息如下,表明立即备份任务创建成功。

    kubernetes_manifest.applicationbackup-demo: Creating...
    kubernetes_manifest.applicationbackup-demo: Creation complete after 1s
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

创建定时备份计划

  1. 在csdr.tf配置文件中加入以下内容,并根据实际需求修改。

    展开查看配置文件

    resource "kubernetes_manifest" "backupschedule-demo" {
      manifest = {
        apiVersion  	= "csdr.alibabacloud.com/v1beta1"
        kind        	= "BackupSchedule"
        metadata = {
          name		= "<yourBackupScheduleName>"
          namespace 	= "csdr"
          annotations = {
            "csdr.alibabacloud.com/backuplocations" = "{\"name\":\"<yourBackuplocationName>\",\"region\":\"cn-beijing\",\"bucket\":\"<cnfs-oss-yourBucketName>\",\"prefix\":\"<subDir>\",\"provider\":\"alibabacloud\"}"
          }
        }
        spec = {
          schedule = "1 4 * * *"
          template = {
            includedNamespaces 			= ["default","default1"]
            includedResources 			= ["statefulset"]
            excludedResources 			= ["excludedResources"]
            labelSelector 				= {
              matchLabels 				= {
                "app" = "mysql-sts"
              }
            }
            pvBackup = {
              defaultPvBackup 			= "false"
            } 
            storageLocation 			= "<yourBackuplocationName>"
            ttl											= "720h0m0s"
            includeClusterResources = "false"
          }
        }
      }
      wait {
        fields = {
          "status.phase" = "Enabled"
        }
      }
      timeouts {
        create = "10m"
      }
    }

    参数

    是否必选

    说明

    csdr.alibabacloud.com/backuplocations

    备份所在的备份仓库信息,需与备份仓库的配置一致。

    name

    定时备份计划的名称。

    schedule

    备份周期,请使用Cron表达式。

    includedNamespaces

    指定命名空间名称。

    includedResources

    指定集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。当两者均为空时,表示备份所有资源类型。

    excludedResources

    排除集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。当两者均为空时,表示备份所有资源类型。

    matchLabels

    指定标签,只备份符合条件的资源。

    includeClusterResources

    是否备份Cluster级别的资源,例如StorageClass、CRD、Webhook等。

    • true:备份所有Cluster级别资源。

    • false:仅备份被选择的命名空间中的Namespace级别资源引用的Cluster级别资源。例如,备份Pod时,引用的ServiceAccount授权了某ClusterRole,该ClusterRole将被自动备份;备份CR时,CRD将被自动备份。

    说明

    通过容器服务管理控制台创建的备份任务,默认IncludeClusterResources字段为false

    defaultPvBackup

    是否备份存储卷,开启数据备份。取值:

    • true:备份应用和存储卷。

    • false:只备份应用。

    storageLocation

    备份仓库名称。

    说明

    若您的集群已使用Velero,请加入钉钉用户群(钉钉群号:35532895)咨询。

    ttl

    备份有效期,即备份任务的数据存储有效期,过期之后数据将无法恢复。格式如720h0m0s,取值范围:24h0m0s~1572864h0m0s。

  2. 执行以下命令,生成资源规划。

    terraform plan

    返回信息如下,表明资源规划生成成功。

    展开查看返回信息

      # kubernetes_manifest.backupschedule-demo will be created
      + resource "kubernetes_manifest" "backupschedule-demo" {
          + manifest = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "BackupSchedule"
              + metadata   = {
                  + annotations = {
                      + "csdr.alibabacloud.com/backuplocations" = "{name:<yourBackuplocationName>,region:cn-beijing,bucket:<cnfs-oss-yourBucketName>,prefix:<subDir>,provider:alibabacloud}"
                    }
                  + name        = "<yourBackupscheduleName>"
                  + namespace   = "csdr"
                }
              + spec       = {
                  + schedule = "1 4 * * *"
                  + template = {
                      + excludedResources  = [
                          + "excludedResources",
                        ]
                      + includedNamespaces = [
                          + "default",
                          + "default1",
                        ]
                      + includedResources  = [
                          + "statefulset",
                        ]
                      + labelSelector      = {
                          + matchLabels = {
                              + app = "mysql-sts"
                            }
                        }
                      + pvBackup           = {
                          + defaultPvBackup = "false"
                        }
                      + storageLocation    = "<yourBackuplocationName>"
                      + ttl                = "720h0m0s"
                    }
                }
            }
          + object   = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "BackupSchedule"
              + metadata   = {
                  + annotations                = (known after apply)
                  + creationTimestamp          = (known after apply)
                  + deletionGracePeriodSeconds = (known after apply)
                  + deletionTimestamp          = (known after apply)
                  + finalizers                 = (known after apply)
                  + generateName               = (known after apply)
                  + generation                 = (known after apply)
                  + labels                     = (known after apply)
                  + managedFields              = (known after apply)
                  + name                       = "<yourBackupscheduleName>"
                  + namespace                  = "csdr"
                  + ownerReferences            = (known after apply)
                  + resourceVersion            = (known after apply)
                  + selfLink                   = (known after apply)
                  + uid                        = (known after apply)
                }
              + spec       = {
                  + backupAll = (known after apply)
                  + paused    = (known after apply)
                  + schedule  = "1 4 * * *"
                  + template  = {
                      + backupType              = (known after apply)
                      + excludedNamespaces      = (known after apply)
                      + excludedResources       = [
                          + "excludedResources",
                        ]
                      + includeClusterResources = (known after apply)
                      + includedNamespaces      = [
                          + "default",
                          + "default1",
                        ]
                      + includedResources       = [
                          + "statefulset",
                        ]
                      + labelSelector           = {
                          + matchExpressions = (known after apply)
                          + matchLabels      = {
                              + app = "mysql-sts"
                            }
                        }
                      + orderedResources        = (known after apply)
                      + pvBackup                = {
                          + defaultPvBackup  = false
                          + pvcList          = (known after apply)
                          + pvcSelector      = {
                              + matchExpressions = (known after apply)
                              + matchLabels      = (known after apply)
                            }
                          + snapshotPostRule = (known after apply)
                          + snapshotPreRule  = (known after apply)
                        }
                      + storageLocation         = "<yourBackuplocationName>"
                      + ttl                     = "720h0m0s"
                    }
                }
            }
          + timeouts {
              + create = "10m"
            }
    
          + wait {
              + fields = {
                  + "status.phase" = "Enabled"
                }
            }
        }
  3. 执行以下命令,创建定时备份计划。

    terraform apply

    返回信息如下,表明定时备份计划创建成功。

    kubernetes_manifest.backupschedule-demo: Creating...
    kubernetes_manifest.backupschedule-demo: Creation complete after 1s
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

相关操作

  • 通过定时备份计划,查找备份。

    通过labelSelector可以查找由备份计划创建的备份。

    data "kubernetes_resources" "list-applicationbackup" {
      api_version 			= "csdr.alibabacloud.com/v1beta1"
      kind       				= "ApplicationBackup"
      namespace         = "csdr"
      label_selector    = "csdr/schedule-name=terraform-schedule-test"
    }
    
    # 通过Output进行输出。
    output "applicationbackup-name" {
      value = data.kubernetes_resources.list-applicationbackup.objects
    }
  • 修改定时备份计划。

    1. spec.paused设置为true,以暂停定时备份计划,然后根据需求修改spec中用于设置备份计划中定义备份行为的其他字段(spec.schedule)等,示例如下。

      resource "kubernetes_manifest" "backupschedule-demo" {
        manifest = {
          apiVersion          = "csdr.alibabacloud.com/v1beta1"
          kind                = "BackupSchedule"
          metadata = {
            name                              = "<yourBackcupscheduleName>"
            namespace         = "csdr"
            annotations = {
              "csdr.alibabacloud.com/backuplocations" = "{\"name\":\"<yourBackuplocationName>\",\"region\":\"cn-beijing\",\"bucket\":\"<cnfs-oss-yourBucketName>\",\"prefix\":\"<subDir>\",\"provider\":\"alibabacloud\"}"
            }
          }
          spec = {
            # 暂停备份计划。
            paused = "true" 
            # 修改备份计划的备份周期。
            schedule = "0 5 * * *"
            template = {
              includedNamespaces      = ["default","default1"]
              includedResources       = ["statefulset"]
              excludedResources       = ["excludedResources"]
              labelSelector                   = {
                matchLabels                   = {
                  "app" = "mysql-sts"
                }
              }
              pvBackup = {
                defaultPvBackup       = "false"
              }
              storageLocation                 = "vault-a"
              ttl                                                                     = "720h0m0s"
              includeClusterResources = "false"
            }
          }
        }
        wait {
          fields = {
            # 暂停备份计划后,预期的状态应为Paused。
            "status.phase" = "Paused"
          }
        }
        timeouts {
          create = "10m"
        }
      }
    2. 执行以下命令,生成资源规划。

      terraform plan

      返回如下信息,表明资源规划生成成功。

      kubernetes_manifest.backupschedule-demo: Refreshing state...
      
      Terraform used the selected providers to generate the following execution plan. Resource actions
      are indicated with the following symbols:
        ~ update in-place
      
      Terraform will perform the following actions:
      
        # kubernetes_manifest.backupschedule-demo will be updated in-place
        ~ resource "kubernetes_manifest" "backupschedule-demo" {
            ~ manifest = {
                ~ spec       = {
                    ~ paused   = "false" -> "true"
                    ~ schedule = "1 4 * * *" -> "0 5 * * *"
                      # (1 unchanged attribute hidden)
                  }
                  # (3 unchanged attributes hidden)
              }
            ~ object   = {
                ~ spec       = {
                    ~ paused    = false -> true
                    ~ schedule  = "1 4 * * *" -> "0 5 * * *"
                      # (2 unchanged attributes hidden)
                  }
                  # (3 unchanged attributes hidden)
              }
      
            ~ wait {
                ~ fields = {
                    ~ "status.phase" = "Enabled" -> "Paused"
                  }
              }
      
              # (1 unchanged block hidden)
          }
      
      Plan: 0 to add, 1 to change, 0 to destroy.
    3. 执行以下命令,使定时备份计划修改生效。

      terraform apply

      返回信息如下,表明定时备份计划修改成功。

      kubernetes_manifest.backupschedule-demo: Modifying...
      kubernetes_manifest.backupschedule-demo: Modifications complete after 1s
      
      Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

步骤四:创建恢复任务

  1. 在csdr.tf配置文件中加入以下内容,并根据实际需求修改。

    resource "kubernetes_manifest" "applicationrestore-demo" {
      manifest = {
        apiVersion  = "csdr.alibabacloud.com/v1beta1"
        kind        = "ApplicationRestore"
        metadata = {
          name        = "<yourApplicationRestoreName>"
          namespace   = "csdr"
          annotations = {
            "csdr.alibabacloud.com/backuplocations" = "{\"name\":\"<yourBackuplocationName>\",\"region\":\"cn-beijing\",\"bucket\":\"<cnfs-oss-yourBucketName>\",\"prefix\":\"<subDir>\",\"provider\":\"alibabacloud\"}"
          }
        }
        spec = {
          appRestoreOnly 		= "false"
          preserveNodePorts		= "true"
          includedNamespaces 	= ["default","default1"]
          includedResources 	= ["statefulset"]
          excludedResources 	= ["excludedResources"]
          convertedarg = [
          {
            convertToStorageClassType	= "alicloud-disk-topology-alltype"
            namespace									= "default"
            persistentVolumeClaim		= "pvc-nas"
          },
          {
            convertToStorageClassType	= "alicloud-disk-topology-alltype"
            namespace									= "default1"
            persistentVolumeClaim		= "pvc-oss"
          }
          ]
          backupName			= "<yourApplicationBackupName>"
          namespaceMapping			= {
            "<backupNamespace>" = "<restoreNamespace>"
          }
        }
      }
      
      # 恢复成功完成的标志为status.phase转化为Completed阶段。
      # 恢复时长与集群应用数量、存储卷数据量有关。
      #wait {
      #  fields = {
      #    "status.phase" = "Completed"
      #  }
      #}
    
      #timeouts {
      #  create = "60m"
      #}
    }

    参数

    是否必选

    说明

    csdr.alibabacloud.com/backuplocations

    备份所在的备份仓库信息,需与备份仓库的配置一致。

    name

    恢复任务的名称。

    appRestoreOnly

    对于备份了存储卷的备份,是否仅恢复应用,不恢复存储声明、存储卷及其对应的数据,取值:

    • true:仅恢复应用。如果您需要更改备份应用的数据源,可以先手动创建存储声明、存储卷,然后再创建恢复任务并将该参数设置为true。

    • false:恢复应用以及相关的存储数据,默认为false。

    preserveNodePorts

    是否保持应用的NodePort端口。当备份集群与恢复集群相同时,默认为false,将随机修改端口值,避免与已有的同类应用冲突。否则设置为true。

    includedNamespaces

    指定恢复的命名空间名称。设置为空时,表示恢复所有已备份的命名空间。

    includedResources

    指定待恢复的集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。均为空时,表示恢复所有已备份的资源类型。

    excludedResources

    排除待恢复的集群资源类型。

    重要

    为避免非预期备份结果,includedResourcesexcludedResources您只需配置一项。均为空时,表示恢复所有已备份的资源类型。

    backupName

    待恢复的备份名称。使用定期备份功能时,需要指定为某个时间点创建的备份名称,例如<yourBackupScheduleName>-20221205225845

    namespaceMapping

    重定义命名空间。取值:

    • <backupNamespace>:创建备份任务时指定的备份集群命名空间。

    • <restoreNamespace>:根据备份创建的资源在恢复集群的所在命名空间。若不设置,则默认在同名命名空间中恢复。

    说明

    若<restoreNamespace>不存在,将创建新的命名空间。

    convertedarg

    转换存储类列表。备份中的FileSystem数据类型的存储卷(OSS、NAS、CPFS、本地存储等)的存储声明,在当前集群中恢复为目标存储类,实现例如NAS存储至云盘存储的转换。

    • convertToStorageClassType:目标存储类,需要在当前集群已存在。当前版本仅支持选择云盘或NAS类型的存储类。

    • namespace:存储声明所在的命名空间。

    • persistentVolumeClaim:存储声明名称。

    您可以通过kubectl -ncsdr describe <backup-name>查询备份的存储声明信息,在输出的status.resourceList.dataResource.pvcBackupInfo列表中dataType字段为数据类型,分为FileSystem与Snapshot,nameSpace与pvcName分别为存储声明所在的命名空间与存储声明名称。

    重要

    对于ReadWriteMany类型的存储卷,暂不支持恢复为云盘类型;对于ReadOnlyMany类型的存储卷,恢复为云盘类型时,需由业务保证不会被多个节点上的副本同时挂载,造成强制拔盘。

  2. 执行以下命令,生成资源规划。

    terraform plan

    返回信息如下,表明资源规划生成成功。

    展开查看返回信息

      # kubernetes_manifest.applicationrestore-demo will be created
      + resource "kubernetes_manifest" "applicationrestore-demo" {
          + manifest = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "ApplicationRestore"
              + metadata   = {
                  + annotations = {
                      + "csdr.alibabacloud.com/backuplocations" = "{name:<yourBackuplocationName>,region:cn-beijing,bucket:<cnfs-oss-yourBucketName>,prefix:subpath,provider:alibabacloud}"
                    }
                  + name        = "<yourApplicationRestoreName>"
                  + namespace   = "csdr"
                }
              + spec       = {
                  + appRestoreOnly     = "false"
                  + backupName         = "<yourApplicationbackupName>"
                  + convertedarg       = [
                      + {
                          + convertToStorageClassType = "alicloud-disk-topology-alltype"
                          + namespace                 = "default"
                          + persistentVolumeClaim     = "pvc-nas"
                        },
                      + {
                          + convertToStorageClassType = "alicloud-disk-topology-alltype"
                          + namespace                 = "default1"
                          + persistentVolumeClaim     = "pvc-oss"
                        },
                    ]
                  + excludedResources  = [
                      + "excludedResources",
                    ]
                  + includedNamespaces = [
                      + "default",
                      + "default1",
                    ]
                  + includedResources  = [
                      + "statefulset",
                    ]
                  + namespaceMapping   = {
                      + default = "default1"
                    }
                  + preserveNodePorts  = "true"
                }
            }
          + object   = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "ApplicationRestore"
              + metadata   = {
                  + annotations                = (known after apply)
                  + creationTimestamp          = (known after apply)
                  + deletionGracePeriodSeconds = (known after apply)
                  + deletionTimestamp          = (known after apply)
                  + finalizers                 = (known after apply)
                  + generateName               = (known after apply)
                  + generation                 = (known after apply)
                  + labels                     = (known after apply)
                  + managedFields              = (known after apply)
                  + name                       = "<yourApplicationRestoreName>"
                  + namespace                  = "csdr"
                  + ownerReferences            = (known after apply)
                  + resourceVersion            = (known after apply)
                  + selfLink                   = (known after apply)
                  + uid                        = (known after apply)
                }
              + spec       = {
                  + appRestoreOnly          = false
                  + backupName              = "<yourApplicationbackupName>"
                  + convertedarg            = [
                      + {
                          + convertToStorageClassType = "alicloud-disk-topology-alltype"
                          + namespace                 = "default"
                          + persistentVolumeClaim     = "pvc-nas"
                        },
                      + {
                          + convertToStorageClassType = "alicloud-disk-topology-alltype"
                          + namespace                 = "default1"
                          + persistentVolumeClaim     = "pvc-oss"
                        },
                    ]
                  + excludedNamespaces      = (known after apply)
                  + excludedResources       = [
                      + "excludedResources",
                    ]
                  + existingResourcePolicy  = (known after apply)
                  + includeClusterResources = (known after apply)
                  + includedNamespaces      = [
                      + "default",
                      + "default1",
                    ]
                  + includedResources       = [
                      + "statefulset",
                    ]
                  + labelSelector           = {
                      + matchExpressions = (known after apply)
                      + matchLabels      = (known after apply)
                    }
                  + namespaceMapping        = {
                      + default = "default1"
                    }
                  + preserveNodePorts       = true
                  + pvRestore               = {
                      + pvcPrefix = (known after apply)
                      + pvcSuffix = (known after apply)
                    }
                  + pvRestoreOnly           = (known after apply)
                }
            }
        }
  3. 执行以下命令,创建恢复任务。

    terraform apply

    返回信息如下,表明恢复任务创建完成。

    kubernetes_manifest.applicationbackup-demo: Creating...
    kubernetes_manifest.applicationrestore-demo: Creation complete after 1s
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

步骤五:删除集群备份中心的相关资源

重要

由于您已创建的备份仓库可能正在被其他集群使用,所以备份中心不支持备份仓库BackupLocation资源的删除。

删除定时备份计划

您可以直接通过terraform destroy删除定时备份计划BackupSchedule资源,停止定时备份。

删除备份任务或恢复任务

  1. 在csdr.tf配置文件中加入以下内容,并根据实际需求修改。

    resource "kubernetes_manifest" "deleterequest-demo" {
      manifest = {
        apiVersion  	= "csdr.alibabacloud.com/v1beta1"
        kind        	= "DeleteRequest"
        metadata = {
          name		= "<objectName-dbr>"
          namespace 	= "csdr"
        }
        spec = {
          deleteObjectName 	= "<objectName>"
          deleteObjectType	= "Backup"
        }
      }
    }

    参数

    是否必选

    说明

    name

    删除请求的名称。

    • 删除备份任务时:待删除的备份任务ApplicationBackup的名称+“-dbr”。

    • 删除恢复任务时:待删除的恢复任务ApplicationRestore的名称+“-dbr”。

    deleteObjectName

    待删除的资源名称。

    deleteObjectType

    待删除的资源类型。取值:

    • "Backup":删除备份任务ApplicationBackup及配套资源。

    • "Restore":删除恢复任务ApplicationRestore及配套资源。

    说明
    • 删除备份任务不会影响已同步到集群中的副本。

    • 删除恢复任务不会影响已恢复的内容。

  2. 执行以下命令,生成资源规划。

    terraform plan

    返回信息如下,表明资源规划生成成功。

    展开查看返回信息

      # kubernetes_manifest.deleterequest-demo will be created
      + resource "kubernetes_manifest" "deleterequest-demo" {
          + manifest = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "DeleteRequest"
              + metadata   = {
                  + name      = "<objectName-dbr>"
                  + namespace = "csdr"
                }
              + spec       = {
                  + deleteObjectName = "<objectName>"
                  + deleteObjectType = "Backup"
                }
            }
          + object   = {
              + apiVersion = "csdr.alibabacloud.com/v1beta1"
              + kind       = "DeleteRequest"
              + metadata   = {
                  + annotations                = (known after apply)
                  + creationTimestamp          = (known after apply)
                  + deletionGracePeriodSeconds = (known after apply)
                  + deletionTimestamp          = (known after apply)
                  + finalizers                 = (known after apply)
                  + generateName               = (known after apply)
                  + generation                 = (known after apply)
                  + labels                     = (known after apply)
                  + managedFields              = (known after apply)
                  + name                       = "<objectName-dbr>"
                  + namespace                  = "csdr"
                  + ownerReferences            = (known after apply)
                  + resourceVersion            = (known after apply)
                  + selfLink                   = (known after apply)
                  + uid                        = (known after apply)
                }
              + spec       = {
                  + backupName       = (known after apply)
                  + deleteObjectName = "<objectName>"
                  + deleteObjectType = "Backup"
                }
            }
        }
  3. 执行以下命令,删除资源。

    terraform apply

    返回信息如下,表明deleterequest资源删除请求生效。

    kubernetes_manifest.deleterequest-demo: Creating...
    kubernetes_manifest.deleterequest-demo: Creation complete after 0s
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    说明

    系统根据deleterequest资源删除请求,对应的备份或恢复任务删除后,deleterequest资源也会被删除。

相关文档