Tim Wißmann
Tim Wißmann

Reputation: 707

How to move a containerd image to different namespace?

I'm setting up a Kubernetes cluster with windows nodes. I accidentally created a local image in the default namespace.

As shown by ctr image ls, my image is in the default namespace:

REF                                 TYPE                                                   DIGEST       SIZE      PLATFORMS     LABELS
docker.io/library/myimage:latest   application/vnd.docker.distribution.manifest.v2+json   sha256:XXX   6.3 GiB   windows/amd64 -

Therefore, Kubernetes cannot find the image while creating the pod (ErrImageNeverPull, imagePullPolicy is set to Never). The reason for this is, the image isn't in the right namespace k8s.io:
The command ctr --namespace k8s.io image ls shows the base Kubernetes images:

REF                                                  TYPE                                                       DIGEST       SIZE       PLATFORMS                               LABELS
mcr.microsoft.com/oss/kubernetes/pause:3.6           application/vnd.docker.distribution.manifest.list.v2+json  sha256:XXX   3.9 KiB    linux/amd64,linux/arm64,windows/amd64   io.cri-containerd.image=managed
mcr.microsoft.com/oss/kubernetes/pause@sha256:DIGEST application/vnd.docker.distribution.manifest.list.v2+json  sha256:XXX   3.9 KiB    linux/amd64,linux/arm64,windows/amd64   io.cri-containerd.image=managed
...

The most straight-forward approach I tried, was exporting the image, deleting the image, and importing the image with different namespace. (as mentioned on a Github comment in the cri-tools project)

ctr --namespace k8s.io image import --base-name foo/myimage container_import.tar

It works. But I wonder, if there is any shorter (less time consuming) way than re-importing the image. (Maybe by running a simple command or changing a text file.)


To clarify my question: I have one node with a container stored in namespace "default". I want to have the same container stored in namespace "k8s.io" on the same node. What else can I do, instead of running the following two (slow) commands?

ctr -n default image export my-image.tar my-image
ctr -n k8s.io image import my-image.tar

I assume a more faster way of renaming the namespace, since it is just editing some meta data.

Upvotes: 4

Views: 3500

Answers (2)

Jon X
Jon X

Reputation: 9143

Here is a tricky approach.

sudo ctr -n default i export /dev/stdout docker.io/library/myimage:latest \
    | sudo ctr -n k8s.io i import -

Export the image from default to stdout (i.e. /dev/stdout) and pass it as stdin (i.e. - symbol) using pipeline to import it to k8s.io.

Hope this helps.

Upvotes: 1

Fariya Rahmat
Fariya Rahmat

Reputation: 3250

As @ P Ekambaram suggested, the podman save and podman load commands let you share images across multiple servers and systems when they aren't available locally or remotely.

You can use Podman to manage images and containers.

The podman save command saves an image to an archive, making it available to be loaded on another server.

For instance, to save a group of images on a host named servera:

[servera]$ podman save --output images.tar \
docker.io/library/redis \
docker.io/library/mysql \
registry.access.redhat.com/ubi8/ubi \
registry.access.redhat.com/ubi8/ubi:8.5-226.1645809065 \
quay.io/centos7/mysql-80-centos7 docker.io/library/nginx

Once complete, you can take the file images.tar to serverb and load it with podman load:

[serverb]$ podman load --input images.tar

The newly released Podman 4.0 includes the new podman image scp command, a useful command to help you manage and transfer container images.

With Podman's podman image scp, you can transfer images between local and remote machines without requiring an image registry.

Podman takes advantage of its SSH support to copy images between machines, and it also allows for local transfer. Registryless image transfer is useful in a couple of key scenarios:

Doing a local transfer between users on one system Sharing images over the network

Upvotes: 0

Related Questions