Skip to content

Instantly share code, notes, and snippets.

@wallentx
Last active October 27, 2023 21:43
Show Gist options
  • Save wallentx/d1bdb95f21a5fb36a38f5794c00dd03b to your computer and use it in GitHub Desktop.
Save wallentx/d1bdb95f21a5fb36a38f5794c00dd03b to your computer and use it in GitHub Desktop.
Create a KinD cluster ezpz
#!/usr/bin/env bash
set -o errexit
function checkDeps() {
if [[ $OSTYPE =~ ^darwin ]]; then
command -v kind &>/dev/null || brew install kind
command -v kubectl &>/dev/null || brew install kubectl
elif [[ $OSTYPE =~ ^linux ]]; then
command -v kind &>/dev/null || go install sigs.k8s.io/kind@v0.17.0 || echo "Install kind from your package manager"
command -v kubectl &>/dev/null || curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" &&
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
fi
}
#### Start Custom Changes ####
#
# Remove the following line to verify that you've modifed this script
delete_me=true
# Using your LAN IP here can be more useful
host_ip="127.0.0.1"
# Put in any path that you want the host to also have access to (which the pods can then also access)
host_mount_path="$HOME/example/db"
# Where to mount on the host container
container_mount_path="/opt/db"
#
#### End Custom Changes ####
# (There are still more things to customize below tho)
if [[ $delete_me == true ]]; then
echo "Please modify the script in the 'Custom Changes' section to fit your system before running"
exit 1
fi
checkDeps
# 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
# Uncomment items as-needed
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
# AnyVolumeDataSource: true
CPUManager: true
DevicePlugins: true
# DisableCloudProviders: true
# HPAContainerMetrics: true
# HPAScaleToZero: true
# HugePages: true
MemoryManager: true
# MemoryQoS: true
# MountContainers: true
# MountPropagation: true
# Sysctls: true
KubeletPodResources: true
networking:
apiServerAddress: "$host_ip"
apiServerPort: 6443
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
extraMounts:
- hostPath: "$host_mount_path"
containerPath: "$container_mount_path"
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
endpoint = ["http://${reg_name}:5000"]
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
for node in $(kind get nodes); do
kubectl annotate node "${node}" "kind.x-k8s.io/registry=localhost:${reg_port}"
done
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment