In this blog, we will explain how to expose an application using a NodePort service in a Kind cluster.

We will keep everything simple and easy to understand.

What is NodePort?

  • A NodePort is a way to make your application reachable outside the Kubernetes cluster.
  • With NodePort, Kubernetes will open a port on every node in your cluster.
  • You can connect to your application by using the IP of your node and the NodePort port number.

Why You Cannot Directly Access NodePort in Kind

When running a regular Kubernetes cluster on VMs or physical machines, NodePorts are exposed directly on the node’s network interface.

But when you create a Kubernetes cluster using Kind, each Kubernetes node is actually running inside a Docker container. By default, Docker containers have their own network, which is separate from your computer’s network. Even if you create a NodePort service, the port is only open inside the container’s network.

Because of this separation, you cannot just open localhost:<NodePort> on your computer to reach the service. It will not work unless you do extra port mappings from the container to your host machine.

In short: The NodePort is open in the Docker container’s internal network, not on your actual machine’s network. You need to map the container port to your machine’s port if you want to access it through localhost.

Creating a Multi-Node Cluster with Port Mapping

To fix the problem, you can explicitly tell Kind to map ports from the Docker containers to your computer’s network. This way, if Kubernetes picks a NodePort like 30000, it will be tied to localhost:30000 on your machine.

You can map Node ports to 80 or 8080 as well. When you do this, you just need to make sure those ports are not already in use on your machine

1. Create a Cluster Configuration File

Create a file named multi-node-cluster.yaml (or any name you like) with the following content:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: kind-dev-cluster
nodes:
  - role: control-plane
    extraPortMappings:
      - containerPort: 30000
        hostPort: 30000
        protocol: TCP
      - containerPort: 31000
        hostPort: 31000
      - containerPort: 80
        hostPort: 80
        protocol: TCP
      - containerPort: 8080
        hostPort: 8080
        protocol: TCP
  - role: worker
  - role: worker
  • name: This is the name of your Kind cluster.
  • extraPortMappings: This tells Docker to map the container’s ports (NodePorts) to matching ports on your computer. In this example, ports 30000, 31000, and 80 inside the cluster will be accessible on localhost:30000, localhost:31000, and localhost:80 respectively.

2. Create the Kind Cluster

Use this command to create your cluster:

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

Kind will start multiple nodes (one control-plane node and four worker nodes) inside Docker containers, using the port mappings you specified.

3. Verify the Cluster

Check if your cluster is running:

kubectl cluster-info --context kind-multi-node-cluster

You should see information about your new multi-node cluster.

Deploying and Exposing a Sample Application

Now that you have a cluster with port mappings, you can deploy any application and expose it using a NodePort.

1. Deploy an Application (Nginx)

kubectl create deployment my-nginx --image=nginx

2. Expose the Deployment as a NodePort

kubectl expose deployment my-nginx --type=NodePort --port=80

Run the following command to find out which NodePort was assigned:

kubectl get services

Look at the PORT(S) column for my-nginx. It should look like 80:30000/TCP (or maybe 31000 or 32000, depending on which port was chosen).

3. Access the Application

If the service is using port 30000, for example, open:

http://localhost:30000

You should see the Nginx welcome page.


Summary

  1. Kind Runs in Containers
    Each Kubernetes node in Kind is a Docker container, so you cannot directly access NodePorts from your computer’s network without extra port mappings.
  2. Use a Custom Kind Configuration
    By adding extraPortMappings in your Kind configuration file, you map Docker container ports to your actual machine’s ports.
  3. Access via Localhost
    After creating the Kind cluster with port mappings, you can reach NodePort services at localhost:<port>.

This setup makes it easy to create a local multi-node Kubernetes cluster for testing, development, or learning—without needing extra hardware or a dedicated environment.

You can use this setup as a lab to prepare for the CKA certification. Also, check out the CKA coupon for big discounts on certification registration.

Categorized in:

KUBERNETES,