Core Kubernetes · 05

Services

1.Why a Service ?

Pods come and go, and each new Pod gets a new IP. A Service puts one stable DNS name, virtual IP and port in front of the Pods its label selector matches.

  • Clients use the name. Traffic goes to ready Pods (if none is ready, to Pods still serving while they stop).
  • L4 only (TCP, UDP, SCTP). HTTP routing and TLS : Ingress or Gateway API.
Not a box in the pathNo Service process exists. The ClusterIP is virtual : no host owns it and nothing answers on it. Rules on the client's own node rewrite each new connection to a real Pod IP. No Pod to pick ? DNS still answers, and a REJECT refuses the call after about a second. internalTrafficPolicy Local with no Pod on that node is a DROP : a timeout.

2.How it works

Two watch loops. No traffic goes through either.

  1. The EndpointSlice controller (kube-controller-manager) watches the selector and the matching Pods.
  2. It writes Pod IPs, ports and ready state into EndpointSlices labelled kubernetes.io/service-name=web (100 per slice by default).
  3. kube-proxy on every node watches both and programs kernel rules for 10.96.0.15:80.
Service web 10.96.0.15:80 Matching Pods listen on 8080 Slice controller kube-controller-manager watch watch, ready ? writes EndpointSlice PodIP:8080 + ready flag kubernetes.io/ service-name=web kube-proxy on every node watch watch programs node rules iptables or nftables
Each new connection
Client Podasks DNS for web.prod
CoreDNSanswers 10.96.0.15
Client Poddials 10.96.0.15:80
Its own nodeDNAT to one ready PodIP:8080
Podgets it on 8080

One Pod per connection. iptables walks a chain of random-probability jumps, nftables does one verdict-map lookup, ipvs uses a kernel IPVS virtual server (round robin). The ClusterIP never goes on the wire.

3.The types nest

Types are layers, not rivals. Each keeps what the smaller one gets, so NodePort and LoadBalancer Services still answer inside on their ClusterIP.

A LoadBalancer drops its NodePorts only with allocateLoadBalancerNodePorts: false. ExternalName is the odd one : only a DNS CNAME, nothing is proxied.

LoadBalancer + an external IP from an LB NodePort + the same port on every node ClusterIP (default) ExternalName DNS CNAME only Headless clusterIP: None
TypeWhat it getsReach it fromGood for
ClusterIPA virtual IP from the service rangeInside the cluster onlyService to service calls
HeadlessNo IP. DNS returns the Pod IPsInside, through DNSStatefulSets, peer discovery
NodePortClusterIP + a port on every node+ outside on NodeIP:nodePortYour own external LB, labs
LoadBalancerNodePorts + an external LB IP+ outside on the LB IPProduction L4 (cloud or MetalLB)
ExternalNameNo IP. A CNAME to externalNameInside, cluster DNS onlyAlias for an outside host

4.port, targetPort, nodePort

  • portwhat clients dial on the ClusterIP
  • targetPortthe Pod's port. Defaults to port, or names a containerPort
  • nodePortthe same port on every node
  • protocolTCP (default), UDP or SCTP
Client outside Client in a Pod NodeIP:30007 nodePort 10.96.0.15:80 port node rules kube-proxy DNAT PodIP:8080 targetPort range 30000-32767 by default fixed picks : 30000-30085
In short : A Service is a stable name and virtual IP over the Pods its selector picks. EndpointSlices track the Pods, kube-proxy turns them into node rules, and the types nest.

1.DNS, headless, ExternalName

Every Service gets <svc>.<ns>.svc.cluster.local.

  • Plain web only works in the same namespace. Elsewhere use web.prod.
  • SRV : _http._tcp.web... needs a named port.
  • Headless : clusterIP: None (the word, not an empty field). No VIP, kube-proxy skips it.
  • No selector : no EndpointSlices. You write them (outside DB, migrations).
Client Pod asks DNS ClusterIP also NodePort, LB Headless clusterIP: None ExternalName points at a host A = ClusterIP kube-proxy DNATs A = Pod IPs client picks one CNAME client goes direct

BusyBox 1.37 nslookup : a dotless name gets only the search list, a dotted one is sent as typed, exit 1 on any NXDOMAIN. An SRV query with no _http._tcp lists every port.

2.Traffic from outside

Kubernetes ships no load balancer. A cloud-controller-manager, or the controller named in loadBalancerClass, builds one and fills .status.loadBalancer. Until then EXTERNAL-IP says <pending>. Bare metal : MetalLB.

externalTrafficPolicy (NodePort and LB) :

  • Clusterdefault. Any node, SNAT, the app sees a node IP.
  • Localclient IP kept. Nodes with no local Pod fail the LB health check.
Client source IP C Load balancer cloud LB or MetalLB Cluster only nodePort Node A (no Pod) Local : health check fails, skipped Node B Pod Local : C kept Cluster : SNAT, client IP lost

Local drops only outside traffic : a Pod, or the node itself, calling a node with no local Pod is still answered.

3.Traffic knobs

  • sessionAffinity : None or ClientIP (timeout 3 h by default, 24 h max).
  • internalTrafficPolicy : Local = same-node endpoints only, or a drop (a timeout).
  • trafficDistribution : PreferSameZone or PreferSameNode. A hint, Local wins.
kube-proxyStatus in v1.37
iptablesDefault on Linux
nftablesStable since v1.33, a future default
ipvsDeprecated in v1.35, removal planned v1.43
kernelspaceThe only Windows mode

Pin the mode yourself before the default flips.

4.Commands

CommandWhat it does
kubectl expose deployment web --port=80 --target-port=8080Service using the Deployment's selector (add --type)
kubectl create service clusterip web --tcp=80:8080Selector app=web. --clusterip="None" = headless
kubectl create service externalname db --external-name my.database.example.comCNAME alias for an outside host
kubectl get svc web / kubectl describe svc webTYPE, CLUSTER-IP, EXTERNAL-IP, PORT(S), selector
kubectl get endpointslices -l kubernetes.io/service-name=webEvery endpoint, ready or not. Empty = bad selector or no Pods
kubectl get endpoints webReady ones only, like describe svc, after Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
kubectl run tmp -it --rm --restart=Never --image=busybox:1.28Throwaway Pod, then nslookup web.prod
curl http://localhost:10249/proxyModeOn a node : which mode kube-proxy runs

5.Gotchas & Good to know

Gotchas
  • One Pod per connection : gRPC and HTTP/2 stick.
  • A bare web from another namespace leaves the cluster : web is a public TLD.
  • ExternalName breaks Host and TLS names.
  • Env vars skip Pods older than the Service.
Good to know
  • Two or more ports : each needs a name.
  • nftables : NodePorts on the primary IP only.
  • Skip externalIPs (deprecated v1.36) and loadBalancerIP (deprecated v1.24).
  • v1.37 : names may start with a digit.
In short : DNS maps a Service to a ClusterIP, Pod IPs or a CNAME. Outside traffic needs an LB implementation, and externalTrafficPolicy decides if the client IP survives.