Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use local docker image without registry setup in k8s

I've set up single node kubernetes according to official tutorial.

In addition to official documentation I've set-up single node cluster:

kubectl taint nodes --all node-role.kubernetes.io/master-

Disabled eviction limit:

cat << EOF >> /var/lib/kubelet/config.yaml
evictionHard:
  imagefs.available: 1%
  memory.available: 100Mi
  nodefs.available: 1%
  nodefs.inodesFree: 1%
EOF

systemctl daemon-reload
systemctl restart kubelet

And set systemd driver for Docker:

cat << EOF > /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

systemctl daemon-reload
systemctl restart docker

I've tried following:

docker build -t localhost:5000/my-image .
kubectl run -it --rm --restart=Always --image=localhost:5000/my-image my-image

But in pod logs I see ImagePullBackOff. If I setup local repository and I do docker push localhost:5000/my-image after I build image, then everything is working.

Is it is possible to use local images (which are already available after issuing docker images) without needing to setting up local repository, pushing to this repository and then pulling from it?

like image 435
Wakan Tanka Avatar asked Feb 14 '20 14:02

Wakan Tanka


People also ask

Does Kubernetes require a registry?

The answer: run a registry inside the Kubernetes cluster itself. This way there's no need to worry about hidden costs or pushing to external resources. You can use the default Docker registry for this purpose, but to do this securely requires setting up TLS certificates and manual twiddling.

Can I use Docker image in Kubernetes?

Kubernetes can still run containers built using Docker's Open Container Initiative (OCI) image format, meaning you can still use Dockerfiles and build your container images using Docker. Kubernetes will also continue to be able to pull from Docker registries (such as Docker hub).

Is Kubernetes a registry for storing images?

What is an Image Registry / Container Registry? Kubernetes is an orchestration management system that allows you to deploy and manage containers. A Kubernetes Pod holds related containers to support an application. When you create a new container within a Pod, you need to provide an image for the container to use.


2 Answers

You simply need to set the imagePullPolicy in your Pod template in the container specification to Never. Otherwise the kubelet will try to pull the image. The example Pod definition may look like this:

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
    - name: uses-local-image
      image: local-image-name
      imagePullPolicy: Never

More on that you can find here.

By default, the kubelet will try to pull each image from the specified registry. However, if the imagePullPolicy property of the container is set to IfNotPresent or Never, then a local image is used (preferentially or exclusively, respectively).

If you want to rely on pre-pulled images as a substitute for registry authentication, you must ensure all nodes in the cluster have the same pre-pulled images.

This can be used to preload certain images for speed or as an alternative to authenticating to a private registry.

All pods will have read access to any pre-pulled images.

like image 84
mario Avatar answered Oct 19 '22 01:10

mario


mario's answer is correct. Would be complete with the command though. So, you want to create your deployment as follows:

kubectl run -it --rm --restart Always DEPLOYMENT_NAME --image my-image --image-pull-policy IfNotPresent COMMAND

Note: if you'd have more than one node, you would need to ensure on all of them you have an image with that name, as a repo doesn't exist, and it will fail.

like image 43
suren Avatar answered Oct 19 '22 02:10

suren