Container Service for Kubernetes (ACK) allows you to back up and restore stateful applications deployed in an ACK cluster. This is an all-in-one solution to achieve crash consistency, application consistency, and cross-region disaster recovery for stateful applications in ACK clusters. This topic describes how to migrate applications across clusters by using the application backup feature.

Prerequisites

Two Kubernetes clusters of the same version are created and the Kubernetes version are 1.18 or later. For more information, see Create an ACK managed cluster.

Introduction

In this example, a cluster named Cluster_A and another cluster named Cluster_B are used. The following example shows how to back up an application in Cluster_A and restore the application to Cluster_B.
Notice Make sure that the two clusters run the same Kubernetes version and the Kubernetes version are 1.18 or later. Otherwise, you cannot migrate the application to the specified cluster.
Backup architecture

Procedure

  1. Grant the required Object Storage Service (OSS) permissions to Cluster_A and Cluster_B. For more information, see Configure OSS permissions.
  2. Install the application backup component in Cluster_A and Cluster_B. For more information, see Install the application backup component.
  3. Create the same backup vaults for Cluster_A and Cluster_B. When you create backup vaults for Cluster_A and Cluster_B, select the same OSS bucket and set Network Type to Public Network. For more information, see Create a backup vault.
  4. Create a backup task in Cluster_A. For more information, see Create a backup task.
    After you create the backup task in Cluster_A, you can log on to Cluster_B to view the backup task on the Backups and Snapshots tab of the Application Backup page.
  5. Create a snapshot in Cluster_B. For more information, see Create a snapshot.
    If you enable Volume Backup and use storage other than disks to store data when you create the backup task, the Hybrid Backup Recovery (HBR) service is used to store the data. In this case, you must create a volume snapshot.
  6. On the Application Backup page, click the Restore tab and create a restoration task based on the backup task that you created in Cluster_A. For more information, see Create a restoration task.

Example 1: Use the backup center feature to migrate WordPress applications across ACK clusters

Install the WordPress application in a Kubernetes cluster and publish a blog post on the WordPress website. Use disks to store the text content and use Apsara File Storage NAS (NAS) file systems to store the images in the blog post. This way, both disk volumes and NAS volumes are used to store the application data. Use the backup center feature to back up the WordPress application in the source Kubernetes cluster. Then, restore the application and application data to the destination Kubernetes cluster and verify data and application integrity.

  1. In the cn-huhehaote region, create a managed Kubernetes cluster named Cluster_A and another managed Kubernetes cluster named Cluster_B.
  2. Install the WordPress application in Cluster_A. For more information, see Deploy a WordPress application by using the Helm CLI.
    On the Parameters tab, select the default namespace and replace storageClass: "alicloud-disk-ssd" with storageClass: "alibabacloud-cnfs-nas", as shown in the following code block:
    persistence:
      enabled: true
      ## wordpress data Persistent Volume Storage Class
      ## If defined, storageClassName: <storageClass>
      ## If set to "-", storageClassName: "", which disables dynamic provisioning
      ## If undefined (the default) or set to null, no storageClassName spec is
      ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
      ##   GKE, AWS & OpenStack)
      storageClass: "alibabacloud-cnfs-nas"
      ## If you want to reuse an existing claim, you can pass the name of the PVC using
      ## the existingClaim variable
      # existingClaim: your-claim
      accessMode: ReadWriteOnce
      size: 20Gi
  3. Install the application backup component and create backup vaults in Cluster_A and Cluster_B.
    Create a backup vault.
  4. Create a backup task in Cluster_A. For more information, see Create a backup task.
    Back up the default namespace. Create a backup taskAfter the 123backup-1 backup task changes to the Completed state, you can view the backup task on the Backups and Snapshots tab of the Application Backup page in Cluster_B.
  5. On the Backups and Snapshots tab, find the 123backup-1 backup task and click Create Snapshot in the Actions column.
    In the Create Snapshot dialog box, select alicloud-disk from the drop-down list in the Change StorageClass column. Create a snapshotAfter the status of the related persistent volume claim (PVC) changes to ConvertionCompleted, the snapshot is created. Create a snapshot
  6. Create a restoration task named restore-123backup-1 in Cluster_B.
    Create a restoration task
  7. Verify that the backup is restored.
    1. In the left-side navigation pane of the details page of Cluster_B, choose Workloads > Deployments. Find the WordPress application, click Details in the Actions column, and then check whether the WordPress application is in the Running state.
    2. In the left-side navigation pane of the details page, choose Network > Services
    3. On the Services page, find the Service that is created for the WordPress application and click the hyperlink in the External Endpoint column. The WordPress homepage appears.
      Homepage

