全部產品
Search
文件中心

Container Service for Kubernetes:使用Terraform備份和恢複應用

更新時間:Jun 19, 2024

您可以通過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將被自動備份。

    說明

    通過Container Service管理主控台建立的備份任務,預設IncludeClusterResources欄位為false

    defaultPvBackup

    是否備份儲存卷,開啟資料備份。取值:

    • true:備份應用和儲存卷。

    • false:只備份應用。

    storageLocation

    備份倉庫名稱。

    說明

    若您的叢集已使用Velero,請加入DingTalk使用者群(DingTalk群號: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將被自動備份。

    說明

    通過Container Service管理主控台建立的備份任務,預設IncludeClusterResources欄位為false

    defaultPvBackup

    是否備份儲存卷,開啟資料備份。取值:

    • true:備份應用和儲存卷。

    • false:只備份應用。

    storageLocation

    備份倉庫名稱。

    說明

    若您的叢集已使用Velero,請加入DingTalk使用者群(DingTalk群號: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分別為儲存聲明所在的命名空間與儲存聲明名稱。

  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資源也會被刪除。

相關文檔