A Pod is the smallest unit you can deploy in Kubernetes, but it is also the least reliable address in the whole system. Pods are created, destroyed, rescheduled and replaced all the time. A Deployment can roll out a new version and every Pod behind it gets a new IP address. If another part of your application hard-codes a Pod IP, that connection breaks the moment the Pod is replaced.
A Kubernetes Service solves that problem. It gives a set of Pods a single, stable identity: one virtual IP address that never changes while the Service exists, and one DNS name that every other workload in the cluster can use. Traffic sent to the Service is forwarded to one of the healthy Pods behind it, even as individual Pods come and go.
Why Pods Need Services
Kubernetes treats Pods as disposable. Controllers such as Deployments create and destroy Pods to keep the desired number running, to roll out new images, and to recover from failures. The system assumes an individual Pod can disappear at any moment and the application keeps working. But that only works if nothing depends on a single Pod: the moment one component calls another by Pod IP, you have built a fragile dependency on something designed to be temporary.
A Service breaks that dependency by sitting in front of a group of Pods and presenting one stable endpoint. Other workloads connect to the Service, not to the Pods. The Service keeps a list of the healthy Pods that match it and forwards each connection to one of them. When a Pod dies, the Service removes it; when a new Pod starts, the Service adds it. The caller never notices, because the Service IP and DNS name never change.
Services also describe the boundary between parts of your application. A frontend Service fronts the web Pods, a backend Service fronts the API Pods, and a database Service fronts the database Pods. Each component only needs to know the Service name, not how many replicas run behind it. It also provides service discovery: when you create a Service called api, every Pod in the cluster can resolve api to the Service IP. If you are still getting comfortable with the building blocks, start with a Kubernetes tutorial for beginners before going deeper here.
How Selectors and Endpoints Work
A Service does not contain Pods. It selects them. Every Service definition carries a selector, a set of label key-value pairs, and Kubernetes continuously matches that selector against the labels on Pods in the same namespace. Any Pod whose labels match becomes part of the Service; any Pod whose labels do not match is ignored. Use labels that describe the role of a Pod, such as app: checkout-api, and keep them consistent between your Deployment template and your Service selector. The most common beginner mistake is a selector that matches nothing, usually a typo or a mismatch such as app: checkout in the Deployment and app: checkout-api in the Service. The Service is created happily and traffic sent to it goes nowhere, so always check that a Service actually has endpoints.
The list of matching Pods is stored in an object called an EndpointSlice, which the endpoint controller maintains automatically. When a Pod that matches the selector becomes ready, its IP and port are added to the slice. When a Pod stops being ready — because its readiness probe fails, because it is terminating, or because it was deleted — its address is removed. The Service itself never changes: the stable front door and the changing backends are separate objects. Readiness matters here, because a Pod that is running but not ready receives no traffic.
Two kubectl commands show the Service and its endpoints:
kubectl get svc web
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl describe svc web
ClusterIP: The Default Service Type
ClusterIP is the default Service type: a Service with no type field is a ClusterIP Service. It gets a single virtual IP address from the cluster Service range, reachable only from inside the cluster. Pods can connect to it, and so can other Services, but nothing outside the cluster can reach it directly. That limitation is also its strength: ClusterIP is the right choice for anything that should never be exposed to the outside world, which is most components in a typical application — an API only the frontend calls, a cache only the application reads, a database only the backend writes to.
Here is a complete ClusterIP Service for a web application. It selects Pods labelled app: web, listens on port 80, and forwards to port 8080 on the Pods, because the container listens on 8080. The type field is shown for clarity, but you could leave it out and get the same result:
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: web
spec:
type: ClusterIP
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080
protocol: TCP
Apply it with kubectl apply -f web-service.yaml. The output shows the cluster IP that was assigned, and that address stays attached to this Service until the Service is deleted. Inside the cluster, the DNS name is simply web from any Pod in the same namespace, and web.default.svc.cluster.local from anywhere, assuming the namespace is default.
NodePort: Exposing a Service on Every Node
NodePort builds on ClusterIP. A NodePort Service gets a cluster IP, exactly like a ClusterIP Service, and it also opens the same port on every node in the cluster. Traffic that arrives at any node on that port is forwarded to the Service, and from there to one of the Pods behind it. The node port is the same on every node, so you can reach the Service through any node address — useful for testing, for on-premises clusters without a cloud load balancer, and where an external load balancer needs a fixed port to forward to.
The node port comes from a fixed range: 30000 to 32767 by default. That range is a cluster-wide setting, and every NodePort Service takes one port from it. If you do not specify a node port, Kubernetes allocates one for you. If you do specify one, it must be inside the range and not already in use by another Service, or the Service will be rejected.
Here is a NodePort Service for the same web application. The only differences from the ClusterIP version are the type and the nodePort field. Port 80 is still the Service port inside the cluster, and traffic still reaches the Pods on port 8080:
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: web
spec:
type: NodePort
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080
nodePort: 30080
protocol: TCP
Once this Service is applied, you can reach the application from outside the cluster by connecting to any node IP on port 30080, for example http://192.168.1.10:30080 if that is a node address. The node does not need to be running one of the web Pods: kube-proxy on every node forwards NodePort traffic to the right Pods, even if those Pods live on a different node. That is convenient, and it is also why NodePort is a poor choice as a permanent public endpoint: you would be advertising a node address, which changes when nodes are replaced, and a high port number, which no user wants to type. NodePort also has no built-in authentication, no hostname routing, and no TLS termination, and the port is open on every node. That is why NodePort is best treated as a building block and a debugging tool rather than a finished solution. It is how LoadBalancer Services are implemented underneath, and it is how many Ingress controllers receive traffic. On a local cluster such as minikube or kind, it is the quickest way to reach a Service from your laptop. To test one, find a node address and the allocated port, then make a request. If it works from one node but not another, the problem is usually node networking or a firewall rule; if it works from nowhere, check the endpoints, then the selector, then whether the Pods answer on their target port.
kubectl get svc web
kubectl get nodes -o wide
curl http://NODE-IP:30080/
LoadBalancer: A Cloud Load Balancer in Front of Your Service
LoadBalancer is the type you use when you want a real, public entry point to a Service on a cloud provider. When you create a LoadBalancer Service, Kubernetes asks the cloud provider — Amazon Web Services, Google Cloud, Microsoft Azure, or another supported provider — to provision an external load balancer, give it a public IP address or hostname, and forward traffic from that address to the Service. The load balancer is managed by the cloud controller manager and appears in your cloud account as a billable resource: every LoadBalancer Service creates real cloud infrastructure, and it keeps costing money until you delete the Service.
The YAML is almost identical to the NodePort version, with the type changed and the nodePort field left out so the cloud provider can allocate it:
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: web
spec:
type: LoadBalancer
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080
protocol: TCP
There are a few behaviours of LoadBalancer Services that catch people out. The first is cost. Each Service creates its own load balancer by default, so ten public Services mean ten load balancers on your cloud bill. That is why production clusters usually expose one or two LoadBalancer Services — often one for the Ingress controller — and route everything else through Ingress or a Gateway. The second is that the load balancer forwards traffic to nodes, not directly to Pods, unless the Service uses external traffic policy Local, which preserves the client IP but only sends traffic to nodes that actually run a Pod for the Service. The default policy, Cluster, spreads traffic across all nodes and hides the original client IP. The third behaviour is about health checks: the cloud load balancer checks node health, not Pod health, so readiness probes still matter, because kube-proxy removes unready Pods from the endpoints. On clusters without a cloud provider, a LoadBalancer Service will stay pending forever, because there is nothing to provision the load balancer; in those environments the usual answer is MetalLB, or an Ingress controller behind a NodePort Service. For the full picture, the official Kubernetes Services documentation is the authoritative reference, covering the Service types, the traffic policies and the cloud provider behaviour in detail.
Port, TargetPort and NodePort: The Three Numbers That Confuse Everyone
The first number is port: the port the Service itself listens on inside the cluster, and the port other Pods use when they call the Service by name, for example http://web:80 when the Service port is 80. It does not have to match anything the Pod listens on: it is a stable interface you choose for the consumers of the Service, and you can pick a conventional value such as 80 for HTTP, even when the container listens on something else. The second number is targetPort: the port the Pod actually listens on. Traffic that arrives at the Service on port is forwarded to the Pod on targetPort. If your container listens on 8080, the targetPort is 8080. The targetPort can be a number, or the name of a port defined in the Pod specification, which is the cleaner option when you control the Pod definition. The most common failure here is a targetPort that does not match the container: the Service forwards traffic to a port where nothing is listening, and the connection is refused.
The third number is nodePort, and it only exists on NodePort and LoadBalancer Services. It is the port opened on every node, from the 30000 to 32767 range, and the port the node listens on for traffic bound for the Service. You only need to think about it when connecting from outside the cluster through a node address, or when an external load balancer needs to know which node port to forward to. Inside the cluster it is irrelevant: Pods always use the Service port. Put the three together for the running example. The Service listens on port 80. The Pods listen on targetPort 8080. The nodes listen on nodePort 30080. A request from another Pod goes to web:80 and reaches a Pod on 8080. A request from outside goes to node-ip:30080 and reaches a Pod on 8080. A request to the cloud load balancer goes to the load balancer on port 80, which forwards to a node on 30080, which forwards to a Pod on 8080. Three doors, one destination, and the Pod only ever sees traffic on 8080.
# Inside the cluster, through the Service port
kubectl run curl-test --image=curlimages/curl -i --rm -- curl -s http://web:80/
# From outside, through the node port (NodePort or LoadBalancer)
curl http://NODE-IP:30080/
# Check what the Service is actually configured to do
kubectl get svc web -o yaml
A few practical rules make port mapping easier to live with. Keep the Service port conventional and stable, because other teams will hard-code it in their configuration. Use a named targetPort when you can, so a container port change does not require a Service change. Let Kubernetes allocate the nodePort unless you have a specific reason to pin it, such as a firewall rule that expects a fixed port, because a pinned nodePort can collide with another Service and block creation. When you debug, check the numbers in order: is the client connecting to the Service port, is the targetPort the port the container listens on, and, for external access, is the nodePort the one that is actually open. One more setting is worth knowing: Services default to TCP. If your application uses UDP — for DNS, for some gaming or streaming protocols, or for certain monitoring agents — you must set protocol: UDP explicitly, or the Service will forward TCP and your UDP packets will go nowhere. A Service can also expose multiple ports, for example HTTP on one port and metrics on another, by listing two entries under ports, each with its own name. If the networking concepts underneath this are still hazy, a refresher will help. The networking basics for DevOps engineers guide covers the underlying ideas that Services build on.
Headless Services and DNS
Not every workload wants a load balancer in front of it. Sometimes the caller needs to know about the individual Pods, not just the Service as a whole. A database cluster with a primary and replicas is the classic example: the application needs to send writes to the primary and reads to the replicas, and a Service that spreads connections randomly across all of them would send writes to a replica and break. A headless Service solves this by giving up the cluster IP and handing the caller the list of Pod addresses directly, so the caller can decide which Pod to talk to. You create one by setting clusterIP: None in the specification. There is no virtual IP and no load balancing. Instead, a DNS query for the Service name returns the IP addresses of the Pods behind it, as A records, one for each ready Pod.
apiVersion: v1
kind: Service
metadata:
name: db
labels:
app: db
spec:
clusterIP: None
selector:
app: db
ports:
- name: postgres
port: 5432
targetPort: 5432
protocol: TCP
The DNS behaviour is the part to understand carefully, because it is different from a normal Service in a way that matters. For a normal ClusterIP Service called web, a DNS lookup returns a single A record: the cluster IP. For a headless Service called db, the same lookup returns one A record per ready Pod, with the Pod IPs themselves. And for Pods created by a StatefulSet behind a headless Service, each Pod also gets its own DNS entry in the form pod-name.service-name.namespace.svc.cluster.local, so db-0.db.default.svc.cluster.local resolves to the first replica specifically. That per-Pod DNS name is what makes StatefulSets usable for clustered databases, message brokers and other systems where identity matters.
Try it from inside the cluster with a temporary Pod:
kubectl run dns-test --image=busybox:1.36 -i --tty --rm -- sh
nslookup web
nslookup db
nslookup db-0.db
DNS in Kubernetes follows a predictable pattern, and it is worth learning the full form even though you will usually use the short form. A Service called web in the namespace default has the fully qualified name web.default.svc.cluster.local: the Service name, the namespace, the literal svc, and the cluster domain, which is cluster.local unless the cluster was configured differently. Inside the same namespace, the short name web resolves because the Pod DNS search path includes its own namespace. From another namespace, you need at least web.default, and the full form always works. When a connection fails across namespaces, check first whether the name was qualified correctly. Headless Services are also the mechanism behind service discovery for applications that do their own load balancing: some client libraries resolve a DNS name, get several addresses, and spread requests across them with their own retry and health-check logic. The Kubernetes DNS for Services and Pods documentation covers the naming rules, the records created for headless Services, and the special cases for Pods.
Choosing the Right Service Type
Choosing a Service type is a decision about who needs to reach the component and from where. Start from the consumer, not from the Service. If the only consumers are other Pods in the cluster, use ClusterIP. If the consumer is outside the cluster and you are on a cloud provider, use LoadBalancer. If the consumer is outside the cluster and you are not on a cloud provider, use NodePort, usually behind an external load balancer or an Ingress controller. If the consumer needs the individual Pod addresses, use a headless Service. That decision tree covers almost every real case.
The more interesting question is how the types combine in a real architecture, because a production cluster rarely uses one type for everything. A typical web application has a ClusterIP Service for the database, a ClusterIP Service for the API, a ClusterIP Service for the frontend Pods, and a single public entry point in front of the frontend. That entry point is usually not a LoadBalancer Service per application, because that would create a load balancer for every application and a bill to match. Instead, one LoadBalancer Service fronts an Ingress controller, and the Ingress routes traffic to the right ClusterIP Service based on the hostname and path in the request. One public address, many applications behind it, and the internal Services stay private. Ingress adds hostname routing, path routing and TLS termination, which a plain LoadBalancer Service does not provide. The Gateway API is the newer, more expressive successor to Ingress, and it is the direction the ecosystem is moving, but the underlying idea is the same. If you want to go deeper on that next step, the Kubernetes Ingress tutorial walks through setting up an Ingress controller and routing traffic to ClusterIP Services behind it.
Frequently Asked Questions
What is the difference between ClusterIP, NodePort and LoadBalancer?
ClusterIP exposes a Service only inside the cluster, on a virtual IP that never changes while the Service exists. NodePort adds to that by opening the same port on every node, so the Service is reachable from outside the cluster through any node address and a port in the 30000 to 32767 range. LoadBalancer adds a cloud load balancer in front of the NodePort, giving the Service a public IP address or hostname on a standard port.
What port range does NodePort use?
The default NodePort range is 30000 to 32767. Kubernetes allocates a port from that range automatically when you create a NodePort or LoadBalancer Service without specifying one, and you can set a specific port inside the range with the nodePort field. The range is a cluster-wide setting, so changing it requires cluster administrator access. If you request a nodePort outside the range, or one already used by another Service, the Service will be rejected.
How does a Service find its Pods?
A Service finds its Pods through its selector, a set of label key-value pairs matched against Pod labels in the same namespace. The endpoint controller watches for Pods that match and keeps an EndpointSlice listing their IP addresses and ports, adding Pods when they become ready and removing them when they stop being ready. Traffic sent to the Service is forwarded to one of the addresses in that slice. If the slice is empty, the selector matches nothing, which usually means a label typo or a mismatch between the Deployment template and the Service selector.
What is a headless Service and when should I use one?
A headless Service is a Service with clusterIP set to None, which means it gets no virtual IP and does no load balancing. Instead, a DNS query for the Service name returns the IP addresses of the Pods behind it, so the caller can choose which Pod to contact. Use one when the caller needs to address individual Pods, most commonly for clustered databases and other StatefulSet workloads where each replica has its own identity and writes must go to a primary while reads go to replicas.
Can I change a Service type after creating it?
Yes. You can change a Service from ClusterIP to NodePort or LoadBalancer, or back again, by editing the Service definition, and the cluster IP and DNS name stay the same. The Pods behind the Service are not affected. This makes it safe to start with ClusterIP, the default and the most private option, and add external access only when you need it. The one thing you cannot change is the cluster IP itself, which is assigned when the Service is created and stays attached to it until the Service is deleted.
Conclusion
Kubernetes Services are a small idea with a large effect. A Service gives a changing set of Pods a stable address and a DNS name, and it keeps that address stable while Pods are created, destroyed, scaled and updated behind it. ClusterIP does that privately inside the cluster, NodePort adds a door on every node, and LoadBalancer adds a cloud load balancer with a public address. The selector and EndpointSlice machinery underneath is the same in all three cases, and the three port numbers — port, targetPort and nodePort — describe the three doors a request can pass through on its way to a Pod. The examples in this guide apply to current clusters, including Kubernetes 1.37 “Garhwal” (patch 1.37.3, released 25 November 2026); 1.38 is planned for 16 December 2026 and has not been released yet.
The practical habit to take away is to start with ClusterIP for everything, expose only what must be public, and use a single Ingress or Gateway entry point for public traffic rather than a load balancer per application. Check the endpoints whenever a Service seems unreachable, match the targetPort to the port the container actually listens on, and use a headless Service when the caller needs the Pod addresses themselves. Those habits will carry you through most Service problems, and they will keep your cluster private by default and understandable as it grows.



