Created
June 9, 2023 12:38
-
-
Save diafour/f2134abf91f64e135427dd8b2e40779d to your computer and use it in GitHub Desktop.
Run kind cluster with access to local registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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