Skip to main content

Setting Up Kind Kubernetes Cluster (Multi Node)

โ€” Aswin

In this blog, we will learn how to create a multi-node Kubernetes cluster using Kind (Kubernetes in Docker). It is a quick setup for you to get started with kubernetes.

We will also see how to expose applications using NodePort.

๐Ÿ’ก
If you are preparing for a Kubernetes certification, a multi-node cluster setup is essential for learning the concepts effectively.

Prerequisites

Before starting, make sure you have the following installed on your machine:

As the name suggests, Kind (Kubernetes in Docker) requires Docker to run the cluster.

If you dont have docker installed, follow Docker Desktop Installtion Guide

๐Ÿ’ก
Kind needs dockerpodman or nerdctl installed (any one) in your system.

Install Kind

Next step is to install Kind.

For Linux,

# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.26.0/kind-linux-amd64
# For ARM64
[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.26.0/kind-linux-arm64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

Mac Users,

brew install kind

Windows users,

choco install kind

For other installation options and methods, please refer the official installation guide.

YAML Configuration for the Multi-Node Cluster

Using a YAML file for creating a Kind cluster is better than using the default command because,

You can specify how many nodes you want, map ports, or set up networking in one place. Itโ€™s much easier to change a line in the YAML file than to remember a long command.

Lets get started.

Create a file called multi-node-cluster.yaml and paste the following content:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: multi-node-cluster
nodes:
  - role: control-plane
    extraPortMappings:
      - containerPort: 30000
        hostPort: 30000
        protocol: TCP
      - containerPort: 31000
        hostPort: 31000
        protocol: TCP
      - containerPort: 32000
        hostPort: 32000
        protocol: TCP
  - role: worker
  - role: worker
  - role: worker
  - role: worker

What Do These Fields Mean?

  • kind and apiVersion: Tells Kind how to read the file.
  • name: The name of our Kind cluster (multi-node-cluster).
  • nodes: A list of the nodes in the cluster.
    • role: control-plane: This is our Kubernetes control plane node.
    • role: worker: These are worker nodes for your cluster.
    • extraPortMappings: This allows you to map ports from the Docker host to ports inside the container. This is helpful if you want to access NodePort services from outside the cluster on your local machine.

Here, we mapped container ports 30000, 31000, and 32000 to the same ports on our machine.

This means if a service is exposed on NodePort 30000 inside Kubernetes, you can access it locally at localhost:30000.

Create the Cluster

Open a terminal, go to the directory where you saved your multi-node-cluster.yaml file, and run:

kind create cluster --config multi-node-cluster.yaml

Test with Nginx using a NodePort

Letโ€™s deploy a simple Nginx server in our new cluster and expose it with a NodePort service.

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 32000
EOF

Find the NodePort assigned to Nginx:

$ kubectl get service nginx-service   

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-service   NodePort   10.96.237.203   <none>        80:32000/TCP   7m1s
๐Ÿ’ก
Notice the 80:32000/TCP. The 32000 is the NodePort. Thanks to our extraPortMappings in the Kind config, the host machine can access anything on port 32000.

Open your browser and go to http://localhost:32000.

You should see the default Nginx page.

How HostPort and NodePort Mapping Works

  • NodePort: This is a Kubernetes Service type that listens on a port on each node in the cluster. If you specify a NodePort of 30000, Kubernetes opens port 30000 on each node.
  • HostPort: In our Kind extraPortMappings, we mapped the hostโ€™s port (hostPort) to a containerโ€™s port (containerPort). This allows traffic hitting localhost:30000 on your machine to reach port 30000 on the control plane node.

In simple words:

  1. Your browser talks to your local host on port 30000.
  2. The Docker host (your machine) then forwards this traffic to the Kind container on port 30000.
  3. The Kind cluster receives it and passes it to the Nginx service which listens on NodePort 30000.

Conclusion

You have successfully deployed a multi-node Kubernetes cluster locally using Kind.

You also exposed Nginx with a NodePort service and accessed it from your browser. This is a great starting point for anyone wanting to learn Kubernetes in a simple and quick way on their own machine.

Feel free to explore more by deploying different apps and services in your new cluster!

Happy Learning!

Aswin
Website India