Services and Ingress

Mid-level Always asked 7 min

On this page

The question

What is the difference between ClusterIP, NodePort and LoadBalancer Services?

30-second answer

Each builds on the previous one. ClusterIP, the default, gives a set of Pods a stable virtual IP and DNS name reachable only inside the cluster, for service-to-service calls. NodePort also opens the same port, from 30000 to 32767, on every node, so outside clients can reach the Service through any node's IP. LoadBalancer also asks the cloud provider for an external load balancer with a stable IP that forwards to those node ports, one per Service.

Understand it

From the internet to a Pod

Step by step

The browser asks shop.example.com for the path /api/orders. Public DNS (Domain Name System) resolves the name to the cloud load balancer in front of the cluster.

Step 1 of 6
View as text

A request travels down a chain: browser, cloud load balancer, ingress controller, the Service orders, then three Pods. Beside each hop, the address the request uses there. Two Pods are ready; the third is not.

The browser asks shop.example.com for the path /api/orders. Public DNS (Domain Name System) resolves the name to the cloud load balancer in front of the cluster.

The load balancer was created by a Service of type LoadBalancer, the ingress controller’s. It forwards the connection to one of the controller’s Pods.

The ingress controller, such as ingress-nginx, terminates TLS (Transport Layer Security) and matches an Ingress rule: host shop.example.com, path /api/orders, backend Service orders on port 80.

The Service is a stable virtual IP and DNS name. Its selector, app: orders, matches 3 Pods, but its EndpointSlice lists only the 2 that pass their readiness probe.

One ready Pod is picked and the request reaches its targetPort, 8080. The response goes back along the same path. The next request may land on another Pod.

The third Pod is running but not ready, so it is not an endpoint and receives nothing. It is not restarted either: readiness only controls traffic.

A request travels down a chain: browser, cloud load balancer, ingress controller, the Service orders, then three Pods. Beside each hop, the address the request uses there. Two Pods are ready; the third is not.

The browser asks shop.example.com for the path /api/orders. Public DNS (Domain Name System) resolves the name to the cloud load balancer in front of the cluster.

The load balancer was created by a Service of type LoadBalancer, the ingress controller’s. It forwards the connection to one of the controller’s Pods.

The ingress controller, such as ingress-nginx, terminates TLS (Transport Layer Security) and matches an Ingress rule: host shop.example.com, path /api/orders, backend Service orders on port 80.

The Service is a stable virtual IP and DNS name. Its selector, app: orders, matches 3 Pods, but its EndpointSlice lists only the 2 that pass their readiness probe.

One ready Pod is picked and the request reaches its targetPort, 8080. The response goes back along the same path. The next request may land on another Pod.

The third Pod is running but not ready, so it is not an endpoint and receives nothing. It is not restarted either: readiness only controls traffic.

Takeaway: DNS points at a cloud load balancer, which feeds the ingress controller. An Ingress rule picks a Service by host and path, and the Service forwards to one of its ready Pods.

ClusterIP, NodePort and LoadBalancer

Compare

ClusterIP, the default: a virtual IP and a DNS name reachable only from inside the cluster. Other Pods call http://orders and reach one of its ready Pods.

View as text

Three facts, from where, the address and what the type adds, above a cluster with two nodes, a Service and a Pod on each node, and the caller above them.

ClusterIP: From: inside the cluster only. Address: orders, 10.96.26.6:80. Adds: virtual IP and DNS name. ClusterIP, the default: a virtual IP and a DNS name reachable only from inside the cluster. Other Pods call http://orders and reach one of its ready Pods.

NodePort: From: any node IP, port 30201. Address: a node IP:30201. Adds: a port on every node. NodePort adds a port from 30000 to 32767 on every node, on top of the ClusterIP. Outside clients call any node on that port, so they must know node IPs.

LoadBalancer: From: the internet, one stable IP. Address: 203.0.113.10:80. Adds: a cloud load balancer. LoadBalancer also asks the cloud for a load balancer with a stable external IP, which forwards to the node ports. Simple, but one load balancer, and one bill, per Service.

