Kubernetes (kind)

Written August 2, 2021, Updated September 19, 2022

There are quite a few ways to get Kubernetes up and running on your machine. On Rancher Desktop, you simply click a Kubernetes checkbox in the settings. With a WSL2 backend this is pretty good, except you need a special distro running just for Kubernetes. It's also not compatible with ARM. Without using Rancher Desktop, we'll be looking for a Linux solution, some come to mind, including:

  1. minikube
  2. microk8s (requires snap, which is a bit of a pain on WSL2)
  3. k3s (does some things on the OS level which are not ideal)
  4. k3d
  5. kind

kind is my favourite because of how isolated it is inside docker, which makes it very easy to get going, and just as easy to remove. k3d is very similar, but not a certified distribution, so you'll probably have higher chance of success with kind.

Note: at the time of this writing, kind does not support the nerdctl CLI options, so you'll need the docker cli.

Install the kubectl binary

sudo wget -q https://packages.cloud.google.com/apt/doc/apt-key.gpg -O /usr/share/keyrings/kubernetes-archive-keyring.gpg
archType="amd64"
if test "$(uname -m)" = "aarch64"
then
    archType="arm64"
fi
echo "deb [arch=${archType} signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list >/dev/null
sudo apt update
sudo apt install kubectl
kubectl version --client --short
# outputs: Client Version: v1.24.0

Install the kind binary

# see https://github.com/kubernetes-sigs/kind/releases for versions
KIND_VERSION=0.13.0

archType="amd64"
if test "$(uname -m)" == "aarch64"
then
    archType="arm64"
fi
wget "https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION}/kind-linux-${archType}" -O ~/.local/bin/kind
chmod u+x ~/.local/bin/kind
kind --version

Create your kind cluster

Generally kubectl should be within +/- two minor releases from your kubernetes cluster, but ideally should match.

In my case, I will want a Kubernetes cluster closest to version 1.24.x

Check out the kind release notes for version 0.13.0.

# create the kind configuration file
>kind.yaml cat <<EOF
---
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    image: kindest/node:v1.24.0@sha256:406fd86d48eaf4c04c7280cd1d2ca1d61e7d0d61ddef0125cb097bc7b82ed6a1
    extraPortMappings:
      - containerPort: 31337
        hostPort: 31337
EOF

# check it for YAML syntax errors
sudo apt install python3-pip
pip3 install yamllint
yamllint -d '{extends: default, rules: {line-length: disable}}' kind.yaml

If you're wondering where that sha256 hash came from, it came from their github release notes that match the kind version.

One annoying thing about kind is you need to declare your port mappings in advance. This is if you use Node Ports. They fall in the range 30000-32767. I used 31337 just so I have a port available for testing later.

# make sure docker is running
startDocker.sh
# note your current-context before kind is setup
kubectl config current-context
# create your cluster!
kind create cluster --config kind.yaml
# the first time may take a bit to download the 1 GB-ish docker image
docker images kindest/node
# notice you have a new container running:
docker ps -a -f name=kind-control-plane
# notice your kube context has been updated (~/.kube/config):
kubectl config current-context
# did it work?
sudo apt install jq
kubectl version -o json | jq -r .serverVersion.gitVersion
# outputs: v1.24.0
kubectl get namespaces
# oh yeah.
# stop kind when you're not using it to save on system resources
docker stop kind-control-plane
docker ps -a -f name=kind-control-plane
# it will start automatically everytime docker starts, or you can run: docker start kind-control-plane

Delete your kind cluster

docker ps -a -f name=kind-control-plane
kind delete cluster