Example 2: Use the backup center feature to migrate MySQL applications from an external Kubernetes cluster to an ACK cluster

Example description

  1. Create an external Kubernetes cluster. For example, create a Kubernetes cluster by using Rancher. Then, register the external Kubernetes cluster in the ACK console and use a YAML template to deploy the MySQL application and mount a local disk to the application.
  2. Create a managed Kubernetes cluster in the ACK console and deploy the application backup component in the cluster.
  3. The cluster registration proxy uses the backup center feature to back up the MySQL application and application data in the external Kubernetes cluster.
  4. Restore the MySQL application and application data to the ACK cluster in the cloud by using the backup center feature.

Migrate steps

  1. Register the external Kubernetes cluster. For more information, see Create a cluster registration proxy and register an on-premises cluster.
  2. Use a kubectl client to connect to the registered Kubernetes cluster. For more information, see Connect to ACK clusters by using kubectl.
  3. Create a persistent volume (PV), a PVC, and a MySQL application.
    1. Use the following template to create a file named mysql.yaml:
      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: local-pv
      spec:
        capacity:
          storage: 100Gi
        accessModes:
        - ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        local:
          path: /home/
        nodeAffinity:
          required:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - for-rancher-k8s-cluster002
      ---
      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: local-pvc
      spec:
        accessModes:
        - ReadWriteOnce
        resources:
          requests:
            storage: 100Gi
      ---
      apiVersion: apps/v1
      kind: StatefulSet
      metadata:
        name: mysql
      spec:
        serviceName: "mysql"
        selector:
          matchLabels:
            app: mysql
        template:
          metadata:
            labels:
              app: mysql
          spec:
            containers:
            - image: mysql:5.6
              name: mysql
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: "mysql"
              volumeMounts:
              - name: local
                mountPath: /var/lib/mysql
            volumes:
              - name: local
                persistentVolumeClaim:
                  claimName: local-pvc
    2. Run the following command to deploy the PV, PVC, and MySQL application:
      kubectl --kubeconfig rancher_huhehaote_kubeconfig apply -f mysql/mysql.yaml
    3. Run the following command to query the pod that runs the MySQL application:
      kubectl --kubeconfig rancher_huhehaote_kubeconfig get pod

      Expected output:

      NAME      READY   STATUS    RESTARTS   AGE
      mysql-0   1/1     Running   0          171m
  4. Run the following command to log on to the MySQL application. Create a database and a table, and write test data in the MySQL application.
    kubectl --kubeconfig rancher_huhehaote_kubeconfig exec -it mysql-0 sh
    root@mysql-0:/# mysql -u root -pmysql
    
    create database test;
    create table student(id int, name varchar(20),age int);
    insert into student values (1,2,3);
  5. Create a backup task in the registered Kubernetes cluster named Cluster_A. For more information, see Create a backup task.
    Back up the default namespace. Back up the MySQL applicationAfter the mysql-backup-111 backup task changes to the Completed state, you can view the backup task on the Backups and Snapshots tab of the Application Backup page in Cluster_B.
  6. On the Backups and Snapshots tab, find the backup task named mysql-backup-111 and click Create Snapshot in the Actions column.
    In the Create Snapshot dialog box, select alicloud-disk from the drop-down list in the Change StorageClass column. Create a snapshot of the MySQL applicationAfter the status of the related PVC changes to ConvertionCompleted, the snapshot is created. Create a snapshot of the MySQL application
  7. Create a restoration task named restore-111 in Cluster_B.
    Create a restoration task for the MySQL application
  8. Verify that the backup is restored.
    1. Run the following command to query the pod that runs the MySQL application:
      kubectl --kubeconfig rancher_online_kube -n mysql-111 get pod

      Expected output:

      NAME      READY   STATUS    RESTARTS   AGE
      mysql-0   1/1     Running   0          37m
    2. Log on to the restored database and view the data.
      View the data in the databaseThe output indicates that the MySQL application and application data of the external Kubernetes cluster are restored to the ACK cluster.