Updated on 2024-06-17 GMT+08:00

Image Migration

To ensure that container images can be properly pulled after cluster migration and improve container deployment efficiency, you are advised to migrate image repositories on a third-party cloud to Huawei Cloud SWR. The Huawei Cloud clusters and multi-cloud clusters of UCS work with SWR to provide a pipeline for automated container delivery. Images are pulled in parallel, which greatly improves container delivery efficiency.

image-migrator is an image migration tool that can automatically migrate images from image repositories on a third-party cloud to SWR.

How image-migrator Works

Figure 1 How image-migrator works

When using image-migrator to migrate images to SWR, you need to prepare two files. One is the image repository access permission file auth.json. The two objects in the file are the accounts and passwords of the source and destination image repositories (registries). The other is the image list file images.json, which consists of multiple image synchronization rules. Each rule contains a source image repository (key) and a destination image repository (value). Place these two files in the directory where image-migrator is located and run a simple command to migrate the image. The two files are described as follows:

  • auth.json

    auth.json is the image repository access permission file. Each object is the username and password of a registry. Generally, the source image repository must have the permissions for pulling images and accessing tags, and the destination image repository must have the permissions for pushing images and creating repositories. If you access the image repository anonymously, you do not need to enter the username and password. Structure of the auth.json file:

    {
        "Source image repository address": { },
        "Destination image repository address": {
            "username": "xxxxxx",
            "password": "***************",
            "insecure": true
        }
    }

    For details about the parameters in this file, see Table 1.

    Table 1 Parameters in the auth.json file

    Parameter

    Description

    Source image repository address

    The value can be in registry or registry/namespace format, which must correspond to the registry or registry/namespace format in images.json.

    NOTE:

    The matched URL in images uses the corresponding username and password for image synchronization. The registry/namespace format is preferred.

    Destination image repository address

    The value can be in registry or registry/namespace format, which must correspond to the registry or registry/namespace format in images.json.

    • If your image repository is Huawei Cloud SWR and the destination image repository address is in the registry format, you can obtain it from the SWR console as follows:

      On the Dashboard page, click Generate Login Command in the upper right corner. The domain name at the end of the login command is the SWR image repository address, for example, swr.cn-north-4.myhuaweicloud.com. Note that the address varies with the region. Switch to the corresponding region to obtain the address.

      If the value is in the registry/namespace format, replace namespace with the organization name of SWR.

    • If your image repository is Amazon ECR or ACR, log in to the image repository console of the corresponding vendor and view the push command of the image repository to obtain the image repository address.

    username

    Username. You can set it to a specific value or use a string of the ${env} or $env type to reference an environment variable.

    • If your image repository is Huawei Cloud SWR, the username of the destination SWR image repository is in the following format: Regional project name@AK.
    • If your image repository is Amazon ECR or ACR, log in to the image repository console of the corresponding vendor and view the push command of the image repository to obtain the corresponding username.

    password

    Password. You can set it to a specific value or use a string of the ${env} or $env type to reference an environment variable.

    • If your image repository is Huawei Cloud SWR, the password of the destination SWR image repository is the encrypted login key of the AK and SK. For details, see Obtaining a Long-Term Valid Login Command.
    • If your image repository is Amazon ECR or ACR, log in to the image repository console of the corresponding vendor and view the push command of the image repository to obtain the corresponding password.

    insecure

    Whether registry is an HTTP service. If yes, the value of insecure is true. The default value is false.

    Example:

    {
        "quay.io/coreos": { },
        "swr.cn-north-4.myhuaweicloud.com": {
            "username": "cn-north-4@RVHVMX******",
            "password": "***************",
            "insecure": true
        }
    }
  • images.json

    This file is essentially a list of images to migrate and consists of multiple image synchronization rules. Each rule contains a source image repository (key) and a destination image repository (value). The specific requirements are as follows:

    1. The largest unit that can be synchronized using one rule is repository. The entire namespace or registry cannot be synchronized using one rule.
    2. The formats of the source and destination repositories are similar to those of the image URL used by the docker pull/push command (registry/namespace/repository:tag).
    3. Both the source and destination repositories (if the destination repository is not an empty string) contain at least registry/namespace/repository.
    4. The source repository field cannot be empty. To synchronize data from a source repository to multiple destination repositories, you need to configure multiple rules.
    5. The destination repository name can be different from the source repository name. In this case, the synchronization function is similar to docker pull + docker tag + docker push.
    6. If the source repository field does not contain tags, all tags of the repository have been synchronized to the destination repository. In this case, the destination repository cannot contain tags.
    7. If the source repository field contains tags, only one tag in the source repository has been synchronized to the destination repository. If the destination repository does not contain tags, the source tag is used by default.
    8. If the destination repository is an empty string, the source image will be synchronized to the default namespace of the default registry. The repository and tag are the same as those of the source repository. The default registry and namespace can be configured using command line parameters and environment variables.

    Example:

    {
        "quay.io/coreos/etcd:1.0.0": "swr.cn-north-4.myhuaweicloud.com/test/etcd:1.0.0",
        "quay.io/coreos/etcd": "swr.cn-north-4.myhuaweicloud.com/test/etcd",
        "quay.io/coreos/etcd:2.7.3": "swr.cn-north-4.myhuaweicloud.com/test/etcd"
    }

    You can use a config subcommand of the image-migrator tool to automatically obtain the image that is being used by the workload in the cluster. For details, see Usage of image-migrator config. After obtaining the images.json file, you can modify, add, or delete its content as required.

