|
| 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 | +for _ in {1..60}; do |
| 53 | + if ctr --namespace k8s.io version >/dev/null 2>&1; then |
| 54 | + break |
| 55 | + fi |
| 56 | + sleep 2 |
| 57 | +done |
| 58 | + |
| 59 | +# 8. The kindest/node image ships a kubelet drop-in (11-kind.conf) whose |
| 60 | +# ExecStartPre runs /kind/bin/create-kubelet-cgroup-v2.sh. That script |
| 61 | +# expects Docker-managed cgroup namespaces and fails in bare WSL2. |
| 62 | +# Patch it out so the kubelet service can start. |
| 63 | +if [ -f /etc/systemd/system/kubelet.service.d/11-kind.conf ]; then |
| 64 | + sed -i '/create-kubelet-cgroup-v2/d' /etc/systemd/system/kubelet.service.d/11-kind.conf |
| 65 | + systemctl daemon-reload |
| 66 | +fi |
| 67 | +systemctl stop kubelet 2>/dev/null || true |
| 68 | + |
| 69 | +# Initialize the control plane using a kubeadm config that forces cgroupfs and |
| 70 | +# disables swap checking. |
| 71 | +set +e |
| 72 | +kubeadm init \ |
| 73 | + --ignore-preflight-errors=all \ |
| 74 | + --config /dev/stdin <<'KUBEADM_CONFIG' |
| 75 | +apiVersion: kubeadm.k8s.io/v1beta4 |
| 76 | +kind: InitConfiguration |
| 77 | +nodeRegistration: |
| 78 | + criSocket: unix:///run/containerd/containerd.sock |
| 79 | + ignorePreflightErrors: |
| 80 | + - all |
| 81 | +--- |
| 82 | +apiVersion: kubeadm.k8s.io/v1beta4 |
| 83 | +kind: ClusterConfiguration |
| 84 | +networking: |
| 85 | + podSubnet: 10.244.0.0/16 |
| 86 | +apiServer: |
| 87 | + certSANs: |
| 88 | + - "127.0.0.1" |
| 89 | + - "localhost" |
| 90 | +--- |
| 91 | +apiVersion: kubelet.config.k8s.io/v1beta1 |
| 92 | +kind: KubeletConfiguration |
| 93 | +cgroupDriver: cgroupfs |
| 94 | +failSwapOn: false |
| 95 | +KUBEADM_CONFIG |
| 96 | +rc=$? |
| 97 | +set -e |
| 98 | + |
| 99 | +if [ $rc -ne 0 ]; then |
| 100 | + echo "=== kubeadm init failed (rc=$rc) — collecting diagnostics ===" >&2 |
| 101 | + systemctl status kubelet --no-pager 2>&1 || true |
| 102 | + journalctl -xeu kubelet --no-pager -n 80 2>&1 || true |
| 103 | + echo "=== cgroup info ===" >&2 |
| 104 | + mount | grep cgroup || true |
| 105 | + cat /proc/self/cgroup 2>/dev/null || true |
| 106 | + ls /sys/fs/cgroup/ 2>/dev/null || true |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +export KUBECONFIG=/etc/kubernetes/admin.conf |
| 111 | + |
| 112 | +# Single node cluster: allow workloads to schedule on the control-plane node. |
| 113 | +kubectl taint nodes --all node-role.kubernetes.io/control-plane- 2>/dev/null || true |
| 114 | + |
| 115 | +# kindest/node bundles the default CNI (kindnet) manifest. |
| 116 | +kubectl apply -f /kind/manifests/default-cni.yaml |
| 117 | + |
| 118 | +kubectl wait --for=condition=Ready nodes --all --timeout=180s |
0 commit comments