12.8 Session Affinity and Traffic Policies

Alright, let’s talk about how your Services actually decide which Pod gets the traffic. You’ve deployed your fancy, multi-Pod Deployment, exposed it with a Service, and you’re probably thinking, “Great, traffic will just spread out evenly!” And by default, you’d be right. But the real world is messy, and sometimes you need to bend that default behavior to your will. That’s where session affinity and traffic policies come in. The Default: A Fair-Weather Friend Out of the box, a standard ClusterIP or NodePort Service uses a completely stateless, round-robin load balancing algorithm across all ready Pods it selects. It’s the epitome of fairness. This is handled by kube-proxy on each node, either via iptables or IPVS rules. It’s simple, effective, and for most stateless workloads, it’s exactly what you want. But “most” isn’t “all.” The moment you need a user’s requests to consistently hit the same Pod—maybe because of an in-memory session, a cache, or some other sticky piece of state—this fairness becomes a problem.

12.7 Service Endpoints and EndpointSlices

Alright, let’s pull back the curtain on the real magic trick: how your Service actually routes traffic. You’ve defined this abstract Service with a selector, but that’s just a declaration of intent. The actual, on-the-ground traffic cops are the Endpoints and EndpointSlices resources. If you don’t understand them, you’re flying blind when things go wrong. Think of your Service as a VIP list for a club. The Endpoints resource is the actual, physical bouncer’s list, with the current addresses of who’s allowed in. When you create a Service with a selector like app=my-api, Kubernetes doesn’t just sit there. It constantly scans the cluster for Pods that match those labels. It then takes the IP addresses of those healthy Pods (where their readiness probes are passing) and populates a resource named exactly the same as your Service: the Endpoints resource.

12.6 Headless Services: Direct Pod DNS Without a VIP

Alright, let’s talk about Headless Services. You’ve probably noticed that a regular Kubernetes Service is a bit of a control freak. It creates a Virtual IP (VIP), sits in front of your Pods, and insists that all traffic go through it for load balancing. It’s a middle-manager. Sometimes, that’s exactly what you want. But what if you don’t want a middle-manager? What if you want to talk to your Pods directly, by name, without some VIP getting in the way? Enter the Headless Service. It’s exactly what it sounds like: a Service without a cluster-internal IP address. You create one by setting clusterIP: None in the spec. Kubernetes, in its infinite wisdom, reads this and says, “Ah, no VIP? Cool. I’ll just set up the DNS for you then and get out of your way.”

12.5 ExternalName: CNAME Alias for External Services

Alright, let’s talk about the weirdo of the Service family: ExternalName. If ClusterIP, NodePort, and LoadBalancer are the overachieving siblings who handle internal traffic, ExternalName is the one who just points out the window and says, “Nah, the thing you want is over there.” It’s gloriously, almost absurdly simple. There’s no proxy, no load balancing, no selector, no Endpoints object. It’s a CNAME record masquerading as a Kubernetes Service. And sometimes, that’s exactly what you need.

12.4 LoadBalancer: Cloud Provider Integration

Right, so you’ve arrived at the LoadBalancer. This is where Kubernetes gets off its high horse of elegant abstraction and says, “Fine, you want to talk to the real world? Let’s call your cloud provider.” A LoadBalancer service is essentially a NodePort service with a superpower: it knows how to automatically phone home to your cloud platform (AWS, GCP, Azure, etc.) and ask it to provision a brand-spanking-new external load balancer pointed right at your service.

12.3 NodePort: Exposing Services on Every Node's IP

Alright, let’s talk about NodePort. You’ve got your ClusterIP service humming along nicely, talking to its Pods, and everything is cozy inside the cluster. But now you need to let the outside world poke at your application. This is where NodePort struts onto the stage, flexing its biceps and shouting “LOOK AT ME!” at anyone with a network connection. Think of a NodePort service as a ClusterIP service that got a serious upgrade. It still gets a virtual ClusterIP for internal traffic, but it also gets something more: it tells every single worker node in your cluster to open a specific, high-numbered port (the NodePort) and forward any traffic from that port directly to the service. It’s like giving the outside world a list of every node’s IP address and saying, “Just hit any of these on port 30007, you’ll get what you need.”

12.2 ClusterIP: Internal-Only Service Discovery

Alright, let’s talk about the workhorse of the Kubernetes service world: the ClusterIP. If you’re picturing a loud, public-facing service with a flashy IP address, erase that. ClusterIP is the quiet, brilliant back-office organizer. It’s the internal switchboard operator of your cluster, and its entire existence is predicated on a simple, beautiful rule: Thou shalt not be reached from the outside world. This is service discovery for your internal microservices. Pod A needs to talk to Pod B? Fantastic. They shouldn’t use each other’s flaky, ephemeral Pod IPs directly. That’s a recipe for Connection refused disasters. Instead, Pod A talks to a stable, virtual IP address—the ClusterIP—and the kube-proxy magic on its node seamlessly forwards that traffic to a healthy pod in the backend Pod B group. It’s a stable endpoint abstracted from the chaotic reality of pod scheduling and mortality.

12.1 The Service Abstraction: Stable VIP for a Set of Pods

Right, let’s talk about the one thing that saves your bacon in a Kubernetes cluster: the Service. You’ve deployed your app. You’ve got, say, three nginx Pods running. They all have their own unique, flaky IP addresses. Pods die, get rescheduled, and get new IPs. You can’t rely on those IPs for anything. Telling another app, “Hey, just connect to 10.244.1.5,” is a recipe for failure. It’s like trying to mail a letter to a friend who changes their apartment number every other day.

— joke —

...