Usage of image-migrator

image-migrator can run on Linux (x86 and Arm) and Windows. The usage is similar in both environments. This section uses the Linux (x86) environment as an example.

If Linux (Arm) or Windows is used, replace image-migrator-linux-amd64 in the following command with image-migrator-linux-arm64 or image-migrator-windows-amd64.exe.

Run ./image-migrator-linux-amd64 -h in the directory where image-migrator is located to learn about its usage.

  • --auth: specifies the path of auth.json. By default, auth.json is stored in the directory where image-migrator is located.
  • --images: specifies the path of images.json. By default, images.json is stored in the directory where image-migrator is located.
  • --log: specifies the path for storing logs generated by image-migrator. The default value is image-migrator.log in the current directory of image-migrator.
  • --namespace: specifies the default namespace of the destination repository. That is, if the namespace of the destination repository is not specified in images.json, you can specify it when running the migration command.
  • --registry: specifies the default registry of the destination repository. That is, if the registry of the destination repository is not specified in images.json, you can specify it when running the migration command.
  • --retries: specifies the number of retry times when the migration fails. The default value is 3.
  • --workers: specifies the number of concurrent workers for image migration. The default value is 7.
$ ./image-migrator-linux-amd64 -h
A Fast and Flexible docker registry image images tool implement by Go.

Usage:
  image-migrator [flags]

Aliases:
  image-migrator, image-migrator

Flags:
      --auth string        auth file path. This flag need to be pair used with --images. (default "./auth.json")
  -h, --help               help for image-migrator
      --images string      images file path. This flag need to be pair used with --auth (default "./images.json")
      --log string         log file path (default "./image-migrator.log")
      --namespace string   default target namespace when target namespace is not given in the images config file, can also be set with DEFAULT_NAMESPACE environment value
      --registry string    default target registry url when target registry is not given in the images config file, can also be set with DEFAULT_REGISTRY environment value
  -r, --retries int        times to retry failed tasks (default 3)
  -w, --workers int        numbers of working goroutines (default 7)

$./image-migrator --workers=5 --auth=./auth.json --images=./images.json --namespace=test \
--registry=swr.cn-north-4.myhuaweicloud.com --retries=2
$ ./image-migrator 
Start to generate images tasks, please wait ...
Start to handle images tasks, please wait ...
Images(38) migration finished, 0 images tasks failed, 0 tasks generate failed

Example:

./image-migrator --workers=5 --auth=./auth.json --images=./images.json --namespace=test --registry=swr.cn-north-4.myhuaweicloud.com --retries=2

The preceding command is used to migrate the images in the images.json file to the image repository swr.cn-north-4.myhuaweicloud.com/test. If the migration fails, you can retry twice. A maximum of five images can be migrated at a time.

Usage of image-migrator config

The config subcommand of image-migrator can be used to obtain images used in cluster applications and generate the images.json file in the directory where the tool is located. You can run ./image-migrator-linux-amd64 config -h to learn how to use the config subcommand.

  • -k, --kubeconfig: specifies the location of the kubeconfig file of kubectl. The default value is $HOME/.kube/config. The kubeconfig file is used to configure access to the Kubernetes cluster. The kubeconfig file contains the authentication credentials and endpoints (access addresses) required for accessing and registering the Kubernetes cluster. For details, see the Kubernetes documentation.
  • -n, --namespaces: specifies the namespace of the image to be obtained. Multiple namespaces are separated by commas (,), for example, ns1,ns2,ns3. The default value is "", indicating that images of all namespaces are obtained.
  • -t, --repo: specifies the destination repository address (registry/namespace).
$ ./image-migrator-linux-amd64 config -h
generate images.json

Usage:
    image-migrator config [flags]

Flags:
    -h, --help                help for config
    -k, --kubeconfig string   The kubeconfig of k8s cluster's. Default is the $HOME/.kube/config. (default "/root/.kube/config")
    -n, --namespaces string   Specify a namespace for information collection. If multiple namespaces are specified, separate them with commas (,), such as ns1,ns2. default("") is all namespaces
    -t, --repo string         target repo,such as swr.cn-north-4.myhuaweicloud.com/test

Examples:

  • Specify a namespace:

    ./image-migrator-linux-amd64 config -n default -t swr.cn-north-4.myhuaweicloud.com/test

  • Specify multiple namespaces:

    ./image-migrator-linux-amd64 config -n default,kube-system -t swr.cn-north-4.myhuaweicloud.com/test

  • If no namespace is specified, images of all namespaces are obtained:

    ./image-migrator-linux-amd64 config -t swr.cn-north-4.myhuaweicloud.com/test

Procedure

  1. Prepare the image repository access permission file auth.json.

    Create an auth.json file and modify it based on the format. If the repository is accessed anonymously, you do not need to enter information such as the username and password. Place the file in the directory where image-migrator is located.

    Example:

    {
        "quay.io/coreos": { },
        "swr.cn-north-4.myhuaweicloud.com": {
            "username": "cn-north-4@RVHVMX******",
            "password": "***************",
            "insecure": true
        }
    }

    For details about the parameters, see the auth.json file.

  2. Prepare the image list file images.json.

    1. Connect to the source cluster using kubectl. For details, see Connecting to a Cluster Using kubectl.
    2. Run the config subcommand for image migration to generate the images.json file.

      You can refer to the methods and examples in Usage of image-migrator config to obtain the image used in the source cluster application without specifying the namespace, or by specifying one or more namespaces.

    3. Modify the images.json file as required. Ensure that the file meets the eight requirements described in images.json file.

  3. Migrate images.

    You can run the default ./image-migrator-linux-amd64 command to migrate images or configure image-migrator parameters as required.

    For example, run the following command:

    ./image-migrator-linux-amd64 --workers=5 --auth=./auth.json --images=./images.json --namespace=test --registry=swr.cn-north-4.myhuaweicloud.com --retries=2

    Example:

    $ ./image-migrator-linux-amd64 
    Start to generate images tasks, please wait ...
    Start to handle images tasks, please wait ...
    Images(38) migration finished, 0 images tasks failed, 0 tasks generate failed

  4. View the result.

    After the preceding command is executed, information similar to the following is displayed:

    Images(38) migration finished, 0 images tasks failed, 0 tasks generate failed

    The preceding information indicates that 38 images have been migrated to the SWR repository.