Three facts, from where, the address and what the type adds, above a cluster with two nodes, a Service and a Pod on each node, and the caller above them.

ClusterIP: From: inside the cluster only. Address: orders, 10.96.26.6:80. Adds: virtual IP and DNS name. ClusterIP, the default: a virtual IP and a DNS name reachable only from inside the cluster. Other Pods call http://orders and reach one of its ready Pods.

NodePort: From: any node IP, port 30201. Address: a node IP:30201. Adds: a port on every node. NodePort adds a port from 30000 to 32767 on every node, on top of the ClusterIP. Outside clients call any node on that port, so they must know node IPs.

LoadBalancer: From: the internet, one stable IP. Address: 203.0.113.10:80. Adds: a cloud load balancer. LoadBalancer also asks the cloud for a load balancer with a stable external IP, which forwards to the node ports. Simple, but one load balancer, and one bill, per Service.

Takeaway: Each type builds on the one before. ClusterIP is a virtual IP inside the cluster; NodePort adds a port on every node; LoadBalancer adds a cloud load balancer in front of those ports.

Which way in, Service type or Ingress?If only other Pods in the cluster call the service, a ClusterIP Service is enough. If outside clients call it over HTTP, put an Ingress or a Gateway in front of a ClusterIP Service, so many services share one load balancer. For other protocols, such as raw TCP (Transmission Control Protocol), use a LoadBalancer Service. NodePort is mostly for local clusters or as the building block underneath.other Pods onlyoutside clientsyesno, raw TCPa local test clusterWho calls it?ClusterIPHTTP or HTTPS?Ingress or Gatewayto a ClusterIPServiceLoadBalancerNodePort
Which way in, Service type or Ingress?If only other Pods in the cluster call the service, a ClusterIP Service is enough. If outside clients call it over HTTP, put an Ingress or a Gateway in front of a ClusterIP Service, so many services share one load balancer. For other protocols, such as raw TCP (Transmission Control Protocol), use a LoadBalancer Service. NodePort is mostly for local clusters or as the building block underneath.other Pods onlyoutside clientsyesno, raw TCPa local test clusterWho calls it?ClusterIPHTTP or HTTPS?Ingress or Gatewayto a ClusterIPServiceLoadBalancerNodePort
Which way in: Service type or Ingress?

In code

apiVersion: v1
kind: Service
metadata:
  name: orders
spec:
  type: ClusterIP            # the default; NodePort or LoadBalancer to expose it
  selector:
    app: orders              # every ready Pod with this label is an endpoint
  ports:
    - port: 80               # what callers use: http://orders
      targetPort: 8080       # where the container listens
A Service selects Pods by label and maps its port to theirs
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
spec:
  ingressClassName: nginx
  tls:
    - hosts: [shop.example.com]
      secretName: shop-tls   # certificate and key, as a Secret
  rules:
    - host: shop.example.com
      http:
        paths:
          - path: /api/orders
            pathType: Prefix
            backend:
              service: { name: orders, port: { number: 80 } }
          - path: /
            pathType: Prefix
            backend:
              service: { name: web, port: { number: 80 } }
An Ingress routes by host and path to Services, and terminates TLS
kubectl get svc orders                    # ClusterIP 10.96.26.6, PORT(S) 80/TCP
kubectl get endpointslices -l kubernetes.io/service-name=orders
# orders-jqcmm   10.244.0.5,10.244.0.7,10.244.0.8     <- the ready Pods behind it
kubectl run tmp --rm -it --image=alpine:3.20 -- nslookup orders.default.svc.cluster.local
# Address: 10.96.26.6                     <- inside the namespace, plain "orders" works too
kubectl patch svc orders -p '{"spec":{"type":"LoadBalancer"}}'
kubectl get svc orders                    # EXTERNAL-IP <pending> without a cloud, 80:30201/TCP
See what a Service resolves to, and which Pods it really sends to
# Deployment labels its Pods app: orders-api, so this Service selects nothing
  selector:
 Wrong     app: orders

# Copy the selector from the Pod template's labels
 Right   selector:
    app: orders-api
