|
| 1 | +# This script sets up a local development cluster. It's roughly equivalent to |
| 2 | +# a managed K8s setup. |
| 3 | + |
| 4 | +# For ease of development and to save disk space we pipe a local container |
| 5 | +# registry through to kind. |
| 6 | +# |
| 7 | +# See https://kind.sigs.k8s.io/docs/user/local-registry/. |
| 8 | + |
| 9 | +reg_name='kind-registry' |
| 10 | +reg_port='5001' |
| 11 | +if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then |
| 12 | + docker run \ |
| 13 | + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --network bridge --name "${reg_name}" \ |
| 14 | + registry:2 |
| 15 | +fi |
| 16 | + |
| 17 | +# Start a basic cluster. We use cilium's CNI and eBPF kube-proxy replacement. |
| 18 | + |
| 19 | +cat <<EOF | kind create cluster --config - |
| 20 | +--- |
| 21 | +kind: Cluster |
| 22 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 23 | +nodes: |
| 24 | + - role: control-plane |
| 25 | + - role: worker |
| 26 | + - role: worker |
| 27 | +networking: |
| 28 | + disableDefaultCNI: true |
| 29 | + kubeProxyMode: none |
| 30 | +containerdConfigPatches: |
| 31 | + - |- |
| 32 | + [plugins."io.containerd.grpc.v1.cri".registry] |
| 33 | + config_path = "/etc/containerd/certs.d" |
| 34 | +EOF |
| 35 | + |
| 36 | +# Enable the registry on the nodes. |
| 37 | + |
| 38 | +REGISTRY_DIR="/etc/containerd/certs.d/localhost:${reg_port}" |
| 39 | +for node in $(kind get nodes); do |
| 40 | + docker exec "${node}" mkdir -p "${REGISTRY_DIR}" |
| 41 | + cat <<EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml" |
| 42 | +[host."http://${reg_name}:5000"] |
| 43 | +EOF |
| 44 | +done |
| 45 | + |
| 46 | +# Connect the registry to the cluster network. |
| 47 | + |
| 48 | +if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then |
| 49 | + docker network connect "kind" "${reg_name}" |
| 50 | +fi |
| 51 | + |
| 52 | +# Advertise the registry location. |
| 53 | + |
| 54 | +cat <<EOF | kubectl apply -f - |
| 55 | +apiVersion: v1 |
| 56 | +kind: ConfigMap |
| 57 | +metadata: |
| 58 | + name: local-registry-hosting |
| 59 | + namespace: kube-public |
| 60 | +data: |
| 61 | + localRegistryHosting.v1: | |
| 62 | + host: "localhost:${reg_port}" |
| 63 | + help: "https://kind.sigs.k8s.io/docs/user/local-registry/" |
| 64 | +EOF |
| 65 | + |
| 66 | +# Prepare Gateway API CRDs. These MUST be available before we start cilium. |
| 67 | + |
| 68 | +kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/experimental-install.yaml |
| 69 | + |
| 70 | +kubectl wait --for condition=Established crd/gatewayclasses.gateway.networking.k8s.io |
| 71 | +kubectl wait --for condition=Established crd/gateways.gateway.networking.k8s.io |
| 72 | +kubectl wait --for condition=Established crd/httproutes.gateway.networking.k8s.io |
| 73 | +kubectl wait --for condition=Established crd/tlsroutes.gateway.networking.k8s.io |
| 74 | +kubectl wait --for condition=Established crd/grpcroutes.gateway.networking.k8s.io |
| 75 | +kubectl wait --for condition=Established crd/referencegrants.gateway.networking.k8s.io |
| 76 | + |
| 77 | +# Start cilium. |
| 78 | + |
| 79 | +helm repo add cilium https://helm.cilium.io |
| 80 | + |
| 81 | +helm upgrade \ |
| 82 | + --install cilium cilium/cilium \ |
| 83 | + --version 1.15.0-pre.3 \ |
| 84 | + --namespace kube-system \ |
| 85 | + --set k8sServiceHost=kind-control-plane \ |
| 86 | + --set k8sServicePort=6443 \ |
| 87 | + --set kubeProxyReplacement=strict \ |
| 88 | + --set gatewayAPI.enabled=true \ |
| 89 | + --wait |
| 90 | + |
| 91 | +# Set up MetalLB. Kind's nodes are containers running on the local docker |
| 92 | +# network. We reuse that network for LB-IPAM so that LoadBalancers are available |
| 93 | +# via "real" local IPs. |
| 94 | + |
| 95 | +KIND_NET_CIDR=$(docker network inspect kind -f '{{(index .IPAM.Config 0).Subnet}}') |
| 96 | +METALLB_IP_START= $(echo ${KIND_NET_CIDR} | sed "[email protected]/[email protected]@") |
| 97 | +METALLB_IP_END= $(echo ${KIND_NET_CIDR} | sed "[email protected]/[email protected]@") |
| 98 | +METALLB_IP_RANGE="${METALLB_IP_START}-${METALLB_IP_END}" |
| 99 | + |
| 100 | +helm install --namespace metallb-system --create-namespace \ |
| 101 | + --repo https://metallb.github.io/metallb metallb metallb \ |
| 102 | + --version 0.13.12 \ |
| 103 | + --wait |
| 104 | + |
| 105 | +cat <<EOF | kubectl apply -f - |
| 106 | +--- |
| 107 | +apiVersion: metallb.io/v1beta1 |
| 108 | +kind: L2Advertisement |
| 109 | +metadata: |
| 110 | + name: l2-ip |
| 111 | + namespace: metallb-system |
| 112 | +spec: |
| 113 | + ipAddressPools: |
| 114 | + - default-pool |
| 115 | +--- |
| 116 | +apiVersion: metallb.io/v1beta1 |
| 117 | +kind: IPAddressPool |
| 118 | +metadata: |
| 119 | + name: default-pool |
| 120 | + namespace: metallb-system |
| 121 | +spec: |
| 122 | + addresses: |
| 123 | + - ${METALLB_IP_RANGE} |
| 124 | +EOF |
| 125 | + |
| 126 | +# At this point we have a similar setup to the one that we'd get with a cloud |
| 127 | +# provider. Move on to `01_operations.sh` for the cluster setup. |
0 commit comments