All Products
Search
Document Center

Container Service for Kubernetes:Use Terraform to back up and restore applications

Last Updated:Jul 30, 2024

You can use Command Line Interface (CLI) of Terraform to deploy custom resources (CRs) for backup vaults, application backup, and application restoration in backup clusters and restoration clusters to migrate applications across clusters.

Prerequisites

Usage notes

  • Do not run the delete command by using kubectl to delete backup and restoration tasks. Otherwise, related cloud resources may fail to be completely deleted. For more information about how to delete backups, see the Step 5: Delete resources from the backup center section of this topic.

  • Pay close attention to the new releases of the backup center component named migrate-controller and update the component to the latest version at your earliest convenience. For more information, see Manage components.

  • Do not delete the parameters in the following sample code in case the backup files cannot be restored.

Step 1: Associate clusters

You can use the Kubernetes provider to associate clusters. For more information about the Kubernetes provider, see Kubernetes Provider.

In this example, a KubeConfig file is used to describe how to associate a cluster.

  1. Create a Terraform directory.

  2. Create a configuration file named csdr.tf in the Terraform directory and copy the following content to the configuration file to associate it with the KubeConfig file.

    provider "kubernetes" {
      config_path = "~/.kube/config"
    }
  3. Run the following command to initialize the runtime environment for Terraform:

    terraform init

    The following output is returned, which indicates that Terraform is initialized.

    View output

    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!
    

Step 2: Create a backup vault

  1. Add the following content to the csdr.tf configuration file and modify the content based on your business requirements.

    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"
      }
    }

    Parameter

    Required

    Description

    name

    Yes

    The name of the backup vault. The name must comply with the Kubernetes naming convention.

    network

    Yes

    The network mode that is used to access the specified Object Storage Service (OSS) bucket. Valid values:

    • internal: internal mode. All associated clusters must be deployed in the same region as the OSS bucket.

    • public: public mode. This mode does not have region limits.

    region

    Yes

    The region where the OSS bucket resides.

    bucket

    Yes

    The name of the OSS bucket. Make sure that the OSS bucket name exists and starts with cnfs-oss-.

    prefix

    No

    The subdirectory in the OSS bucket. If you specify this parameter, backups are stored in the specified subdirectory.

  2. Run the following command to create a resource planning:

    terraform plan

    The following output is returned, which indicates that the resource planning is created.

    View output

      # 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. Run the following command to create a backup vault:

    terraform apply

    The following output is returned, which indicates that the backup vault is created.

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

Step 3: Create a backup task

Create an instant backup task

  1. Add the following content to the csdr.tf configuration file and modify the content based on your business requirements.

    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"
        }
      }
      
      # If the value of the status.phase parameter changes to Completed, the backup is successful.
      # The backup duration depends on the number of applications in the cluster and the amount of data stored in the volumes.
      #wait {
      #  fields = {
      #    "status.phase" = "Completed"
      #  }
      #}
    
      #timeouts {
      #  create = "60m"
      #}
     }
    }

    Parameter

    Required

    Description

    csdr.alibabacloud.com/backuplocations

    Yes

    The backup vault that stores the backups. Make sure that the information is the same as the configuration of the backup vault.

    name

    Yes

    The name of the real-time backup task.

    includedNamespaces

    Yes

    The name of the namespace.

    includedResources

    No

    The type of cluster resource to be included in the backup list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    excludedResources

    No

    The type of cluster resource to be excluded from the backup list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    matchLabels

    No

    The labels of the resources to be backed up.

    includeClusterResources

    No

    Specify whether to back up all cluster-level resources, such as StorageClasses, CustomResourceDefinitions (CRDs), and webhooks.

    • true: back up all cluster-level resources.

    • false: back up only cluster-level resources used by namespace-level resources in the specified namespaces. For example, when the system backs up a pod, the service account used by the pod is assigned a cluster role. In this case, the cluster role is automatically backed up. When the system backs up a CustomResource (CR), the corresponding CRD is backed up.

    Note

    By default, IncludeClusterResources is set to false for backup tasks created in the ACK console.

    defaultPvBackup

    Specifies whether to back up volumes. Valid values:

    • true: backs up applications and volumes.

    • false: backs up only applications.

    storageLocation

    Yes

    The name of the backup vault.

    Note

    If your ACK cluster uses Velero, join DingTalk group 35532895 to request technical support.

    ttl

    Yes

    The validity period of backups. You cannot restore data from expired backups. Specify the time period in the 720h0m0s format. Valid values: 24h0m0s to 1572864h0m0s.

  2. Run the following command to create a resource planning:

    terraform plan

    The following output is returned, which indicates that the resource planning is created.

    View output

      # 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. Run the following command to create an instant backup task.

    terraform apply

    The following output is returned, which indicates that the instant backup task is created.

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

