GeekCoding101 make your way to geek

NodePort vs ClusterIP: Kubernetes Service Types Explained

NodePort vs ClusterIP on a real 1 master + 4 worker cluster: deploy Nginx, expose it both ways, test access, and fix CoreDNS and firewalld issues.

11 min read
Kubernetes Tutorials Feature Image Setup Cluster Part4
On this pageNodePort vs ClusterIP at a glance

ClusterIP gives a Service a stable virtual IP that is reachable only from inside the cluster. NodePort builds on ClusterIP and also opens the same port (30000–32767 by default) on every node, so clients outside the cluster can reach it. Use ClusterIP for internal traffic between microservices. Use NodePort when you need quick external access and have no load balancer.

Hey, welcome back to my Kubernetes tutorial series! Our 1 master + 4 worker node cluster is up and running, so it’s time to put both Service types to work on it.

Pods are ephemeral and their IPs change constantly. Without Services, your pods would be isolated islands with no stable way to reach each other. Services give them a fixed address, whether the traffic is pod-to-pod inside the cluster or coming from outside. Let’s break down how each one works and when to use it. 🚀

NodePort vs ClusterIP at a glance

ClusterIPNodePort
Reachable fromInside the cluster (pods, and nodes via kube-proxy rules)Inside the cluster and from outside via <NodeIP>:<NodePort>
AddressOne virtual IP + DNS nameSame ClusterIP + DNS name, plus a port on every node
Port rangeAny service portNodePort defaults to 30000–32767
Default type?Yes, used when type is omittedNo, set type: NodePort
Typical useInternal microservices, databases, backendsLabs, demos, simple external access without a LoadBalancer

The four common Kubernetes Service types

Service TypeDescriptionUse Case
ClusterIPExposes the service internally within the cluster. No external access.Internal microservices that only communicate within Kubernetes.
NodePortExposes the service on a static port on each node’s IP, making it accessible externally.Basic external access without a LoadBalancer.
LoadBalancerCreates an external load balancer that directs traffic to the service.Production environments requiring automated load balancing.
ExternalNameMaps a Service to an external DNS name instead of forwarding traffic.Redirecting traffic to services outside the cluster.

PS. You’ll also hear about the headless Service. It isn’t a separate type. It’s a ClusterIP Service with clusterIP: None, so DNS returns the pod IPs directly instead of one virtual IP.

What we’ll do in this post

✅ Create an Nginx deployment running on a single node ✅ Test it from inside the pod and from a temporary test pod ✅ Fix two networking issues you may hit along the way (CoreDNS and firewalld) ✅ Expose Nginx with a NodePort Service and test access inside and outside the cluster ✅ Expose it with a ClusterIP Service and test access inside and outside the cluster ✅ Compare the two

Let’s get started! 🚀

Deploying Nginx on a Single Node

Info: Unless stated otherwise, all commands run on k8s-1 as the admin account.

Create a testing namespace

Namespaces are like virtual clusters within your cluster. A dedicated testing namespace keeps this experiment away from existing workloads in default. Cleanup also becomes one command: delete the namespace and everything inside it goes with it.

Info: If you don’t create one, the new deployment goes into the default namespace.

List existing namespaces:

Terminal window
kubectl get namespaces -o wide
default namespaces list

Check the current default namespace:

Terminal window
kubectl config get-contexts
check current namespace

If the NAMESPACE column is empty, the namespace is default.

Create the namespace service-type-test:

Terminal window
kubectl create namespace service-type-test
added new namespace service-type-test

Make it the default for your current context:

Terminal window
kubectl config set-context --current --namespace=service-type-test
set new namespace as default

From now on, kubectl commands in this account’s session use this namespace unless you pass -n. If you also work as root, run the same command there to get the same convenience.

Verify the change:

Terminal window
kubectl config view --minify --output 'jsonpath={..namespace}'

Create a deployment YAML

Terminal window
cd
mkdir nginx-deployment
vim nginx-deployment/nginx-single-node.yaml

