kubernetes deploy with tar docker image

11/18/2019

I have a problem to deploy docker image via kubernetes.

One issue is that, we cannot use any docker image registry service e.g. docker hub or any cloud services. But, yes I have docker images as .tar file.

However, it always fails with following message

Warning  Failed     1s         kubelet, dell20    

Failed to pull image "test:latest": rpc
error: code = Unknown
desc = failed to resolve image "docker.io/library/test:latest":
failed to do request: Head https://registry-1.docker.io/v2/library/test/manifests/latest: dial tcp i/o timeout

I also change deployment description with IfNotPresent or Never. In this case it will fail anyway with ErrImageNeverPull.

My guess is: kubernetes tries to use Docker Hub anyway, since it https://registry-1.docker.io in order to pull the image. I just want to use tar docker image in local disk, rather than pulling from some services.

And yes the image is in docker:

docker images
REPOSITORY  TAG    IMAGE ID      CREATED     SIZE
test        latest 9f4916a0780c  6 days ago  1.72GB

Can anyone give me any advices on this problem?

-- zedoul
docker
kubernetes

1 Answer

11/19/2019

I was successful with using local image with Kubernetes cluster. I provided the explanation with example below:

The only prerequisite is that you need to make sure you have access to upload this image directly to nodes.

Create the image

Pull the default nginx image from docker registry with below command:

$ docker pull nginx:1.17.5

Nginx image is used only for demonstration purposes.

Tag this image with new name as nginx-local with command:

$ docker tag nginx:1.17.5 nginx-local:1.17.5

Save this image as nginx-local.tar executing command:

$ docker save nginx-local:1.17.5 > nginx-local.tar

Link to documentation: docker save

File nginx-local.tar is used as your image.

Copy the image to all of the nodes

The problem with this technique is that you need to ensure all of the nodes have this image.
Lack of image will result in failed pod creation.

To copy it you can use scp. It's secure way to transer files between machines.
Example command for scp:

$ scp /path/to/your/file/nginx-local.tar user@ip_adddress:/where/you/want/it/nginx-local.tar

If image is already on the node, you will need to load it into local docker image repository with command:

$ docker load -i nginx-local.tar

To ensure that image is loaded invoke command

$ docker images | grep nginx-local

Link to documentation: docker load:

It should show something like that:

docker images | grep nginx
nginx-local                                           1.17.5              540a289bab6c        3 weeks ago         126MB

Creating deployment with local image

The last part is to create deployment with use of nginx-local image.

Please note that:

  • The image version is explicitly typed inside yaml file.
  • ImagePullPolicy is set to Never. ImagePullPolicy

Without this options the pod creation will fail.

Below is example deployment which uses exactly that image:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-local
  namespace: default
spec:
  selector:
    matchLabels:
      run: nginx-local
  replicas: 5 
  template:
    metadata:
      labels:
        run: nginx-local
    spec:
      containers:
      - image: nginx-local:1.17.5
        imagePullPolicy: Never
        name: nginx-local
        ports:
        - containerPort: 80

Create this deployment with command: $ kubectl create -f local-test.yaml

The result was that pods were created successfully as shown below:

NAME                           READY   STATUS    RESTARTS   AGE
nginx-local-84ddb99b55-7vpvd   1/1     Running   0          2m15s
nginx-local-84ddb99b55-fgb2n   1/1     Running   0          2m15s
nginx-local-84ddb99b55-jlpz8   1/1     Running   0          2m15s
nginx-local-84ddb99b55-kzgw5   1/1     Running   0          2m15s
nginx-local-84ddb99b55-mc7rw   1/1     Running   0          2m15s

This operation was successful but I would recommend you to use local docker repository. It will easier management process with images and will be inside your infrastructure. Link to documentation about it: Local Docker Registry

-- Dawid Kruk
Source: StackOverflow