Skip to content
Visual explainer 3h to learn 4 steps

Services: ClusterIP, NodePort, LoadBalancer

Expose workloads and debug Services with no endpoints.

By Invicly Tech, , in Kubernetes Administrator (CKA). About 10 minutes to read.

Topic 19 of 33

In short

A Service gives a stable virtual IP and DNS name to a changing set of Pods chosen by a label selector. ClusterIP is internal only, NodePort opens a port on every node, and LoadBalancer asks the cloud for an external load balancer. kube-proxy programs node rules that forward Service traffic to ready endpoints.

Explained simply

Pods come and go, and each new Pod gets a new IP. If a web front end had to track every backend Pod IP, it would break constantly. A Service fixes this by giving a group of Pods one stable name and one stable IP.

The Service finds its Pods with a label selector, such as app=api. Any Ready Pod with that label becomes a backend, called an endpoint. Requests to the Service are spread across those endpoints.

There are three main types. ClusterIP is only reachable inside the cluster and is the default. NodePort also opens the same port on every node, so you can reach the app from outside. LoadBalancer asks a cloud provider for a real load balancer with a public address.

The magic is done by kube-proxy, a program on every node. It does not pass traffic itself; it writes kernel rules that rewrite the Service IP to a real Pod IP. That is why you cannot ping a Service IP, but you can connect to its port.

Press Play on a diagram to walk through it one step at a time.

CompareService types
ClusterIP
  • Default type
  • Internal virtual IP
  • Reached via DNS name
  • Pod-to-Pod traffic
NodePort
  • Builds on ClusterIP
  • Opens 30000-32767 on every node
  • NodeIP:nodePort from outside
  • Simple external testing
LoadBalancer
  • Builds on NodePort
  • Cloud provisions external LB
  • Public IP or hostname
  • Needs cloud or MetalLB
FlowTraffic to a ClusterIP
  1. 1
    Client Pod

    Calls web.default.svc:80.

  2. 2
    Service IP

    Virtual IP from the Service CIDR.

  3. 3
    kube-proxy rules

    iptables, IPVS or nftables pick an endpoint.

  4. 4
    EndpointSlice

    Ready Pod IPs matching the selector.

  5. 5
    Pod targetPort

    Container receives the request.

LayersEach Service type builds on the one below
  1. 1
    LoadBalancer

    External LB sends to node ports

  2. 2
    NodePort

    Port 30000-32767 open on every node

  3. 3
    ClusterIP

    Virtual IP and DNS name inside the cluster

  4. 4
    EndpointSlices

    Ready Pod IPs from the selector

  5. 5
    Pods

    Receive traffic on targetPort

In depth

Three ports that people mix up

port is the port on the Service IP that clients call. targetPort is the port on the container that receives the traffic; if you leave it out, it equals port. nodePort is only for NodePort and LoadBalancer Services and is opened on every node, chosen from 30000 to 32767 unless you set one in that range.

targetPort may also be a name, such as http, that matches a named containerPort in the Pod spec. That lets different Pods use different numbers behind one Service and survives port changes in the image. The containerPort field itself is informational; a process listening on 8080 receives traffic whether you declared it or not.

  • kubectl expose deploy web --port=80 --target-port=8080
  • kubectl create service nodeport web --tcp=80:8080 --node-port=30080
  • kubectl get svc web -o yaml shows all three ports

What kube-proxy really programs

kube-proxy watches Services and EndpointSlices and writes rules in the kernel. In the default iptables mode, a packet to the Service IP hits a KUBE-SERVICES rule, jumps to a per-Service chain, and is randomly sent to one per-endpoint chain that rewrites the destination (DNAT) to a Pod IP and port. The nftables mode, stable since Kubernetes 1.33, does the same with nftables and scales better. IPVS mode uses the kernel's load balancer.

Because only ports are translated, ICMP ping to a ClusterIP usually gets no answer. Test Services with wget, curl or nc instead. sessionAffinity: ClientIP sends one client to the same Pod (default timeout 3 hours). externalTrafficPolicy: Local keeps the client's real source IP for NodePort and LoadBalancer traffic, but only nodes running a Ready endpoint will answer.

FlowA packet to 10.96.40.12:80 in iptables mode
  1. 1
    Client Pod sends

    Destination 10.96.40.12:80

  2. 2
    KUBE-SERVICES

    Matches the Service IP and port

  3. 3
    KUBE-SVC chain

    Picks an endpoint at random

  4. 4
    KUBE-SEP chain

    DNAT to 10.244.2.7:8080

  5. 5
    Normal Pod routing

    CNI delivers to the Pod

EndpointSlices and readiness

The EndpointSlice controller looks for Pods matching the selector in the Service's namespace and lists their IPs, ports and readiness. Each slice holds up to 100 endpoints by default and carries the label kubernetes.io/service-name. The older Endpoints API still works but is deprecated since 1.33, so prefer EndpointSlices.