Paste the following. The nodeSelector pins the pod to worker node k8s-2:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-single
namespace: service-type-test
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeSelector:
kubernetes.io/hostname: k8s-2
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80

Apply the deployment

Terminal window
kubectl apply -f nginx-deployment/nginx-single-node.yaml
kubectl get pods -o wide

Example output:

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-single-7dfff5577-2v25s 1/1 Running 0 117s 10.244.1.2 k8s-2 <none> <none>

Testing Nginx from inside the pod

There’s no Service yet, so there’s no stable access path. You either exec into the pod or start a temporary test pod.

Terminal window
kubectl exec -it nginx-single-7dfff5577-2v25s -- sh

Inside the pod:

Terminal window
curl http://10.244.1.2
check nginx in pod

You should get the HTML of the default “Welcome to nginx!” page.

This method has obvious limits. It only proves Nginx works inside its own pod. It doesn’t test network policies, DNS or service discovery.

Testing Nginx by creating a temporary pod

A separate pod simulates real application traffic. It also lets you check DNS resolution and service discovery, and it’s deleted as soon as you exit.

Terminal window
kubectl run testpod --rm -it --image=rockylinux:9 -- bash

Inside testpod, install the networking tools:

Terminal window
dnf install -y iputils net-tools nc traceroute bind-utils iproute
internet access issue in pod

Troubleshooting: Pod Has No Internet Access

If that dnf install hangs or fails to resolve mirrors, the problem is usually the cluster’s DNS settings in CoreDNS.

Terminal window
kubectl edit configmap -n kube-system coredns
Add Google and Cloudflare DNS into Kubernetes

Add public resolvers 8.8.8.8 and 1.1.1.1 as upstream servers. This usually means changing the forward line in the Corefile to forward . 8.8.8.8 1.1.1.1.

Info: 1.1.1.1 is Cloudflare’s public DNS resolver. 8.8.8.8 is Google’s primary public DNS server.

Delete the existing CoreDNS pods so they’re re-created with the new settings:

Terminal window
kubectl delete pod -n kube-system -l k8s-app=kube-dns
kubectl get pods -n kube-system -l k8s-app=kube-dns
delete CoreDNS pods and check recreation

Run the dnf install command in testpod again. It should now succeed.

dnf command started working

Troubleshooting: Pods on Different Nodes Can’t Talk

Check which nodes the pods run on:

Terminal window
kubectl get pods -n service-type-test -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-single-7dfff5577-2v25s 1/1 Running 2 (46h ago) 3d23h 10.244.1.4 k8s-2 <none> <none>
testpod 1/1 Running 0 46h 10.244.2.5 k8s-3 <none> <none>

testpod is on k8s-3 and Nginx is on k8s-2. Ping the Nginx pod from testpod:

[root@testpod /]# ping 10.244.1.4
PING 10.244.1.4 (10.244.1.4) 56(84) bytes of data.
From 10.244.1.0 icmp_seq=1 Packet filtered
From 10.244.1.0 icmp_seq=2 Packet filtered

Packet filtered usually points to firewalld rules.

ICMP flowchart in Kubernetes cluster

Step 1: Understand the node’s network interfaces

When I troubleshoot Kubernetes networking, I always check the worker node’s interfaces first. The layout tells you how traffic flows within the node and between nodes. Here’s ip a on k8s-2:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:56:d2:2d brd ff:ff:ff:ff:ff:ff
inet 172.16.211.12/24 brd 172.16.211.255 scope global noprefixroute ens160
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:56:d2:37 brd ff:ff:ff:ff:ff:ff
inet 172.16.68.135/24 brd 172.16.68.255 scope global dynamic noprefixroute ens224
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 7e:e6:b8:4c:23:64 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
5: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
link/ether 06:8c:32:9e:aa:fc brd ff:ff:ff:ff:ff:ff
inet 10.244.1.0/32 scope global flannel.1
6: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default qlen 1000
link/ether 2a:f2:2f:f3:e1:ac brd ff:ff:ff:ff:ff:ff
inet 10.244.1.1/24 brd 10.244.1.255 scope global cni0
7: vethf4df6ba9@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP group default
link/ether 36:57:42:b1:e7:6f brd ff:ff:ff:ff:ff:ff link-netns cni-224ac32a-95b2-1ef2-b716-e1230f1e1296

