Lesson 19 · Section 2: Core Concepts
Lesson 19: Services
Why Pods need a Service in front of them, how a Service finds its Pods and moves traffic to them, the Service types at a glance, and NodePort in depth: three ports, every node, and the commands that save time in the exam. · 25 min read · 5 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why Services exist
In the last lessons you created Pods, then let a ReplicaSet and a Deployment keep them running. That raises the next question: how does anything talk to those Pods?
Every Pod gets its own IP address, and Pods can reach each other on those addresses. But a Pod's IP is only good for as long as that one Pod lives. A Deployment keeps your application running precisely by replacing Pods: during every rollout, after every node failure, whenever a Pod is evicted. Each replacement comes up with a new IP address. Three things go wrong if clients use Pod IPs directly:
- The address a client stored is stale after the next rollout.
- With three replicas there are three addresses, and the client would have to choose between them and notice when one goes away.
- Pod IPs only mean something inside the cluster. A browser on your laptop cannot reach
10.244.1.7at all.
A Service solves all three. It is a small Kubernetes object that puts one DNS name and one virtual IP address in front of a group of Pods. The name and address stay the same for the Service's whole life, connections are spread over the Pods that are ready to take them, and, depending on its type, the Service can also open a way in from outside the cluster.
A Service is also what keeps the parts of an application loosely coupled. The frontend does not know how many
backend Pods exist, which nodes they run on, or that they were all replaced ten minutes ago. It knows one word:
backend.
What a Service is made of
Parts:
- Client Pod (frontend-7d9)
- CoreDNS (web = 10.96.0.20)
- Service web (10.96.0.20:80)
- EndpointSlice (3 ready addresses)
- web Pod (10.244.1.7:8080), in Node A
- web Pod (10.244.1.9:8080), in Node A
- web Pod (10.244.2.4:8080), in Node B
Connections:
- Client Pod to CoreDNS: web?
- Client Pod to Service web: web:80
- Service web to web Pod: :8080
- Service web to web Pod: :8080
- Service web to web Pod: :8080
- EndpointSlice to Service web: lists backends
Four pieces work together:
- The selector. A Service has a label selector, such as
app: web. Every Pod in the same namespace whose labels match is a candidate backend. The Service does not know or care which Deployment created the Pods: labels are the only link, exactly as they are between a ReplicaSet and its Pods. - The EndpointSlice. Kubernetes writes the current list of matching Pod IPs and ports into EndpointSlice objects. Each address is marked ready or not ready, and only ready addresses receive traffic. This list is the first thing to look at when a Service misbehaves.
- The virtual IP (ClusterIP). When you create the Service, the API server gives it an IP from the cluster's
Service range, for example
10.96.0.20. It does not change while the Service exists. - The DNS name. Cluster DNS (CoreDNS) answers
web.shop.svc.cluster.localwith that IP. From a Pod in the same namespace the short namewebis enough; from another namespace useweb.shop.
Here is the smallest useful Service. It has no type, so it is a ClusterIP Service:
apiVersion: v1
kind: Service
metadata:
name: web
namespace: shop
spec:
selector:
app: web # Pods with this label are the backends
ports:
- port: 80 # clients call web:80
targetPort: 8080 # the container listens on 8080
How it works underneath
Parts:
- Service web (selector app=web)
- Pods (labels, readiness)
- Slice controller (controller manager)
- EndpointSlice (ready Pod IPs)
- kube-proxy (on every node)
- Kernel rules (iptables / nftables)
Connections:
- Service web to Slice controller: watched
- Pods to Slice controller: watched
- Slice controller to EndpointSlice: writes
- EndpointSlice to kube-proxy: watched
- kube-proxy to Kernel rules: programs
The ClusterIP is virtual. No network interface has that address and no process listens on it, which is why
pinging a Service usually gets no answer. Two control loops make it work anyway:
- The EndpointSlice controller (part of the kube-controller-manager) watches Services and Pods. Whenever a Pod that matches the selector appears, becomes ready, stops being ready or is deleted, it rewrites the Service's EndpointSlices.
- kube-proxy, on every node, watches Services and EndpointSlices and programs that node's kernel with
iptables or nftables rules. The rules say: a new connection to
10.96.0.20:80is rewritten to one of these Pod addresses.
When a Pod sends a packet to 10.96.0.20:80, the kernel on the sending Pod's own node matches the rule, picks
one ready backend and rewrites the destination to that Pod's real address, for example 10.244.2.4:8080. Replies
are translated back on the way, so the client never sees the Pod address. There is no central load balancer box
that the traffic passes through, and so there is no single point that can fall over: every node does its own
forwarding.
This design explains several things you will meet in practice:
- A new Service works within a second or two on every node, because every kube-proxy sees the same change.
- A Pod that fails its readiness probe stops receiving traffic without being restarted: it is simply dropped from the list. That is the whole point of readiness.
- If kube-proxy is not running on a node, Pods on that node cannot reach any Service, while Pod-to-Pod traffic by IP still works. That pattern is a classic troubleshooting clue.
Under the hood · What happens to each connection
- The backend is chosen per connection, not per request. A client that keeps one connection open (HTTP keep-alive, gRPC, a database pool) keeps talking to the same Pod. Three replicas do not guarantee an even split.
- In iptables mode the backend is picked at random. There is no "least busy" choice: a Service is a simple spreader, not a smart load balancer.
sessionAffinity: ClientIPsends all connections from one client IP to the same Pod. The stickiness lasts three hours (10800 seconds) by default.- The rules live in the kernel, so restarting kube-proxy does not break running traffic: the rules stay in place until it writes new ones.
- kube-proxy has several modes on Linux.
iptablesis today's default,nftablesis its successor andipvsis deprecated. For the exam, knowing that the mode exists and that it is set in thekube-proxyConfigMap inkube-systemis enough.
Under the hood · Endpoints and EndpointSlices: two names for the same idea
Older material, and many exam-style questions, talk about the Endpoints object: one object per Service, with
the same name as the Service. It still exists, but it is deprecated since Kubernetes v1.33, and kubectl get endpoints now prints a warning. Its replacement, EndpointSlice, splits the list over several objects (so a
Service with thousands of Pods does not rewrite one huge object on every change) and carries more detail per
address.
EndpointSlices have generated names, so you find them by label:
kubectl get endpointslices -l kubernetes.io/service-name=web
The fastest check of all needs neither: kubectl describe svc web prints an Endpoints: line.
Under the hood · How a Pod finds a Service: DNS, and the older environment variables
DNS is the normal way. Every Pod's /etc/resolv.conf points at the cluster DNS Service and carries a search list
that starts with the Pod's own namespace. That is why web works inside namespace shop, web.shop works from
anywhere, and web.shop.svc.cluster.local is the full name.
There is also an older mechanism. When a Pod starts, the kubelet injects environment variables for every Service
that exists in the Pod's namespace at that moment, such as WEB_SERVICE_HOST=10.96.0.20 and
WEB_SERVICE_PORT=80. A Service created after the Pod started is missing from that Pod's environment, which is
one more reason to prefer DNS. You will see these variables when you run env in a container, so it is worth
knowing where they come from.
The Service types at a glance
Parts:
- User (outside the cluster)
- Load balancer (type: LoadBalancer)
- Every node (type: NodePort)
- Cluster IP (type: ClusterIP)
- Ready Pods (targetPort 8080)
- Pods in cluster (call web.shop)
Connections:
- User to Load balancer
- Load balancer to Every node
- Every node to Cluster IP
- Cluster IP to Ready Pods
- Pods in cluster to Cluster IP
The type field decides from where the Service can be reached. It does not change how Pods are selected.
| Type | Reachable from | What Kubernetes adds |
|---|---|---|
ClusterIP (default) | Inside the cluster | A virtual IP and a DNS name |
NodePort | Anything that can reach a node's IP | ClusterIP, plus the same port opened on every node |
LoadBalancer | Outside, through one external address | NodePort, plus a load balancer from the cloud provider |
ExternalName | Inside the cluster | Only a DNS alias (CNAME) to another name; no selector, no proxying |
The first three stack like building blocks. A NodePort Service still has a cluster IP and can be called from inside the cluster exactly like a ClusterIP Service; a LoadBalancer Service still has a node port. So everything in this lesson about selectors, endpoints and ports applies to all of them.
ClusterIP and LoadBalancer each have their own lesson next. The rest of this one goes deep on NodePort, the simplest way to reach an application from outside the cluster and the one you can always use on an exam cluster.
NodePort in depth
Three ports, read from the outside in
Parts:
- Outside client (calls node IP)
- Any node (nodePort 30080)
- Service web (port 80)
- web Pod (targetPort 8080)
- Pod in cluster (calls web:80)
Connections:
- Outside client to Any node: :30080
- Any node to Service web: :80
- Service web to web Pod: :8080
- Pod in cluster to Service web: :80
A NodePort Service has three port numbers. Most Service mistakes are one of these in the wrong place:
| Field | Where it lives | Meaning | If you leave it out |
|---|---|---|---|
nodePort | On every node | The port outside clients call, on a node's IP | A free port from 30000 to 32767 is picked |
port | On the Service's virtual IP | The port clients inside the cluster call | Required |
targetPort | On the Pod | The port the container really listens on | Same as port |
apiVersion: v1
kind: Service
metadata:
name: web
namespace: shop
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 8080
nodePort: 30080
With this Service, all of these reach the same Pods:
http://192.168.1.11:30080from your laptop, where192.168.1.11is any node's IP,http://web:80from a Pod in namespaceshop,http://10.96.0.20:80from any Pod or node in the cluster.
Only port is required. That is a trap in itself: if you write only port: 80 and the container listens on 8080,
targetPort silently becomes 80 and every connection is refused.
Every node answers
Parts:
- Outside client (curl node:30080)
- Node A rules (:30080 open), in Node A
- Node B rules (:30080 open), in Node B
- web Pod (10.244.1.7:8080), in Node A
- web Pod (10.244.1.9:8080), in Node A
- Other Pods (no web Pod here), in Node B
Connections:
- Outside client to Node A rules: 192.168.1.11
- Outside client to Node B rules: 192.168.1.12
- Node A rules to web Pod: local
- Node B rules to web Pod: second hop
The node port is opened on every node in the cluster, control plane nodes included, and not only on the nodes that happen to run one of the Pods. A node without a local Pod forwards the connection to a Pod on another node. That is convenient (any node IP works, and Pods can move freely), and it has two consequences worth knowing:
- The Pod does not see the real client address. To make sure replies travel back the same way, the receiving node replaces the packet's source address with its own. Your application logs show node IPs.
- There may be an extra hop between the node the client called and the node that runs the Pod.
Both are controlled by one field. externalTrafficPolicy: Local tells each node to use only its own Pods:
the client address is preserved and there is no second hop, but a node that runs no Pod of the Service no longer
answers on the node port at all. The default, Cluster, is what the diagram shows.
Where the port number comes from
- The range 30000 to 32767 is a setting of the API server (
--service-node-port-range). On a kubeadm cluster you can read it, or change it, in/etc/kubernetes/manifests/kube-apiserver.yaml; if the flag is absent the default applies. - A node port is unique in the whole cluster, across all namespaces, because it is opened on every node. Asking
for a port that another Service already holds fails with
provided port is already allocated. - Asking for a port outside the range fails with
provided port is not in the valid range. That is why you cannot put a NodePort Service on port 80 or 443 of a default cluster. - When you do not ask, Kubernetes prefers to pick from the upper part of the range and leaves the lowest ports (30000 to 30085 with the default range) for people who choose a number by hand. Choosing a fixed port in that low band makes collisions with automatic picks unlikely.
Under the hood · Why NodePort exists at all, and when to use it
NodePort is the one way in that needs nothing outside Kubernetes: no cloud API, no extra controller. That makes it the foundation the other entry points stand on. A cloud load balancer created for a LoadBalancer Service usually sends its traffic to the node port, and many ingress controllers on bare-metal clusters are themselves exposed through one.
Used directly, it fits demos, development clusters, lab and exam clusters, and setups where you run your own load balancer in front of the nodes. It fits poorly as the public face of a production application: clients would need to know node IPs, node IPs change, the port is an odd high number, and one Service takes one port on every node.
Using it
Example · Expose a Deployment on a node port and prove it works
The Deployment web in namespace shop runs Pods labelled app: web that listen on port 8080. Make it reachable
from outside the cluster, then check every link in the chain.
kubectl -n shop expose deployment web --type=NodePort --port=80 --target-port=8080
kubectl -n shop get svc web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.96.0.20 <none> 80:31742/TCP 4s
Read 80:31742/TCP as port:nodePort. The target port is not in this output; kubectl describe svc web shows
all three. Now check that the Service found its Pods, then call it from outside through any node:
kubectl -n shop get endpointslices -l kubernetes.io/service-name=web
kubectl get nodes -o wide # the INTERNAL-IP column
curl -s http://192.168.1.11:31742
kubectl expose copies the Deployment's selector into the Service, so the selector cannot be wrong. If you leave
out --port, it uses the containerPort declared in the Pod template, and fails if there is none.
Example · Ask for a specific node port
The task says "reachable on port 30080 of every node". kubectl expose has no flag for the node port, so generate
the manifest, add one line and apply it:
kubectl -n shop expose deployment web --type=NodePort --port=80 --target-port=8080 \
--dry-run=client -o yaml > web-svc.yaml
vim web-svc.yaml # under ports: add nodePort: 30080
kubectl apply -f web-svc.yaml
The Service already exists? Change it in place. Both of these work on a live Service, and you can turn a ClusterIP Service into a NodePort Service the same way without recreating it:
kubectl -n shop edit svc web
kubectl -n shop patch svc web -p '{"spec":{"type":"NodePort","ports":[{"port":80,"nodePort":30080}]}}'
Example · Find out why a Service does not answer
curl http://192.168.1.11:30080 fails. Do not guess: walk the chain from the Service to the Pod.
kubectl -n shop describe svc web # Selector, TargetPort, NodePort and Endpoints in one place
Selector: app=wbe
Type: NodePort
Port: <unset> 80/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30080/TCP
Endpoints: <none>
Endpoints: <none> says no ready Pod matches. Compare the selector with the real labels:
kubectl -n shop get pods --show-labels
kubectl -n shop get pods -l app=web # the selector you expected: does it find the Pods?
Here the selector has a typo. Fix it with kubectl -n shop edit svc web, or in one line with
kubectl -n shop set selector svc web app=web, and the Endpoints line fills in within a second.
- Endpoints listed?
- Selector equals Pod labels?
- Pods Ready?
- targetPort is the port the app listens on?
- Right node IP and nodePort?
Tricks
Common mistakes
- Selector does not match.
app: Webandapp: webare different labels, and so areapp: webandname: web. The Service is created without complaint and simply has no endpoints. - Swapping
portandtargetPort. Clients callport; the container listens ontargetPort. A Service that has endpoints but refuses connections almost always has the wrongtargetPort. - Calling the wrong number from outside. From outside the cluster only the node port works.
curl node:80fails for a Service withport: 80; you need the number after the colon in80:31742. - Calling the cluster IP from your laptop. The cluster IP and the Service's DNS name exist only inside the cluster.
- A Service in another namespace than its Pods. A selector only sees Pods in the Service's own namespace.
Forgetting
-ncreates a perfectly valid Service indefaultwith no endpoints. - Pods that are not ready. A Pod that fails its readiness probe is removed from the endpoints, so a broken
probe looks exactly like a broken selector. The READY column of
kubectl get podstells them apart. - A selector that matches too much. If two Deployments share the label you select on, the Service mixes both sets of Pods. Select on enough labels to be specific.
- Several ports without names. A Service with more than one entry under
portsmust give each aname.
Recap
- A Service gives a changing set of Pods one stable DNS name and virtual IP; its selector picks the Pods and the EndpointSlice lists the ready ones.
- No process sits in the traffic path: kube-proxy writes kernel rules on every node, and the kernel forwards each connection to one Pod.
ClusterIP→NodePort→LoadBalancereach add a way in on top of the previous type.- NodePort opens the same port (30000 to 32767) on every node:
nodePort→port→targetPort. - In the exam:
kubectl expose,--dry-run=client -o yaml, and when something fails, look at the endpoints first.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Put two Deployments behind NodePort Services: one on a fixed port, one on a port the cluster picks.
Build · Level 2 · standard · target 15 min
Section capstone. Take over a half-finished namespace: get its Deployment running under a Pod limit, repair its Service, open it on a node port, add a Pod that calls another namespace, and write it all down as a manifest.
Fix · Level 3 · challenge · target 30 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.