Skip to content

Instantly share code, notes, and snippets.

@diafour
Last active March 27, 2024 10:00
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save diafour/13cef191b7cf39543393d310dd6353a0 to your computer and use it in GitHub Desktop.
Save diafour/13cef191b7cf39543393d310dd6353a0 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.10.0
# Settings for kind cluster
CLUSTER_NAME="kube-19"
KIND_NODE_IMAGE="kindest/node:v1.19.7"
# 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)"
regStarted=0
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 🎁"
# Set flag to connect the registry container to a "kind" network later.
regStarted=1
fi
# Create cluster with kind.
cat <<EOF | kind create cluster \
--name $CLUSTER_NAME \
--image $KIND_NODE_IMAGE \
--config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.example.com:${REGISTRY_PORT}"]
endpoint = ["http://${REGISTRY_NAME}:5000"]
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
- role: worker
EOF
if [ $regStarted == 1 ]; then
echo " 🔗 Connect registry container to docker network 'kind'"
docker network connect "kind" "${REGISTRY_NAME}"
fi
echo " 🧩 Install Nginx Ingress"
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/kind/deploy.yaml
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=90s
}
function delete() {
echo " 👋 Will delete these containers:"
docker ps --filter 'label=io.x-k8s.kind.cluster='$CLUSTER_NAME
kind delete cluster --name $CLUSTER_NAME
}
function status() {
docker ps --filter 'label=io.x-k8s.kind.cluster='$CLUSTER_NAME
kubectl cluster-info --context=kind-$CLUSTER_NAME
}
function main() {
echo $1
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