(IPv6 and lifetime lines trimmed for readability.)

To save you time, here’s what each interface does:

InterfacePurposeNotes
loLoopbackInternal communication within the node. Always present.
ens160Primary network interfaceConnected to vmnet2, a private network for our VMs (172.16.211.12/24).
ens224Secondary network interfaceConnected to another network (172.16.68.135/24) managed by VMware Fusion, providing internet access.
docker0Docker bridge (not used by Kubernetes)Created by Docker, not part of Kubernetes networking. Leftover from local development.
flannel.1Flannel overlay networkHandles pod-to-pod traffic across nodes (10.244.1.0/32).
cni0Main CNI bridgeConnects pods on this node to the Flannel overlay (10.244.1.1/24).
veth*Virtual Ethernet pairsConnect individual pods to the cni0 bridge. Created dynamically as pods start.

Step 2: Check the firewalld zone

❯ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160 ens224 flannel.1
sources:
services: cockpit dhcpv6-client ssh
ports: 6443/tcp 2379-2380/tcp 10250/tcp 10251/tcp 10252/tcp 10255/tcp 5473/tcp 8472/udp 30000-32767/tcp
protocols:
forward: yes
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
check flannel and masquerade

Did you spot the problem?

cni0 is missing from the active zone. cni0 is the bridge that connects every pod on this node to the Flannel overlay. Because it isn’t in the public zone, firewalld rejects traffic forwarded from flannel.1 to cni0, which is exactly the path a packet from another node takes.

Step 3: Confirm with firewalld logs

Terminal window
sudo firewall-cmd --set-log-denied=all
sudo firewall-cmd --reload

Then run tail -f /var/log/messages on k8s-2 while pinging again. You should see rejects like this:

Mar 14 21:28:44 k8s-2 kernel: filter_FWD_public_REJECT: IN=flannel.1 OUT=cni0 MAC=06:8c:32:9e:aa:fc:aa:fc:a1:4d:b8:af:08:00 SRC=10.244.2.0 DST=10.244.1.4 LEN=84 TOS=0x00 PREC=0x00 TTL=62 ID=21550 DF PROTO=ICMP TYPE=8 CODE=0 ID=44547 SEQ=1

IN=flannel.1 OUT=cni0 confirms it: cross-node pod traffic is dropped at the bridge.

Step 4: Fix it

Add cni0 to the zone permanently and reload:

Terminal window
sudo firewall-cmd --permanent --zone=public --add-interface=cni0
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Do this on every worker node. Turn denied-packet logging back off when you’re done:

Terminal window
sudo firewall-cmd --set-log-denied=off

Ping 10.244.1.4 from testpod again. The replies should come back.

Exploring the NodePort Service

A NodePort Service gets a ClusterIP like any other Service. Kubernetes also reserves one port from the 30000–32767 range on every node and forwards it to the Service’s pods. That’s why 30000-32767/tcp is open in the firewalld output above.

Create nginx-deployment/nginx-nodeport.yaml:

apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
namespace: service-type-test
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80 # Service (ClusterIP) port
targetPort: 80 # containerPort in the pod
nodePort: 30080 # optional; omit it and Kubernetes picks one from 30000-32767
Terminal window
kubectl apply -f nginx-deployment/nginx-nodeport.yaml
kubectl get svc -o wide
nodeport is running

The PORT(S) column shows 80:30080/TCP: the Service port and the node port.

NodePort: Nginx access methods