Create a scheduled backup task

  1. Add the following content to the csdr.tf configuration file and modify the content based on your business requirements.

    View configuration file

    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"
      }
    }

    Parameter

    Required

    Description

    csdr.alibabacloud.com/backuplocations

    Yes

    The backup vault that stores the backups. Make sure that the information is the same as the configuration of the backup vault.

    name

    Yes

    The name of the scheduled backup task.

    schedule

    Yes

    The backup cycle specified by a cron expression.

    includedNamespaces

    Yes

    The name of the namespace.

    includedResources

    No

    The type of cluster resource to be included in the backup list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    excludedResources

    No

    The type of cluster resource to be excluded from the backup list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    matchLabels

    No

    The labels of the resources to be backed up.

    includeClusterResources

    No

    Specify whether to back up all cluster-level resources, such as StorageClasses, CustomResourceDefinitions (CRDs), and webhooks.

    • true: back up all cluster-level resources.

    • false: back up only cluster-level resources used by namespace-level resources in the specified namespaces. For example, when the system backs up a pod, the service account used by the pod is assigned a cluster role. In this case, the cluster role is automatically backed up. When the system backs up a CustomResource (CR), the corresponding CRD is backed up.

    Note

    By default, IncludeClusterResources is set to false for backup tasks created in the ACK console.

    defaultPvBackup

    Yes

    Specifies whether to back up volumes. Valid values:

    • true: backs up applications and volumes.

    • false: backs up only applications.

    storageLocation

    Yes

    The name of the backup vault.

    Note

    If your ACK cluster uses Velero, join DingTalk group 35532895 to request technical support.

    ttl

    Yes

    The validity period of backups. You cannot restore data from expired backups. Specify the time period in the 720h0m0s format. Valid values: 24h0m0s to 1572864h0m0s.

  2. Run the following command to create a resource planning:

    terraform plan

    The following output is returned, which indicates that the resource planning is created.

    View output

      # 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. Run the following command to create a scheduled backup task:

    terraform apply

    The following output is returned, which indicates that the scheduled backup task is created.

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

Related operations

  • Search for backups based on the scheduled backup task.

    Use labelSelector to find the backups created by the scheduled backup task.

    data "kubernetes_resources" "list-applicationbackup" {
      api_version 			= "csdr.alibabacloud.com/v1beta1"
      kind       				= "ApplicationBackup"
      namespace         = "csdr"
      label_selector    = "csdr/schedule-name=terraform-schedule-test"
    }
    
    # Return the output. 
    output "applicationbackup-name" {
      value = data.kubernetes_resources.list-applicationbackup.objects
    }
  • Modify the scheduled backup task.

    1. Set the spec.paused parameter to true to pause the scheduled backup task, and then modify other parameters in the spec parameter such as spec.schedule that defines the backup behavior in the backup task based on your business requirements. Sample code:

      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 = {
            Pause the backup task. 
            paused = "true" 
            # Modify the backup cycle of the backup task. 
            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 = {
            # After the backup task is paused, the state of the backup task changes to Paused as expected. 
            "status.phase" = "Paused"
          }
        }
        timeouts {
          create = "10m"
        }
      }
    2. Run the following command to create a resource planning:

      terraform plan

      The following output is returned, which indicates that the resource planning is created.

      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. Run the following command to apply the modified scheduled backup task:

      terraform apply

      The following output is returned, which indicates that the scheduled backup task is modified.

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