A Pod that is not Ready stays in the slice marked ready false, and kube-proxy does not send it traffic. So an empty Service has three common causes: the selector matches no Pods (a label typo, or the Pods are in another namespace), the Pods exist but fail their readiness probe, or the targetPort does not match what the app listens on, which shows up as connection refused rather than an empty list.

  • kubectl get endpointslices -l kubernetes.io/service-name=web -o wide
  • kubectl get pods -l app=web -o wide shows whether the selector finds Pods
  • kubectl describe svc web shows Selector and Endpoints together

Special Services

A headless Service (clusterIP: None) has no virtual IP and no kube-proxy rules; DNS returns the Pod IPs directly. StatefulSets use it so each Pod gets a stable name like db-0.db.prod.svc.cluster.local.

A Service without a selector gets no automatic endpoints. You add an EndpointSlice by hand, which is a way to give an outside database a cluster-internal name. type ExternalName returns a DNS CNAME to another hostname and involves no proxying at all. A LoadBalancer Service on a cluster without a cloud controller shows EXTERNAL-IP pending forever; in labs use NodePort, or install something like MetalLB.

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Expose Pods with ClusterIP~45m
  2. 2
    Expose with NodePort and LoadBalancer~45m
  3. 3
    Inspect endpoints and EndpointSlices~45m
  4. 4
    Fix Services with no endpoints~45m

Key points

  • A Service with no endpoints usually has a selector that matches no labels.
  • port is the Service port; targetPort is the container port.
  • Only Ready Pods appear as endpoints, so failing readiness empties the Service.
  • Headless Services (clusterIP: None) return Pod IPs directly via DNS.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
kubectl expose deployment web --port=80 --target-port=8080
kubectl expose deployment web --name=web-np \
  --type=NodePort --port=80 --target-port=8080
kubectl get svc web -o wide
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl get pods --show-labels   # compare with selector

A Service with no endpoints

Users get errors from web, and kubectl describe svc web shows an empty Endpoints line. Step 1: read the Service selector. Step 2: list Pods with that exact selector; nothing comes back. Step 3: show Pod labels; the Pods say app=web-app. Step 4: fix the selector to match. Step 5: the EndpointSlice fills within seconds and requests work.

bash
kubectl get svc web -o jsonpath='{.spec.selector}'; echo   # {"app":"web"}
kubectl get pods -l app=web                                 # No resources found
kubectl get pods --show-labels | grep web                   # app=web-app
kubectl patch svc web -p '{"spec":{"selector":{"app":"web-app"}}}'
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl run t --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- -T 3 http://web

Expose an app on a fixed NodePort

A task says: expose Deployment shop on port 30080 of every node, forwarding to container port 8080. Step 1: generate the Service YAML with expose and dry-run. Step 2: add nodePort: 30080 under ports. Step 3: apply and test from outside with a node IP.

bash
kubectl expose deploy shop --name=shop-np --type=NodePort --port=80 \
  --target-port=8080 --dry-run=client -o yaml > np.yaml
# edit ports:  - { port: 80, targetPort: 8080, nodePort: 30080 }
kubectl apply -f np.yaml
kubectl get svc shop-np        # 80:30080/TCP
curl -s http://NODE_IP:30080/ | head -3

Quick reference

  • port = Service port, targetPort = container port, nodePort = 30000-32767 on every node
  • kubectl expose pod web --port=80 --name=web-svc creates a ClusterIP Service
  • kubectl create svc clusterip db --tcp=5432:5432 sets selector app=db
  • Do not ping a ClusterIP; test with wget, curl or nc to the port
  • EndpointSlice label: kubernetes.io/service-name=NAME
  • externalTrafficPolicy: Local keeps the client IP; only nodes with endpoints answer
  • kubectl port-forward svc/web 8080:80 tests a Service from your machine

Common mistakes

  • Mismatched selector labels, leaving the Service with zero endpoints.
  • Setting targetPort to the Service port instead of the container port.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Pods are Running and labels match the Service selector, but the Service has no ready endpoints. Most likely cause?

Question 2

2A Service has port 80 and no targetPort. Which container port receives traffic?

Question 3

3ping 10.96.12.5 (a ClusterIP) fails, but wget to its port works. What does this tell you?

Question 4

4In a lab without a cloud provider, a LoadBalancer Service shows EXTERNAL-IP pending. What still works?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create Deployment echo (image nginx:1.27, 2 replicas) and a ClusterIP Service echo on port 8000 that targets port 80, then call it from a temporary Pod.

  2. Exercise 2medium

    Give the echo Pods a named port http and change the Service to use targetPort: http. Prove traffic still flows.

  3. Exercise 3hard

    On a node, find the iptables rules kube-proxy created for Service echo and name the Pod IPs it can DNAT to.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between ClusterIP, NodePort and LoadBalancer?
ClusterIP is reachable only inside the cluster. NodePort adds a port on every node for outside access. LoadBalancer adds a cloud load balancer in front of those node ports.
Why does my Service have no endpoints?
The selector does not match any Pod labels in that namespace, or the matching Pods are not Ready. Compare kubectl get svc -o wide with kubectl get pods --show-labels.
Do I need a Service for Pods to talk to each other?
No, Pods can use each other's IPs directly. But Pod IPs change, so a Service gives a stable name and spreads load, which is what you want in practice.

Go deeper

ShareXLinkedInWhatsApp