FromCommand
Outside the cluster (your laptop, another VM)curl http://<any-node-ip>:30080, e.g. curl http://172.16.211.12:30080
Any nodecurl http://localhost:30080 or curl http://<node-ip>:30080
Inside a pod (testpod) via ClusterIPcurl http://<cluster-ip>:80
Inside a pod via DNScurl http://nginx-nodeport.service-type-test.svc.cluster.local

Any node works, even one that doesn’t run the Nginx pod. kube-proxy forwards the traffic to the right node over the Flannel overlay. That’s also why the firewalld fix above matters for NodePort.

Exploring the ClusterIP Service

Create nginx-deployment/nginx-clusterip.yaml:

apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip
namespace: service-type-test
spec:
type: ClusterIP # default; can be omitted
selector:
app: nginx
ports:
- port: 80
targetPort: 80
Terminal window
kubectl apply -f nginx-deployment/nginx-clusterip.yaml
kubectl get svc -o wide

Test it:

  • From testpod: curl http://nginx-clusterip (same namespace) or curl http://<cluster-ip> should return the Nginx page.
  • From outside the cluster (your laptop): the ClusterIP isn’t reachable. There’s no route to it and no node port.

What is a ClusterIP?

A ClusterIP is a virtual IP. No interface on any node actually holds it, so you won’t find it in ip a. Kubernetes assigns it from the cluster’s service CIDR and registers a DNS name for it in CoreDNS (<service>.<namespace>.svc.cluster.local). Clients talk to that stable address while the pods behind it come and go.

Why can a worker node access the ClusterIP?

kube-proxy runs on every node and programs iptables (or IPVS) rules for each Service. When a packet leaves a node or a pod with a ClusterIP as its destination, those rules rewrite it (DNAT) to one of the backing pod IPs. Because the rules exist on every node, the nodes can reach ClusterIPs too, not just pods. Machines outside the cluster have none of these rules, so the ClusterIP means nothing to them.

iptables rules added for the ClusterIP

NodePort vs ClusterIP: When to Use Which

QuestionClusterIPNodePort
Need access from outside the cluster?NoYes
Who calls it?Other pods: frontends calling APIs, apps calling databasesPeople or systems outside the cluster
Port you useThe Service port (e.g. 80)A high port (30000–32767) on every node
Firewall changesNone beyond pod networkingOpen the NodePort range on nodes
Production fitThe standard for internal servicesFine for labs and simple setups. Production usually puts a LoadBalancer or Ingress in front.

Rule of thumb: start with ClusterIP. Switch to NodePort only when something outside the cluster needs to reach the Service and you don’t have a load balancer.

Clean Up

Remember why we created a dedicated namespace? One command removes the Deployment, both Services and everything else in it:

Terminal window
kubectl delete namespace service-type-test
kubectl config set-context --current --namespace=default

FAQ

Does a NodePort Service also have a ClusterIP? Yes. NodePort builds on ClusterIP. Pods can still use the Service’s ClusterIP and DNS name, and the node port is added on top for external access.

What is the default NodePort range? 30000–32767. You can set a specific nodePort in that range or let Kubernetes pick one.

Can I reach a ClusterIP from outside the cluster? Not directly. Only pods and nodes have the kube-proxy rules that translate the ClusterIP. For external access, use NodePort, LoadBalancer, Ingress, or kubectl port-forward for quick debugging.

Is a headless Service a fifth Service type? No. It’s a ClusterIP Service with clusterIP: None. DNS returns the pod IPs instead of a single virtual IP, which is useful for StatefulSets.

My NodePort works from one node but not from the others. Why? Check pod networking between nodes first. In this lab, firewalld was dropping traffic from flannel.1 to cni0 until cni0 was added to the zone.

🎉 Congratulations!

You deployed Nginx on a pinned node and fixed CoreDNS and firewalld issues along the way. You also exposed Nginx with both NodePort and ClusterIP and saw where each one is reachable. Next time a Service “doesn’t work”, you’ll know to check the type, the node interfaces and the firewall zone.

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts