Wednesday, July 28, 2021

How to create local registry setup for kind

You can create a local registry in docker as a container.

And use the same in kubernetes environment running using kind(kubernetes in docker).


The host machine is same.


The registry is running as a docker container.

Also, the kubernetes nodes are running as docker containers.


Ref: https://kind.sigs.k8s.io/docs/user/local-registry/



#!/bin/sh
set -o errexit

# create registry container unless it already exists

clustername=$1
reg_name='kind-registry'
reg_port='5000'

echo "RegistryName: ${reg_name}"
echo "Clustername: ${clustername}"
echo "Port: ${reg_port}"


running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
if [ "${running}" != 'true' ]; then
  docker run \
    -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \
    registry:2
fi



# create a cluster with the local registry enabled in containerd
cat <<EOF | kind create cluster --name ${clustername} --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
    endpoint = ["http://${reg_name}:${reg_port}"]
EOF

# connect the registry to the cluster network
# (the network may already be connected)
docker network connect "kind" "${reg_name}" || true

# Document the local registry
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
cat <<EOF | kubectl apply --context kind-${clustername} -f -
apiVersion: v1
kind: ConfigMap
metadata:
  name: local-registry-hosting
  namespace: kube-public
data:
  localRegistryHosting.v1: |
    host: "localhost:${reg_port}"
    help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF

You need to run this file using:
chmod +x new_cluster.sh
./new_cluster.sh "clustername"

The above shell script is directly available in the reference page but I added a command line argument to create a custom cluster.
The above script checks for the registry whether already running and create it if not running.
Once it is found/created then the registry is connected to the cluster network(kind) and the local registry configuration is added to the cluter to use this.

No comments:

Post a Comment