A selector that matches no Pod gives a Service with no endpoints
kube-proxy turns a Service IP into a Pod IPThe EndpointSlice controller watches Pods matching the Service's selector and records the ready ones. kube-proxy on every node watches the Service and its EndpointSlices and writes iptables rules. When a Pod connects to the Service IP, the node's rules rewrite the destination to one ready Pod's IP and port, without any proxy process in the path.orders PodCaller Podkube-proxyAPI serverEndpointSlicesrewritten to 10.244.0.7:8080ready Pod IPswatch Services, sliceswrite iptables rulesconnect 10.96.26.6:80response
kube-proxy turns a Service IP into a Pod IPThe EndpointSlice controller watches Pods matching the Service's selector and records the ready ones. kube-proxy on every node watches the Service and its EndpointSlices and writes iptables rules. When a Pod connects to the Service IP, the node's rules rewrite the destination to one ready Pod's IP and port, without any proxy process in the path.orders PodCaller Podkube-proxyAPI serverEndpointSlicesrewritten to 10.244.0.7:8080ready Pod IPswatch Services, sliceswrite iptables rulesconnect 10.96.26.6:80response
kube-proxy turns a Service IP into a Pod IP

Solve it

The Ingress returns 503 for /api/orders

After a deploy, https://shop.example.com/api/orders answers 503 Service Temporarily Unavailable, while / still works. The orders Pods are Running, and their logs show no errors and no requests. Find where the request stops and fix it.

  1. Follow the path backward. The ingress controller answers, so DNS, the load balancer and TLS work. A 503 from ingress-nginx means its backend Service has no endpoint to send to; the Pods never see the request.
  2. Check the endpoints. kubectl get endpointslices -l kubernetes.io/service-name=orders lists no address. A Service only lists Pods that match its selector and pass their readiness probe.
  3. Compare the labels. The new Deployment labels its Pods app: orders-api, but the Service still selects app: orders. Fix the selector, or the labels, so they match; then check targetPort against the container port.
  4. Rule out readiness. If labels match but endpoints stay empty, the Pods are not Ready: kubectl describe pod shows the failing readiness probe, which is a different fix.
  5. Check. The EndpointSlice lists the Pod IPs, curl through the Ingress returns 200, and the Pods’ logs show the requests arriving.
kubectl get endpointslices -l kubernetes.io/service-name=orders    # ENDPOINTS <unset>
kubectl get pods --show-labels | grep orders                       # app=orders-api
kubectl get svc orders -o jsonpath='{.spec.selector}'              # {"app":"orders"}
kubectl patch svc orders -p '{"spec":{"selector":{"app":"orders-api"}}}'
curl -s -o /dev/null -w '%{http_code}\n' https://shop.example.com/api/orders   # 200
Find the missing endpoints, then align the selector with the Pods' labels

How to answer

  • All three select Pods by label and send only to ready ones.
  • port is what callers use, targetPort where the container listens.
  • For many HTTP services, an Ingress or Gateway shares one load balancer.
  • ExternalName maps a name to an outside DNS name, and headless Services, clusterIP None, return Pod IPs directly.

Common mistakes

  • Exposing every microservice with its own LoadBalancer.
  • Calling Pods by IP instead of through a Service.

Follow-up questions

What is an Ingress, and why does it need an Ingress controller?

An Ingress is a set of HTTP routing rules: for host shop.example.com, send /api/orders to the Service orders and / to web, with TLS from this Secret. The object does nothing alone. An Ingress controller, such as ingress-nginx, Traefik or a cloud provider's, is a set of Pods behind one LoadBalancer that watches Ingress objects and configures itself to apply them. The newer Gateway API splits the same job into GatewayClass, Gateway and HTTPRoute objects.

  • ingressClassName picks which controller handles an Ingress.
  • The controller usually sends traffic straight to Pod IPs from the EndpointSlices.
  • Annotations configure controller-specific behavior, such as timeouts and rewrites.

See it in the infographic: From the internet to a Pod

How does a request from the internet reach a Pod?

DNS resolves the public host name to a cloud load balancer, created by the ingress controller's LoadBalancer Service. The load balancer forwards to the ingress controller's Pods, usually through node ports. The controller terminates TLS, matches the Ingress rule by host and path and picks the backend Service. It sends the request to one of that Service's ready endpoints, a Pod IP and targetPort; inside the cluster, kube-proxy's rules do the same for calls to a Service IP.

  • Only Pods passing their readiness probe are endpoints.
  • Pod IPs are routable across nodes thanks to the CNI (Container Network Interface) plugin.
  • Each hop can fail independently: DNS, load balancer, Ingress rule, selector, readiness, port.

See it in the infographic: From the internet to a Pod

Pods already have IP addresses. Why do you need a Service?

Because Pod IPs are not stable. Pods are replaced on every deploy, crash or reschedule, each time with a new IP, and there are several of them. A Service gives the group one stable virtual IP and DNS name, keeps an up-to-date list of the ready Pods matching its selector, in EndpointSlices, and spreads connections across them. Callers depend on the name orders, never on which Pods exist right now.

  • The selector decides membership; readiness decides who receives traffic.
  • A Service balances connections, not requests, so long-lived connections can stick to one Pod.
  • Changing Pods never changes the Service's IP.

See it in the infographic: ClusterIP, NodePort and LoadBalancer

How does one service find another inside the cluster?

Through cluster DNS, CoreDNS. Every Service gets the name name.namespace.svc.cluster.local, and each Pod's resolver searches its own namespace first, so a Pod in shop calls http://orders, and a Pod in another namespace calls http://orders.shop. The name resolves to the Service's virtual IP, or for a headless Service to the Pod IPs. No registry such as Eureka is needed; Spring Boot just reads the URL from configuration.

  • Cross-namespace calls need the namespace in the name.
  • StatefulSet Pods get their own names, db-0.db.shop.svc.cluster.local.
  • Kubernetes also injects ORDERS_SERVICE_HOST variables, but only for Services that existed first.

See it in the infographic: ClusterIP, NodePort and LoadBalancer

Calls to a Service fail although its Pods are running. How do you debug it?

Work from the Service to the Pods. kubectl get endpointslices for the Service: if it lists no addresses, either the selector does not match the Pods' labels, compare with kubectl get pods --show-labels, or the Pods are not Ready, so read their readiness probe in kubectl describe pod. If addresses are listed, check that targetPort is the port the container listens on, then call a Pod IP directly from a debug Pod to split network problems from app problems.

  • Empty endpoints mean selector or readiness, almost always.
  • A wrong targetPort gives connection refused with endpoints present.
  • Network policies can block traffic that everything else allows.

See it in the infographic: From the internet to a Pod

Why not give every service its own LoadBalancer?

Each one is a separate cloud load balancer with its own IP and its own monthly cost, and each needs its own TLS (Transport Layer Security) certificate and DNS (Domain Name System) record. An Ingress or Gateway puts one load balancer in front of the cluster and routes by host and path to many ClusterIP Services, with TLS handled in one place.

Check yourself

1. payments must be callable by other services in the cluster, and never from outside. Which Service type fits?
Show answers
  • ClusterIP: Correct. It is reachable only inside the cluster, which is exactly the requirement.
  • NodePort: Incorrect. It opens a port on every node, reachable from outside the cluster.
  • LoadBalancer: Incorrect. It adds a public load balancer, the opposite of the requirement.
2. One of three orders Pods starts failing its readiness probe. What happens?
Show answers
  • It is removed from the Service's endpoints and gets no traffic, but keeps running: Correct. Readiness only decides who receives traffic.
  • Kubernetes restarts it: Incorrect. Restarts come from a failing liveness probe, not readiness.
  • It still gets a third of the requests: Incorrect. Only ready Pods are endpoints.
3. You need to expose 20 HTTP services under one domain, by path, with one TLS certificate. What do you use?
Show answers
  • One Ingress, or Gateway, in front of 20 ClusterIP Services: Correct. One controller and load balancer routes by host and path to every Service.
  • 20 LoadBalancer Services: Incorrect. That is 20 public IPs and 20 bills, with TLS configured 20 times.
  • 20 NodePort Services: Incorrect. Clients would need node IPs and 20 different ports.