A local Kubernetes cluster in seconds with Kind

Jérôme Dx - Feb 2 '23 - - Dev Community

I wanted a little tool to study Kubernetes locally, there are some options, but Kind (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 :

brew install kind
# It works with Linux, Mac, Windows (with Docker Desktop and WSL2)
Enter fullscreen mode Exit fullscreen mode

Then you can create a cluster :

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
Enter fullscreen mode Exit fullscreen mode

It really takes seconds to create, you can set a timer to check that :
Kind cluster creation

Some configuration for Kind

Kind offers the possibility to make configuration to the clusters you create.

It works on top of KubeADM, you can also implement specific configuration about it.

We will explore some simple usecases.

Nginx exposition, with a NodePort service

Create the cluster :

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
Enter fullscreen mode Exit fullscreen mode

Create the deployment and the service :

k create deployment nginx --image=nginx --port=80
k create service nodeport nginx --tcp=80:80 --node-port=30000
Enter fullscreen mode Exit fullscreen mode

Now you can access the exposed service :

curl localhost:30000
Enter fullscreen mode Exit fullscreen mode

A cluster with one master and one worker

Create the cluster :

cat <<EOF | kind create cluster -n kind1 --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
EOF
Enter fullscreen mode Exit fullscreen mode

A cluster with a Nginx controller

Create the cluster :

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
Enter fullscreen mode Exit fullscreen mode

Create the resources :

# 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
Enter fullscreen mode Exit fullscreen mode

Now you can access the exposed service :

curl localhost:5678
Enter fullscreen mode Exit fullscreen mode

All this configuration code is available in this repository, I hope you will have as much pleasure as me to use this awesome tool.

. . . . . . . . . . . . . . . . . . . .
Terabox Video Player