Step 4: Create a restoration task

  1. Add the following content to the csdr.tf configuration file and modify the content based on your business requirements.

    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>"
          }
        }
      }
      
      # If the value of the status.phase parameter changes to Completed, the restoration task is successful. 
      # The restoration duration depends on the number of applications in the cluster and the amount of data stored in the volumes. 
      #wait {
      #  fields = {
      #    "status.phase" = "Completed"
      #  }
      #}
    
      #timeouts {
      #  create = "60m"
      #}
    }

    Parameter

    Required

    Description

    csdr.alibabacloud.com/backuplocations

    Yes

    The backup vault that stores the backups. Make sure that the information is the same as the configuration of the backup vault.

    name

    Yes

    The name of the restore task.

    appRestoreOnly

    No

    Specifies whether to restore only applications from backups that contain volume data. Persistent volume claims (PVCs), persistent volumes (PVs), and the relevant data are not restored. Valid values:

    • true: restores only applications. If you want to change the data source where you back up applications, you need to manually create a PVC and PV, create a restore task, and then set this parameter to true.

    • false: restores applications and the relevant volume data. Default value: false.

    preserveNodePorts

    No

    Specifies whether to retain the NodePort of the application. If the backup cluster and restore cluster use the same NodePort, set this parameter to false to change the NodePort to a random port. In other scenarios, set the parameter to true.

    includedNamespaces

    Yes

    The name of the namespace to be restored. If you leave this parameter empty, all namespaces that have been backed up are restored.

    includedResources

    No

    The type of cluster resource to be included in the restore list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    excludedResources

    No

    The type of cluster resource to be excluded from the restore list.

    Important

    To ensure that backups are created as expected, specify only one of the following parameters: includedResources and excludedResources. If you leave both parameters empty, all resource types are backed up.

    backupName

    Yes

    The name of the backup to be restored. If you use the scheduled backup feature, you need to specify the name of a backup that is created at a point in time. Example: <yourBackupScheduleName>-20221205225845.

    namespaceMapping

    No

    This parameter maps the namespace in the backup cluster to a namespace in the restore cluster. Valid values:

    • <backupNamespace>: the namespace of the backups in the backup cluster.

    • <restoreNamespace>: the namespace to which resources are restored in the restore cluster. If you do not specify this parameter, the resources are restored to a namespace that uses the same name as the namespace in the backup cluster.

    Note

    If <restoreNamespace> does not exist, the system automatically creates one.

    convertedarg

    No

    The StorageClass conversion list. For volumes of the FileSystem type, such as OSS, NAS, CPFS, and local volumes, you can configure this parameter to convert the StorageClasses of their PVCs to the specified StorageClass during the restoration process. For example, you can convert NAS volumes to disk volumes.

    • convertToStorageClassType: the desired StorageClass. Make sure that the StorageClass exists in the current cluster. You can specify only the disk or NAS StorageClass.

    • namespace: the namespace of the PVC.

    • persistentVolumeClaim: the name of the PVC.

    You can run the kubectl -ncsdr describe <backup-name> command to query the PVC information of a backup. In the returned status.resourceList.dataResource.pvcBackupInfo list, the dataType field displays the data type of the PVC, which can be FileSystem or Snapshot. The nameSpace and pvcName fields display the namespace and name of the PVC.

    Important

    For ReadWriteMany volumes, restore to disk is not supported. For ReadOnlyMany volumes, when you restore to disk, ensure that replicas are not simultaneously mounted on multiple nodes to avoid forced disk detachment.

  2. Run the following command to create a resource planning:

    terraform plan

    The following output is returned, which indicates that the resource planning is created.

    View output

      # 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. Run the following command to create a restoration task.

    terraform apply

    The following output is returned, which indicates that the restoration task is created.

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

Step 5: Delete resources from the backup center

Important

Other clusters may also use the backup vault that you created. Therefore, the backup center does not allow you to delete the BackupLocation object.

Delete the scheduled backup task

You can run the terraform destroy command to delete the BackupSchedule resource to stop the scheduled backup task.

Delete the backup task or restoration task

  1. Add the following content to the csdr.tf configuration file and modify the content based on your business requirements.

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

    Parameter

    Required

    Description

    name

    Yes

    The name of the deletion request.

    • To delete a backup task, specify the request name in the following format: ApplicationBackup name of the backup task+"-dbr".

    • To delete a restore task, specify the request name in the following format: ApplicationRestore name of the restore task+"-dbr".

    deleteObjectName

    Yes

    The name of the resource to be deleted.

    deleteObjectType

    Yes

    The type of resource to be deleted. Valid values:

    • "Backup": deletes the ApplicationBackup object and relevant resources of the backup task.

    • "Restore": deletes the ApplicationRestore object and relevant resources of the restore task.

    Note
    • Deleting backup tasks does not affect the backups that are created in the cluster.

    • Deleting restore tasks does not affect the data that is restored.

  2. Run the following command to create a resource planning:

    terraform plan

    The following output is returned, which indicates that the resource planning is created.

    View output

      # 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. Run the following command to delete resources:

    terraform apply

    The following output is returned, which indicates that a deleterequest resource is created to delete resources.

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

    After the system deletes the corresponding backup or restoration task based on the deleterequest resource, the deleterequest resource is also deleted.

References