9.2.4. 使用 MTC API 迁移应用程序

您可以使用 MTC API 从命令行迁移应用程序。

流程

  1. 为主机集群创建一个 MigCluster CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: migration.openshift.io/v1alpha1
    kind: MigCluster
    metadata:
      name: <host_cluster>
      namespace: openshift-migration
    spec:
      isHostCluster: true
    EOF
  2. 为每个远程集群创建一个 Secret CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: v1
    kind: Secret
    metadata:
      name: <cluster_secret>
      namespace: openshift-config
    type: Opaque
    data:
      saToken: <sa_token> 1
    EOF
    1
    指定远程集群的 base64 编码的 migration-controller 服务帐户(SA)令牌。您可以运行以下命令来获取令牌:
    $ oc sa get-token migration-controller -n openshift-migration | base64 -w 0
  3. 为每个远程集群创建一个 MigCluster CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: migration.openshift.io/v1alpha1
    kind: MigCluster
    metadata:
      name: <remote_cluster> 1
      namespace: openshift-migration
    spec:
      exposedRegistryPath: <exposed_registry_route> 2
      insecure: false 3
      isHostCluster: false
      serviceAccountSecretRef:
        name: <remote_cluster_secret> 4
        namespace: openshift-config
      url: <remote_cluster_url> 5
    EOF
    1
    指定远程集群的 Cluster CR。
    2
    可选: 要直接镜像迁移,请指定公开的 registry 路由。
    3
    如果 false 则启用 SSL 验证。如果为 true,则不需要 CA 证书或不检查 CA 证书。
    4
    指定远程集群的 Secret CR 对象。
    5
    指定远程集群的 URL。
  4. 验证所有集群是否处于 Ready 状态:

    $ oc describe cluster <cluster>
  5. 为复制存储库创建 Secret CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: v1
    kind: Secret
    metadata:
      namespace: openshift-config
      name: <migstorage_creds>
    type: Opaque
    data:
      aws-access-key-id: <key_id_base64> 1
      aws-secret-access-key: <secret_key_base64> 2
    EOF
    1
    指定 base64 格式的密钥 ID。
    2
    指定 base64 格式的 secret 密钥。

    AWS 凭证默认为 base64 编码。对于其他存储供应商,您必须使用每个密钥运行以下命令来对凭证进行编码:

    $ echo -n "<key>" | base64 -w 0 1
    1
    指定密钥 ID 或 secret 密钥。这两个密钥都必须都是 base64 编码。
  6. 为复制存储库创建一个 MigStorage CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: migration.openshift.io/v1alpha1
    kind: MigStorage
    metadata:
      name: <migstorage>
      namespace: openshift-migration
    spec:
      backupStorageConfig:
        awsBucketName: <bucket> 1
        credsSecretRef:
          name: <storage_secret> 2
          namespace: openshift-config
      backupStorageProvider: <storage_provider> 3
      volumeSnapshotConfig:
        credsSecretRef:
          name: <storage_secret> 4
          namespace: openshift-config
      volumeSnapshotProvider: <storage_provider> 5
    EOF
    1
    指定存储桶名称。
    2
    指定对象存储的 Secrets CR。您必须确保存储在对象存储的 Secrets CR 中的凭证是正确的。
    3
    指定存储供应商。
    4
    可选: 如果要使用快照复制数据,请指定对象存储的 Secrets CR。您必须确保存储在对象存储的 Secrets CR 中的凭证是正确的。
    5
    可选: 如果您使用快照复制数据,请指定存储供应商。
  7. 验证 MigStorage CR 是否处于 Ready 状态:

    $ oc describe migstorage <migstorage>
  8. 创建一个 MigPlan CR 清单:

    $ cat << EOF | oc apply -f -
    apiVersion: migration.openshift.io/v1alpha1
    kind: MigPlan
    metadata:
      name: <migplan>
      namespace: openshift-migration
    spec:
      destMigClusterRef:
        name: <host_cluster>
        namespace: openshift-migration
      indirectImageMigration: true 1
      indirectVolumeMigration: true 2
      migStorageRef:
        name: <migstorage> 3
        namespace: openshift-migration
      namespaces:
        - <application_namespace> 4
      srcMigClusterRef:
        name: <remote_cluster> 5
        namespace: openshift-migration
    EOF
    1
    如果为 false,则启用直接镜像迁移。
    2
    如果为 false,则启用直接卷迁移。
    3
    指定 MigStorage CR 实例的名称。
    4
    指定一个或多个源命名空间。默认情况下,目标命名空间具有相同的名称。
    5
    指定源集群 MigCluster 实例的名称。
  9. 验证 MigPlan 实例是否处于 Ready 状态:

    $ oc describe migplan <migplan> -n openshift-migration
  10. 创建一个 MigMigration CR 清单,以启动 MigPlan 实例中定义的迁移:

    $ cat << EOF | oc apply -f -
    apiVersion: migration.openshift.io/v1alpha1
    kind: MigMigration
    metadata:
      name: <migmigration>
      namespace: openshift-migration
    spec:
      migPlanRef:
        name: <migplan> 1
        namespace: openshift-migration
      quiescePods: true 2
      stage: false 3
      rollback: false 4
    EOF
    1
    指定 MigPlan CR 名称。
    2
    如果为 true,则源集群上的 pod 会在迁移前停止。
    3
    如果为 true,则进行阶段(stage)迁移,即在不停止应用程序的情况下复制大多数数据。
    4
    如果为 true,则会回滚到一个已完成的迁移。
  11. 通过观察 MigMigration CR 进度来验证迁移:

    $ oc watch migmigration <migmigration> -n openshift-migration

    输出类似于以下:

    输出示例

    Name:         c8b034c0-6567-11eb-9a4f-0bc004db0fbc
    Namespace:    openshift-migration
    Labels:       migration.openshift.io/migplan-name=django
    Annotations:  openshift.io/touch: e99f9083-6567-11eb-8420-0a580a81020c
    API Version:  migration.openshift.io/v1alpha1
    Kind:         MigMigration
    ...
    Spec:
      Mig Plan Ref:
        Name:       migplan
        Namespace:  openshift-migration
      Stage:        false
    Status:
      Conditions:
        Category:              Advisory
        Last Transition Time:  2021-02-02T15:04:09Z
        Message:               Step: 19/47
        Reason:                InitialBackupCreated
        Status:                True
        Type:                  Running
        Category:              Required
        Last Transition Time:  2021-02-02T15:03:19Z
        Message:               The migration is ready.
        Status:                True
        Type:                  Ready
        Category:              Required
        Durable:               true
        Last Transition Time:  2021-02-02T15:04:05Z
        Message:               The migration registries are healthy.
        Status:                True
        Type:                  RegistriesHealthy
      Itinerary:               Final
      Observed Digest:         7fae9d21f15979c71ddc7dd075cb97061895caac5b936d92fae967019ab616d5
      Phase:                   InitialBackupCreated
      Pipeline:
        Completed:  2021-02-02T15:04:07Z
        Message:    Completed
        Name:       Prepare
        Started:    2021-02-02T15:03:18Z
        Message:    Waiting for initial Velero backup to complete.
        Name:       Backup
        Phase:      InitialBackupCreated
        Progress:
          Backup openshift-migration/c8b034c0-6567-11eb-9a4f-0bc004db0fbc-wpc44: 0 out of estimated total of 0 objects backed up (5s)
        Started:        2021-02-02T15:04:07Z
        Message:        Not started
        Name:           StageBackup
        Message:        Not started
        Name:           StageRestore
        Message:        Not started
        Name:           DirectImage
        Message:        Not started
        Name:           DirectVolume
        Message:        Not started
        Name:           Restore
        Message:        Not started
        Name:           Cleanup
      Start Timestamp:  2021-02-02T15:03:18Z
    Events:
      Type    Reason   Age                 From                     Message
      ----    ------   ----                ----                     -------
      Normal  Running  57s                 migmigration_controller  Step: 2/47
      Normal  Running  57s                 migmigration_controller  Step: 3/47
      Normal  Running  57s (x3 over 57s)   migmigration_controller  Step: 4/47
      Normal  Running  54s                 migmigration_controller  Step: 5/47
      Normal  Running  54s                 migmigration_controller  Step: 6/47
      Normal  Running  52s (x2 over 53s)   migmigration_controller  Step: 7/47
      Normal  Running  51s (x2 over 51s)   migmigration_controller  Step: 8/47
      Normal  Ready    50s (x12 over 57s)  migmigration_controller  The migration is ready.
      Normal  Running  50s                 migmigration_controller  Step: 9/47
      Normal  Running  50s                 migmigration_controller  Step: 10/47