Skip to content

Instantly share code, notes, and snippets.

@gmorse81
Created April 23, 2022 14:40
Show Gist options
  • Save gmorse81/49a4d9998831aa98d604e015a817a078 to your computer and use it in GitHub Desktop.
Save gmorse81/49a4d9998831aa98d604e015a817a078 to your computer and use it in GitHub Desktop.
Local kubernetes startup
#!/bin/sh
set -o errexit
up()
{
if ! docker ps > /dev/null 2>&1;
then
echo You need to install docker before running this tool
exit 1
fi
if ! kind -h > /dev/null 2>&1;
then
if [[ $OSTYPE == 'darwin'* ]] && brew -h > /dev/null 2>&1; then
echo 'Since you are on a mac and already have brew installed, this script is installing Kind now'
brew install kind
else
echo You need to install kind to run this script. see https://kind.sigs.k8s.io/
exit 1
fi
fi
echo "Starting up Kind with local container registry and ingress controller"
# create registry container unless it already exists
reg_name='kind-registry'
reg_port='5001'
if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != '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 --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}: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
EOF
# connect the registry to the cluster network if not already connected
if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
docker network connect "kind" "${reg_name}"
fi
# 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 -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
echo "installing nginx ingress controller for Kind"
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
echo "waiting for ingress to be ready"
sleep 20
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=180s
}
down()
{
echo "removing kind cluster and local registry"
kind delete cluster
docker stop kind-registry
docker rm kind-registry
}
if [[ "$1" = "up" ]]
then
up
fi
if [[ "$1" = "down" ]]
then
down
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment