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

Migrating Images to SWR using image-migrator

To ensure that container images can be properly pulled after cluster migration and improve container deployment efficiency, you are advised to migrate self-built image repositories to Huawei Cloud SoftWare Repository for Container (SWR).

image-migrator is an image migration tool that can automatically migrate images from a Docker registry built on Docker Registry v2 to SWR.

Preparations

Before the migration, prepare a server with kubectl installed for the connection between the source and destination clusters. The server must have at least 5 GB local disk space and at least 8 GB memory so that image-migrator can work properly and can store related data, such as data collected from the source cluster and recommendation data of the destination cluster.

image-migrator can run on Linux (x86 and Arm) and Windows.

Before using image-migrator in Linux, run the chmod u+x Tool name command (for example, chmod u+x image-migrator-linux-amd64) to grant the execute permission.

Table 1 Obtaining the image-migrator package

image-migrator

image-migrator is an image migration tool that can automatically migrate images from a Docker registry built on Docker Registry v2 to SWR, or from a registry on a third-party cloud to SWR.

Linux x86: https://ucs-migration.obs.cn-north-4.myhuaweicloud.com/toolkits/image-migrator-linux-amd64

Linux Arm: https://ucs-migration.obs.cn-north-4.myhuaweicloud.com/toolkits/image-migrator-linux-arm64

Windows: https://ucs-migration.obs.cn-north-4.myhuaweicloud.com/toolkits/image-migrator-windows-amd64.exe

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 registry access permission file auth.json. The two objects in the file are the accounts and passwords of the source and destination registries. The other is the image list file images.json, which consists of multiple image synchronization rules. Each rule contains a source repository (key) and a destination 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 registry access permission file. Each object is the username and password of a registry. Generally, you must have permission to pull images from and access image tags in the source registry and permission to push images to and create repositories in the destination registry. If you access a registry anonymously, you do not need to enter the username and password. Structure of the auth.json file:

    {
        "Source registry address": { },
        "Destination registry address": {
            "username": "xxxxxx",
            "password": "xxxxxx",
            "insecure": true
        }
    }

    To be more specific:

    • The Source registry address and Destination registry address can be in the registry or registry/namespace format matching that in images.json. The matched URL in images uses the corresponding username and password for image synchronization. The registry/namespace format is preferred.

      If the destination registry address is in the registry format, you can obtain it from the SWR console. 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 registry address, for example, swr.cn-north-4.myhuaweicloud.com. Note that the address varies depending on 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.

    • username: (Optional) username. You can set it to a specific value or use a string of the ${env} or $env type to reference an environment variable.
    • password: (Optional) password. You can set it to a specific value or use a string of the ${env} or $env type to reference an environment variable.
    • insecure: (Optional) whether registry is an HTTP service. If yes, the value of insecure is true. The default value is false.

    The username of the destination SWR registry is in the following format: Regional project name@AK. The password is the encrypted login key of the AK and SK. For details, see Obtaining a Long-Term Valid Login Command.

    Example:

    {
        "quay.io/coreos": { },
        "swr.cn-north-4.myhuaweicloud.com": {
            "username": "cn-north-4@RVHVMX******",
            "password": "cab4ceab4a1545***************",
            "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 repository (key) and a destination 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 registry is not an empty string) contain at least registry/namespace/repository.
    4. The source registry field cannot be empty. To synchronize data from a source registry to multiple destination registries, 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"
    }

    We provide 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 needed.

How to Use 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 images in the images.json file to 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 registry 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": "cab4ceab4a1545***************",
            "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 to make it meet the requirements 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 needed.

    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 SWR.