Skip to content

Instantly share code, notes, and snippets.

@diafour
Created June 9, 2023 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diafour/f2134abf91f64e135427dd8b2e40779d to your computer and use it in GitHub Desktop.
Save diafour/f2134abf91f64e135427dd8b2e40779d to your computer and use it in GitHub Desktop.
Run kind cluster with access to local registry
#!/usr/bin/env bash
# A helper for kind to create and delete clusters with untrusted local registry.
# Based on https://kind.sigs.k8s.io/docs/user/local-registry/
#
# Note: kind 0.17.0
set -Eeo pipefail
# Settings for kind cluster
CLUSTER_NAME="kube-23"
KIND_NODE_IMAGE="kindest/node:v1.23.13@sha256:ef453bb7c79f0e3caba88d2067d4196f427794086a7d0df8df4f019d5e336b61"
# Container's name for registry
REGISTRY_NAME="registry2"
# A port to be used in repositories, e.g. "localhost:5000"
REGISTRY_PORT="5000"
# A path where blobs will be located.
REGISTRY_STORAGE_PATH=$HOME/registry
# !!! DO NOT EDIT BELOW !!!
function create() {
# Check registry: create registry container unless it already exists.
echo Check local registry ...
regRunning="$(docker inspect -f '{{.State.Running}}' "${REGISTRY_NAME}" 2>/dev/null || true)"
if [ "${regRunning}" == 'true' ]; then
echo -e " \033[32mโœ“\033[0m Registry is running ๐ŸŽ"
else
echo -e " \033[31mโœ—\033[0m No registry running, start ..."
mkdir -p ${REGISTRY_STORAGE_PATH}
docker run \
--detach \
--restart always \
--publish "${REGISTRY_PORT}:5000" \
--name "${REGISTRY_NAME}" \
--volume ${REGISTRY_STORAGE_PATH}:/var/lib/registry \
--env REGISTRY_STORAGE_DELETE_ENABLED=true \
registry:2
echo -e " \033[32mโœ“\033[0m Registry started ๐ŸŽ"
fi
# Create cluster with kind.
kindCfg=$(cat <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${REGISTRY_PORT}"]
endpoint = ["http://${REGISTRY_NAME}:5000"]
networking:
ipFamily: ipv4
nodes:
- role: control-plane
- role: worker
EOF
)
if ! kind create cluster --name $CLUSTER_NAME --image $KIND_NODE_IMAGE --config=- <<<"${kindCfg}" ; then
echo "kind create cluster failed"
exit 1
fi
echo " ๐Ÿ”— Connect registry container to docker network 'kind'"
docker network connect "kind" "${REGISTRY_NAME}" 2>&1 | grep -v "already exist" || true
}
function delete() {
echo " ๐Ÿ‘‹ Will delete these containers:"
docker ps --filter 'label=io.x-k8s.kind.cluster='$CLUSTER_NAME || true
kind delete cluster --name $CLUSTER_NAME
}
function status() {
docker ps --filter 'label=io.x-k8s.kind.cluster='$CLUSTER_NAME || true
kubectl cluster-info --context=kind-$CLUSTER_NAME || true
}
function main() {
echo $1
if [[ $cmd == "start" ]] ; then
echo "Use 'create' command instead of 'start'"
cmd="create"
fi
if [[ $cmd == "stop" ]] ; then
read -p "'stop' will destroy a cluster. Continue? [Enter] โ†’ Yes, [Ctrl+C] โ†’ No."
cmd="delete"
fi
for cmd in create delete status ; do
if [ "$1" == "$cmd" ]; then
$cmd
return $?
fi
done
# unknown command
cat <<EOF
Usage:
$0 create|delete|status
EOF
return 1
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment