Five ways to reach a service in Kubernetes, and the one you should use

2025/09/07
kubernetesnetworkingdevops

To check whether an API inside a cluster is reachable, a common move is to kubectl exec into a pod and hit it with wget or curl.

A pod gets a new IP whenever it is recreated. So that callers don't have to chase IPs, Kubernetes puts a service in front of a set of pods. A service has a stable DNS name and virtual IP; a NodePort service also opens a port on every node. That gives the same API several addresses to hit, tried below in this order:

  1. From inside a node, localhost plus the node port
  2. From a pod, the service's DNS name
  3. From a pod, the service's virtual IP
  4. From a pod, a node's IP plus the node port
  5. Skipping the service, a pod's IP directly

The setup

I'm on Kind, which runs each K8s node as a Docker container. A pod is one or more containers sharing one IP. Two tiny Node.js APIs run in pods in the noah namespace (a name that groups resources), each behind a service:

  • joke-service → type: ClusterIP
  • number-service → type: NodePort

A ClusterIP service gets a virtual IP that only answers inside the cluster. A NodePort service gets one too, plus a port (30000–32767) opened on every node.

Apart from the names, the two YAML files differ in just the type line, yet number-service (NodePort) can be reached all five ways below and joke-service (ClusterIP) only three. Going from joke-service to number-service:

 apiVersion: v1
 kind: Service
 metadata:
-  name: joke-service
+  name: number-service
   namespace: noah
 spec:
   selector:
-    app: joke-service
+    app: number-service
   ports:
     - port: 80
       targetPort: 3000
-  type: ClusterIP
+  type: NodePort
$ kubectl get svc -n noah
NAME             TYPE        CLUSTER-IP      PORT(S)
joke-service     ClusterIP   10.96.236.182   80/TCP
number-service   NodePort    10.96.183.131   80:30405/TCP

One detail that explains the commands below: the Alpine pods only ship wget, while Kind's Debian nodes have curl. Same request, different tool.

Rerun in November 2026 on Kind v0.30.0 (Kubernetes v1.34.0) with the same results; the IPs and ports below are from the original run.

Hitting a NodePort service (all five work)

A NodePort service is the permissive one. Every method is on the table.

1. Docker exec into the node (a Kind-only trick). Kind nodes are containers, so you can jump inside one and hit localhost:

docker exec -it kubecmds-test-control-plane curl http://localhost:30405/health

30405 is the node port from kubectl get svc. Inside the node, the NodePort is just a local port. On a real cluster you SSH into the node (or run kubectl debug node/<name>) and hit 127.0.0.1:30405. Avoid localhost: an Alpine kubectl debug pod resolves it to IPv6 ::1, which is refused because kube-proxy (the component on each node that turns service addresses into packet rules) never serves NodePorts there. Even 127.0.0.1 works only in kube-proxy's iptables mode, Kind's default; in its other modes the docs point to the node's own IP (untested here). Methods 2 to 5 use nothing Kind-specific, but I ran them only on Kind.

2. Service DNS (the one you should use). Name the service, let K8s resolve it:

kubectl exec -it number-service-xxx -n noah -- wget -qO- http://number-service:80/health

Two caveats worth knowing:

  • From another namespace, add the namespace: number-service.noah (or the full number-service.noah.svc.cluster.local). The bare name fails with bad address, because DNS only fills in the pod's own namespace.
  • If CoreDNS (the cluster's DNS server, running as pods) is down, this is the method that stops working. The IP-based ones below still do.

3. ClusterIP direct. Skip DNS, use the service's virtual IP:

kubectl exec -it <pod> -n noah -- wget -qO- http://10.96.183.131:80/health

Reliable, but you have to look the IP up first (kubectl get svc).

4. NodeIP + NodePort (the painful one). Reach the node's real IP on the node port:

kubectl exec -it number-service-xxx -n noah -- wget -qO- http://172.18.0.3:30405/health

You need two lookups to build this: kubectl get nodes -o wide for the node IP, then kubectl get svc for the random port. Rarely worth it.

5. Direct pod IP (bypass the service entirely). Talk to one pod on its container port:

kubectl exec -it <pod> -n noah -- wget -qO- http://10.244.1.18:3000/health

Find the pod IPs with kubectl get pods -o wide, or read them off the service's endpoints:

$ kubectl describe svc number-service -n noah
Endpoints: 10.244.1.18:3000,10.244.2.23:3000

This skips load balancing completely, which is exactly why it's useful: it lets you test one misbehaving pod in isolation.

A ClusterIP service loses two of the five

Switch joke-service to type: ClusterIP and every method that needs a node port vanishes. Here are all five, side by side:

#MethodNodePortClusterIP
1Docker exec → localhost:NodePort✅❌ no node port
2Service DNS✅✅
3ClusterIP direct✅✅
4NodeIP + NodePort✅❌ no node port
5Direct pod IP✅✅

ClusterIP keeps 2, 3, and 5, and loses 1 and 4, the two that reach in through a node port it doesn't have.

The takeaway

In practice, use service DNS (service-name:port). It reads cleanly, and the naming comes from the Kubernetes DNS spec, not from Kind.

The reason: every address under the name can change, and the name doesn't. A pod replaced by a rollout (deploying a new version) or a deletion gets a new IP; only a container restart inside the same pod keeps it. The ClusterIP and node port last as long as the Service. This YAML sets neither spec.clusterIP nor nodePort, so recreating the Service gets new ones. In the rerun, 10.96.54.49 and 31738 became 10.96.209.100 and 30236, but number-service kept working. The other four methods all reach through to one of those addresses, which makes them perfect for debugging a specific layer (is DNS the problem? is one pod the problem?) and a bad idea for anything you actually ship.