How to use NodePort with kind?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kind (Kubernetes IN Docker) runs a full Kubernetes cluster inside Docker containers on your local machine. Because the cluster nodes are themselves containers rather than real hosts, exposing a Kubernetes NodePort service to your laptop requires an extra configuration step: you must tell kind to map container ports to host ports at cluster creation time.
This article walks through the complete workflow -- creating a kind cluster with port mappings, deploying a sample application, exposing it via a NodePort service, and verifying access from your host machine.
Prerequisites
Before you begin, make sure you have the following installed:
- Docker (version 20.10 or later)
- kubectl (matching your target Kubernetes version)
- kind (version 0.11 or later). Install instructions are at https://kind.sigs.k8s.io
Step 1 -- Create a Kind Cluster with extraPortMappings
By default, kind does not expose any container ports to the host. You must provide a cluster configuration file that maps a host port to the container port you plan to use for your NodePort service.
Create a file called kind-config.yaml:
The containerPort value must match the nodePort you will assign to your Kubernetes Service (in the valid range of 30000-32767). The hostPort is the port that becomes accessible on localhost.
Now create the cluster:
Verify the cluster is running:
Step 2 -- Deploy a Sample Application
Create a file called deployment.yaml with a simple Nginx deployment:
Apply it:
Wait for the pods to become ready:
Step 3 -- Expose the Deployment with a NodePort Service
Create a file called service.yaml:
The nodePort: 30007 must match the containerPort in your kind configuration from Step 1. Apply the service:
Check that the service is running and the NodePort is assigned:
You should see output similar to:
Step 4 -- Access the Service from Your Host
Because the kind configuration mapped hostPort: 30007 to containerPort: 30007, you can now reach the Nginx server from your laptop:
You should see the default Nginx welcome page HTML.
How the Networking Works
The chain of connections looks like this:
- Your browser or
curlsends a request tolocalhost:30007. - Docker forwards it from host port 30007 to the kind container's port 30007.
- Inside the container, the Kubernetes
kube-proxymatches port 30007 to the NodePort service. - The service routes the request to one of the Nginx pods on container port 80.
Without the extraPortMappings entry, step 2 would fail because Docker would have no port-forwarding rule.
Cleanup
When you are finished experimenting, delete the cluster:
Common Pitfalls
- Forgetting
extraPortMappingsat cluster creation. Port mappings can only be set when the cluster is created. You cannot add them to a running cluster; you must delete and recreate it. - Mismatched port numbers. The
containerPortinkind-config.yamlmust exactly match thenodePortin your Kubernetes Service manifest. A mismatch means traffic never reaches the service. - Port already in use on the host. If another process (or another kind cluster) is already listening on the host port, cluster creation will fail. Check with
lsof -i :30007before creating the cluster. - Using NodePort outside the valid range. Kubernetes only allows NodePort values between 30000 and 32767 by default. Specifying a port outside this range causes the service creation to fail.
- Not waiting for pods to be ready. Applying the service before the deployment pods are running results in connection refused errors. Always verify pod readiness with
kubectl get pods --watchbefore testing.
Summary
- Kind requires
extraPortMappingsin its cluster configuration to forward host ports into the Docker container that acts as a Kubernetes node. - The
containerPortin the kind config must match thenodePortin your Kubernetes Service manifest. - After applying the deployment and NodePort service, access your application at
localhost:\<hostPort\>. - Port mappings cannot be added after cluster creation; plan them before running
kind create cluster. - Use
kubectl get svcandcurlto verify end-to-end connectivity from your host to the pods.
Related reading
- How to use PodTemplate
- How to use rolling update to re-pull container image?
- How to use Session Affinity on requests to Kubernetes service?
- How to use skaffold with volumes
- How to use OpenAPI oneOf property with openapi-generator-maven-plugin when generating Spring code
- How to use python ray for independent computers (each have its username and password) via internet(distributed computation with ip address)?
- How to use the kubernetes go-client to get the same Pod status info that kubectl gives
- How to use variables with forward slash in kubernetes chart?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.