|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Bring up a single node Kubernetes control plane inside the kindest/node rootfs |
| 3 | +# that oci-to-wsl imported into WSL. kind normally runs this image as a Docker |
| 4 | +# container and drives kubeadm from the host through `docker exec`; here WSL |
| 5 | +# boots the image's systemd directly, so the equivalent bootstrap is performed |
| 6 | +# in place. |
| 7 | +set -euxo pipefail |
| 8 | + |
| 9 | +# -------------------------------------------------------------------------- |
| 10 | +# Reproduce the setup that kind's container entrypoint (/usr/local/bin/entrypoint) |
| 11 | +# performs when running kindest/node inside Docker. WSL boots systemd directly |
| 12 | +# from the image's rootfs, so these steps must be done explicitly. |
| 13 | +# -------------------------------------------------------------------------- |
| 14 | + |
| 15 | +# 1. Mount propagation — kubeadm and kubelet need shared mounts. |
| 16 | +mount --make-rshared / 2>/dev/null || true |
| 17 | + |
| 18 | +# 2. /dev/kmsg — kubelet reads kernel messages from this device and exits |
| 19 | +# immediately if it is missing. WSL2 does not create it by default. |
| 20 | +if [ ! -e /dev/kmsg ]; then |
| 21 | + ln -sf /dev/console /dev/kmsg |
| 22 | +fi |
| 23 | + |
| 24 | +# 3. Kernel parameters required by kube-proxy and networking. |
| 25 | +sysctl -w net.ipv4.ip_forward=1 || true |
| 26 | +sysctl -w net.ipv4.conf.all.forwarding=1 || true |
| 27 | +sysctl -w net.ipv6.conf.all.forwarding=1 2>/dev/null || true |
| 28 | + |
| 29 | +# 4. Ensure /run is a tmpfs (systemd usually handles this, but be safe). |
| 30 | +if ! mountpoint -q /run; then |
| 31 | + mount -t tmpfs tmpfs /run |
| 32 | + mkdir -p /run/lock |
| 33 | +fi |
| 34 | + |
| 35 | +# 5. Ensure a machine-id exists (kubelet uses it as node identity). |
| 36 | +if [ ! -s /etc/machine-id ]; then |
| 37 | + systemd-machine-id-setup 2>/dev/null || uuidgen | tr -d '-' > /etc/machine-id |
| 38 | +fi |
| 39 | + |
| 40 | +# 6. containerd configuration — use cgroupfs driver. WSL2's systemd has limited |
| 41 | +# cgroup delegation, so the cgroupfs driver is more reliable here. |
| 42 | +mkdir -p /etc/containerd |
| 43 | +containerd config default \ |
| 44 | + | sed 's/SystemdCgroup = true/SystemdCgroup = false/' \ |
| 45 | + > /etc/containerd/config.toml |
| 46 | + |
| 47 | +# 7. Start containerd. |
| 48 | +systemctl enable --now containerd |
| 49 | +systemctl restart containerd |
| 50 | + |
| 51 | +# Wait for the container runtime to accept requests. |
| 52 | +containerd_ready=0 |
| 53 | +for _ in {1..60}; do |
| 54 | + if ctr --namespace k8s.io version >/dev/null 2>&1; then |
| 55 | + containerd_ready=1 |
| 56 | + break |
| 57 | + fi |
| 58 | + sleep 2 |
| 59 | +done |
| 60 | +if [ "$containerd_ready" -ne 1 ]; then |
| 61 | + echo "containerd did not become ready after 120s" >&2 |
| 62 | + systemctl status containerd --no-pager || true |
| 63 | + journalctl -xeu containerd --no-pager -n 80 || true |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +# 8. The kindest/node image ships a kubelet drop-in (11-kind.conf) whose |
| 68 | +# ExecStartPre runs /kind/bin/create-kubelet-cgroup-v2.sh. That script |
| 69 | +# expects Docker-managed cgroup namespaces and fails in bare WSL2. |
| 70 | +# Patch it out so the kubelet service can start. |
| 71 | +if [ -f /etc/systemd/system/kubelet.service.d/11-kind.conf ]; then |
| 72 | + sed -i '/create-kubelet-cgroup-v2/d' /etc/systemd/system/kubelet.service.d/11-kind.conf |
| 73 | + systemctl daemon-reload |
| 74 | +fi |
| 75 | +systemctl stop kubelet 2>/dev/null || true |
| 76 | + |
| 77 | +# Initialize the control plane using a kubeadm config that forces cgroupfs and |
| 78 | +# disables swap checking. |
| 79 | +set +e |
| 80 | +kubeadm init \ |
| 81 | + --ignore-preflight-errors=all \ |
| 82 | + --config /dev/stdin <<'KUBEADM_CONFIG' |
| 83 | +apiVersion: kubeadm.k8s.io/v1beta4 |
| 84 | +kind: InitConfiguration |
| 85 | +nodeRegistration: |
| 86 | + criSocket: unix:///run/containerd/containerd.sock |
| 87 | + ignorePreflightErrors: |
| 88 | + - all |
| 89 | +--- |
| 90 | +apiVersion: kubeadm.k8s.io/v1beta4 |
| 91 | +kind: ClusterConfiguration |
| 92 | +networking: |
| 93 | + podSubnet: 10.244.0.0/16 |
| 94 | +apiServer: |
| 95 | + certSANs: |
| 96 | + - "127.0.0.1" |
| 97 | + - "localhost" |
| 98 | +--- |
| 99 | +apiVersion: kubelet.config.k8s.io/v1beta1 |
| 100 | +kind: KubeletConfiguration |
| 101 | +cgroupDriver: cgroupfs |
| 102 | +failSwapOn: false |
| 103 | +KUBEADM_CONFIG |
| 104 | +rc=$? |
| 105 | +set -e |
| 106 | + |
| 107 | +if [ $rc -ne 0 ]; then |
| 108 | + echo "=== kubeadm init failed (rc=$rc) — collecting diagnostics ===" >&2 |
| 109 | + systemctl status kubelet --no-pager 2>&1 || true |
| 110 | + journalctl -xeu kubelet --no-pager -n 80 2>&1 || true |
| 111 | + echo "=== cgroup info ===" >&2 |
| 112 | + mount | grep cgroup || true |
| 113 | + cat /proc/self/cgroup 2>/dev/null || true |
| 114 | + ls /sys/fs/cgroup/ 2>/dev/null || true |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | + |
| 118 | +export KUBECONFIG=/etc/kubernetes/admin.conf |
| 119 | + |
| 120 | +# Single node cluster: allow workloads to schedule on the control-plane node. |
| 121 | +kubectl taint nodes --all node-role.kubernetes.io/control-plane- 2>/dev/null || true |
| 122 | + |
| 123 | +# The bundled kindnet manifest (/kind/manifests/default-cni.yaml) contains Go |
| 124 | +# template placeholders (e.g. {{ .PodSubnet }}) that kind normally renders at |
| 125 | +# cluster creation time. Render the template and apply; if that fails, install |
| 126 | +# a standalone CNI config for host-local networking on this single node. |
| 127 | +if ! sed -e 's/{{ \.PodSubnet }}/10.244.0.0\/16/g' \ |
| 128 | + -e 's/{{ \.PodSubnet}}/10.244.0.0\/16/g' \ |
| 129 | + -e 's/{{\.PodSubnet}}/10.244.0.0\/16/g' \ |
| 130 | + -e '/^{{/d' -e '/^}}/d' \ |
| 131 | + /kind/manifests/default-cni.yaml | kubectl apply -f - 2>&1; then |
| 132 | + echo "kindnet manifest apply failed; installing host-local CNI config directly" |
| 133 | + mkdir -p /etc/cni/net.d |
| 134 | + cat > /etc/cni/net.d/10-kindnet.conflist <<'CNI' |
| 135 | +{ |
| 136 | + "cniVersion": "0.4.0", |
| 137 | + "name": "kindnet", |
| 138 | + "plugins": [ |
| 139 | + { |
| 140 | + "type": "ptp", |
| 141 | + "ipMasq": false, |
| 142 | + "ipam": { "type": "host-local", "dataDir": "/run/cni-ipam-state", "routes": [{"dst": "0.0.0.0/0"}], "ranges": [[{"subnet": "10.244.0.0/24"}]] } |
| 143 | + }, |
| 144 | + { "type": "portmap", "capabilities": {"portMappings": true} } |
| 145 | + ] |
| 146 | +} |
| 147 | +CNI |
| 148 | +fi |
| 149 | + |
| 150 | +# Assign podCIDR to the node so kubelet can allocate pod IPs. |
| 151 | +NODE=$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}') |
| 152 | +kubectl patch node "$NODE" -p '{"spec":{"podCIDR":"10.244.0.0/24","podCIDRs":["10.244.0.0/24"]}}' 2>/dev/null || true |
| 153 | + |
| 154 | +kubectl wait --for=condition=Ready nodes --all --timeout=300s |
0 commit comments