A local Kubernetes cluster in seconds with Kind
Tags: cloud, kubernetes, containers, ckad
Published: 2023-02-02 08:30:00

I wanted a little tool to study Kubernetes locally, there are some options, but [Kind](https://kind.sigs.k8s.io) (**Kubernetes In Docker**) was my preferred choice, you just need Docker, and you can handle a Kubernetes setup very easily.

## Kind setup

Install it with Brew :
```bash
brew install kind
# It works with Linux, Mac, Windows (with Docker Desktop and WSL2)
```

Then you can create a cluster :
```bash
kind create cluster -n kind1 [--config conf.yml]
kind create cluster -n kind2

# Get the list of clusters
kind get clusters

# Select the cluster you want, by selecting context
k config get-contexts
k config use-context kind-kind1

# Display available addresses for the current cluster
k cluster-info

# Export logs about the cluster
kind export logs -n kind1

# You can delete clusters, when you want
kind delete cluster -n kind1
kind delete cluster -n kind2
```

**It really takes seconds** to create, you can set a timer to check that :
![Kind cluster creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j670qp358eqxe68qbuua.png)

### Some configuration for Kind

Kind offers the possibility to make [configuration](https://kind.sigs.k8s.io/docs/user/configuration/) to the clusters you create.

It works on top of [KubeADM](https://github.com/kubernetes/kubeadm), you can also implement [specific configuration](https://kind.sigs.k8s.io/docs/user/configuration/#kubeadm-config-patches) about it.

We will explore some simple usecases.

#### Nginx exposition, with a NodePort service

Create the cluster :
```bash
cat <<EOF | kind create cluster -n kind1 --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30000
    hostPort: 30000
    protocol: TCP
EOF
```

Create the deployment and the service :
```bash
k create deployment nginx --image=nginx --port=80
k create service nodeport nginx --tcp=80:80 --node-port=30000
```

Now you can access the exposed service :
```bash
curl localhost:30000
```

#### A cluster with one master and one worker

Create the cluster :
```bash
cat <<EOF | kind create cluster -n kind1 --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
EOF
```

#### A cluster with a Nginx controller

Create the cluster :
```bash
cat <<EOF | kind create cluster -n kind1 --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: kind
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  - containerPort: 80
    hostPort: 8080
    protocol: TCP
  - containerPort: 443
    hostPort: 44300
    protocol: TCP
EOF
```

Create the resources :
```bash
# install the Nginx ingress
k apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml

# install the resources
cat <<EOF | k apply -f -
kind: Pod
apiVersion: v1
metadata:
  name: test-app
  labels:
    app: test-app
spec:
  containers:
  - name: test-app
    image: hashicorp/http-echo:latest
    args:
    - "-text=The test has been successful!"
---
kind: Service
apiVersion: v1
metadata:
  name: test-service
spec:
  selector:
    app: test-app
  ports:
  - port: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
spec:
  rules:
  - http:
      paths:
      - pathType: Prefix
        path: "/app"
        backend:
          service:
            name: test-service
            port:
              number: 5678
EOF

# forward port
k port-forward service/test-service 5678:5678
```

Now you can access the exposed service :
```bash
curl localhost:5678
```

All this configuration code is available in [this repository](https://github.com/jdxlabs/k8s-local-kind/), I hope you will have as much pleasure as me to use this awesome tool.