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.
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 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:
$ 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:
Your browser talks to your local host on port 30000.
The Docker host (your machine) then forwards this traffic to the Kind container on port 30000.
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!
Aswin Vijayan is a DevOps engineer with a passion for open-source tools and automation. In his free time, Aswin enjoys reading and exploring new technologies.
Setting Up Kind Kubernetes Cluster (Multi Node)
โ Aswin
Setting Up Kind Kubernetes Cluster (Multi Node)
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
.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
Install Kind
Next step is to install Kind.
For Linux,
Mac Users,
Windows users,
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:What Do These Fields Mean?
multi-node-cluster
).NodePort
services from outside the cluster on your local machine.Here, we mapped container ports
30000
,31000
, and32000
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:Test with Nginx using a NodePort
Letโs deploy a simple Nginx server in our new cluster and expose it with a NodePort service.
Find the NodePort assigned to Nginx:
80:32000/TCP
. The32000
is the NodePort. Thanks to ourextraPortMappings
in the Kind config, the host machine can access anything on port32000
.Open your browser and go to
http://localhost:32000
.You should see the default Nginx page.
How HostPort and NodePort Mapping Works
30000
, Kubernetes opens port30000
on each node.extraPortMappings
, we mapped the hostโs port (hostPort
) to a containerโs port (containerPort
). This allows traffic hittinglocalhost:30000
on your machine to reach port30000
on the control plane node.In simple words:
30000
.30000
.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 Vijayan is a DevOps engineer with a passion for open-source tools and automation. In his free time, Aswin enjoys reading and exploring new technologies.