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
| ClusterIP | NodePort | |
|---|---|---|
| Reachable from | Inside the cluster (pods, and nodes via kube-proxy rules) | Inside the cluster and from outside via <NodeIP>:<NodePort> |
| Address | One virtual IP + DNS name | Same ClusterIP + DNS name, plus a port on every node |
| Port range | Any service port | NodePort defaults to 30000–32767 |
| Default type? | Yes, used when type is omitted | No, set type: NodePort |
| Typical use | Internal microservices, databases, backends | Labs, demos, simple external access without a LoadBalancer |
The four common Kubernetes Service types
| Service Type | Description | Use Case |
|---|---|---|
| ClusterIP | Exposes the service internally within the cluster. No external access. | Internal microservices that only communicate within Kubernetes. |
| NodePort | Exposes the service on a static port on each node’s IP, making it accessible externally. | Basic external access without a LoadBalancer. |
| LoadBalancer | Creates an external load balancer that directs traffic to the service. | Production environments requiring automated load balancing. |
| ExternalName | Maps 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
defaultnamespace.
List existing namespaces:
kubectl get namespaces -o wide
Check the current default namespace:
kubectl config get-contexts
If the NAMESPACE column is empty, the namespace is default.
Create the namespace service-type-test:
kubectl create namespace service-type-test
Make it the default for your current context:
kubectl config set-context --current --namespace=service-type-test
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:
kubectl config view --minify --output 'jsonpath={..namespace}'Create a deployment YAML
cdmkdir nginx-deploymentvim nginx-deployment/nginx-single-node.yamlPaste the following. The nodeSelector pins the pod to worker node k8s-2:
apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-single namespace: service-type-test labels: app: nginxspec: 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: 80Apply the deployment
kubectl apply -f nginx-deployment/nginx-single-node.yamlkubectl get pods -o wideExample output:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-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.
kubectl exec -it nginx-single-7dfff5577-2v25s -- shInside the pod:
curl http://10.244.1.2
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.
kubectl run testpod --rm -it --image=rockylinux:9 -- bashInside testpod, install the networking tools:
dnf install -y iputils net-tools nc traceroute bind-utils iproute
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.
kubectl edit configmap -n kube-system coredns
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:
kubectl delete pod -n kube-system -l k8s-app=kube-dnskubectl get pods -n kube-system -l k8s-app=kube-dns
Run the dnf install command in testpod again. It should now succeed.
Troubleshooting: Pods on Different Nodes Can’t Talk
Check which nodes the pods run on:
kubectl get pods -n service-type-test -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-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.4PING 10.244.1.4 (10.244.1.4) 56(84) bytes of data.From 10.244.1.0 icmp_seq=1 Packet filteredFrom 10.244.1.0 icmp_seq=2 Packet filteredPacket filtered usually points to firewalld rules.
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 lo2: 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 ens1603: 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 ens2244: 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 docker05: 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.16: 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 cni07: 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:
| Interface | Purpose | Notes |
|---|---|---|
| lo | Loopback | Internal communication within the node. Always present. |
| ens160 | Primary network interface | Connected to vmnet2, a private network for our VMs (172.16.211.12/24). |
| ens224 | Secondary network interface | Connected to another network (172.16.68.135/24) managed by VMware Fusion, providing internet access. |
| docker0 | Docker bridge (not used by Kubernetes) | Created by Docker, not part of Kubernetes networking. Leftover from local development. |
| flannel.1 | Flannel overlay network | Handles pod-to-pod traffic across nodes (10.244.1.0/32). |
| cni0 | Main CNI bridge | Connects pods on this node to the Flannel overlay (10.244.1.1/24). |
| veth* | Virtual Ethernet pairs | Connect individual pods to the cni0 bridge. Created dynamically as pods start. |
Step 2: Check the firewalld zone
❯ sudo firewall-cmd --list-allpublic (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:
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
sudo firewall-cmd --set-log-denied=allsudo firewall-cmd --reloadThen 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=1IN=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:
sudo firewall-cmd --permanent --zone=public --add-interface=cni0sudo firewall-cmd --reloadsudo firewall-cmd --list-allDo this on every worker node. Turn denied-packet logging back off when you’re done:
sudo firewall-cmd --set-log-denied=offPing 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: v1kind: Servicemetadata: name: nginx-nodeport namespace: service-type-testspec: 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-32767kubectl apply -f nginx-deployment/nginx-nodeport.yamlkubectl get svc -o wide
The PORT(S) column shows 80:30080/TCP: the Service port and the node port.
NodePort: Nginx access methods
| From | Command |
|---|---|
| Outside the cluster (your laptop, another VM) | curl http://<any-node-ip>:30080, e.g. curl http://172.16.211.12:30080 |
| Any node | curl http://localhost:30080 or curl http://<node-ip>:30080 |
Inside a pod (testpod) via ClusterIP | curl http://<cluster-ip>:80 |
| Inside a pod via DNS | curl 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: v1kind: Servicemetadata: name: nginx-clusterip namespace: service-type-testspec: type: ClusterIP # default; can be omitted selector: app: nginx ports: - port: 80 targetPort: 80kubectl apply -f nginx-deployment/nginx-clusterip.yamlkubectl get svc -o wideTest it:
- From
testpod:curl http://nginx-clusterip(same namespace) orcurl 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.
NodePort vs ClusterIP: When to Use Which
| Question | ClusterIP | NodePort |
|---|---|---|
| Need access from outside the cluster? | No | Yes |
| Who calls it? | Other pods: frontends calling APIs, apps calling databases | People or systems outside the cluster |
| Port you use | The Service port (e.g. 80) | A high port (30000–32767) on every node |
| Firewall changes | None beyond pod networking | Open the NodePort range on nodes |
| Production fit | The standard for internal services | Fine 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:
kubectl delete namespace service-type-testkubectl config set-context --current --namespace=defaultFAQ
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.