Local Registry

With kind v0.6.0 there is a new config feature containerdConfigPatches that canbe leveraged to configure insecure registries.The following recipe leverages this to enable a local registry.

Create A Cluster And Registry

The following shell script will create a local docker registry and a kind clusterwith it enabled.

  1. #!/bin/sh
  2. set -o errexit
  3. # desired cluster name; default is "kind"
  4. KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind}"
  5. # create registry container unless it already exists
  6. reg_name='kind-registry'
  7. reg_port='5000'
  8. running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
  9. if [ "${running}" != 'true' ]; then
  10. docker run \
  11. -d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \
  12. registry:2
  13. fi
  14. # create a cluster with the local registry enabled in containerd
  15. cat <<EOF | kind create cluster --name "${KIND_CLUSTER_NAME}" --config=-
  16. kind: Cluster
  17. apiVersion: kind.x-k8s.io/v1alpha4
  18. containerdConfigPatches:
  19. - |-
  20. [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry:${reg_port}"]
  21. endpoint = ["http://registry:${reg_port}"]
  22. EOF
  23. # add the registry to /etc/hosts on each node
  24. ip_fmt='{{.NetworkSettings.IPAddress}}'
  25. cmd="echo $(docker inspect -f "${ip_fmt}" "${reg_name}") registry >> /etc/hosts"
  26. for node in $(kind get nodes --name "${KIND_CLUSTER_NAME}"); do
  27. docker exec "${node}" sh -c "${cmd}"
  28. done

Using The Registry

The registry can be used like this.

  • First we'll pull an image docker pull gcr.io/google-samples/hello-app:1.0
  • Then we'll tag the image to use the local registry docker tag gcr.io/google-samples/hello-app:1.0 localhost:5000/hello-app:1.0
  • Then we'll push it to the registry docker push localhost:5000/hello-app:1.0
  • And now we can use the image kubectl create deployment hello-server —image=registry:5000/hello-app:1.0 If you build your own image and tag it like localhost:5000/image:foo and then useit in kubernetes as registry:5000/image:foo.

Note: you may update your local hosts file as well, for example by adding 127.0.0.1 registry in your laptop's /etc/hosts, so you can reference it in a consistent way by simply using registry:5000.