tutorials

Developing with Docker and Kubernetes locally - and avoid the errors that "Hello World" examples do not show

View on GitHub

Setup a local registry to use in k3s

Setting up a local registry to push and pull images to involves at least three steps:

  1. Setup the registry itself
  2. Add the registry to the docker daemon
  3. Add the registry to the kubernetes cluster

After those steps you should be able to use your own registry.

Setup the registry

Because we use the local k3s cluster for this demo and we do not know, which IP adress was assigned to you, we have to use some kind of text replacement. In the following textual examples I point the url to registry.domain.de:

  1. Apply the registry-template.yaml but replace the ##REPLACE-WITH-KUBERNETES-HOST## text with the hostname you are using, here in the text I use registry.domain.de
  2. Add insecure registry to docker daemon and restart docker:

     sudo vim /var/snap/docker/current/config/daemon.json
    

    Insert the following `insecure registry:

     {
       ...
       "insecure-registries": [
         "registry.domain.de"
       ]
     }
    

    And finally restart the docker daemon:

         ubuntu@dockerhost:~$ sudo systemctl restart snap.docker.dockerd
    
  3. Add insecure registry to kubernetes host and restart k3s:

     sudo vim  /etc/rancher/k3s/registries.yaml
    

    Add the following mirror to the registries:

     mirrors:
       "registry.domain.de":
         endpoint:
           - "http://registry.domain.de"
    

    And restart the k3s service:

     sudo systemctl restart k3s
    

Finally we can test if that worked as expected, see the next section. The steps are also automated, if you followed the tutorial steps, see the Automation Section for the details.

Test that the registry is working as expected

First: Pull an arbitrary image:

> docker pull alpine

Tag that image that it will be delivered to the registry:

> docker tag alpine registry-192-168-64-11.nip.io/alpine:latest

And finally push the image to this registry:

> docker push registry-192-168-64-11.nip.io/alpine:latest

Let us have a look, that images we have locally:

docker images

You notice the alpine image and you notice the tagged image registry-192-168-64-11.nip.io/alpine:latest. To see, if our registry is working, just delete tagged image locally:

docker image rm registry-192-168-64-11.nip.io/alpine

To verify that the image was delete, run the docker images command again:

docker images

The tagged image is gone, now for the final verification step: Pull the tagged image again from the registry:

docker run -it --rm registry-192-168-64-11.nip.io/alpine /bin/sh

It works!

Automation

The whole described process is automated in the script createRegistry.sh. The script expects, that both env variables CLUSTER_HOSTNAME and DOCKERHOSTNAME are set.

References

Contact

You can contact me using Twitter (my profile) or if you have comments regarding this tutorial, visit me on GitHub and file an issue or create a pull requests.