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.
2.How it works
Two watch loops. No traffic goes through either.
- The EndpointSlice controller (kube-controller-manager) watches the selector and the matching Pods.
- It writes Pod IPs, ports and ready state into EndpointSlices labelled
kubernetes.(100 per slice by default).io/ service-name=web - kube-proxy on every node watches both and programs kernel rules for
10.96.0.15:80.
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.
| Type | What it gets | Reach it from | Good for |
|---|---|---|---|
| ClusterIP | A virtual IP from the service range | Inside the cluster only | Service to service calls |
| Headless | No IP. DNS returns the Pod IPs | Inside, through DNS | StatefulSets, peer discovery |
| NodePort | ClusterIP + a port on every node | + outside on NodeIP:nodePort | Your own external LB, labs |
| LoadBalancer | NodePorts + an external LB IP | + outside on the LB IP | Production L4 (cloud or MetalLB) |
| ExternalName | No IP. A CNAME to externalName | Inside, cluster DNS only | Alias 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
1.DNS, headless, ExternalName
Every Service gets <svc>.<ns>..
- Plain
webonly works in the same namespace. Elsewhere useweb.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).
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 .. 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.
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-proxy | Status in v1.37 |
|---|---|
| iptables | Default on Linux |
| nftables | Stable since v1.33, a future default |
| ipvs | Deprecated in v1.35, removal planned v1.43 |
| kernelspace | The only Windows mode |
4.Commands
| Command | What it does |
|---|---|
| kubectl expose deployment web --port=80 --target-port=8080 | Service using the Deployment's selector (add --type) |
| kubectl create service clusterip web --tcp=80:8080 | Selector app=web. --clusterip="None" = headless |
| kubectl create service externalname db --external-name my.database.example.com | CNAME alias for an outside host |
| kubectl get svc web / kubectl describe svc web | TYPE, CLUSTER-IP, EXTERNAL-IP, PORT(S), selector |
| kubectl get endpointslices -l kubernetes.io/service-name=web | Every endpoint, ready or not. Empty = bad selector or no Pods |
| kubectl get endpoints web | Ready 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.28 | Throwaway Pod, then nslookup web.prod |
| curl http://localhost:10249/proxyMode | On a node : which mode kube-proxy runs |
5.Gotchas & Good to know
- One Pod per connection : gRPC and HTTP/2 stick.
- A bare
webfrom another namespace leaves the cluster : web is a public TLD. - ExternalName breaks Host and TLS names.
- Env vars skip Pods older than the Service.
- Two or more ports : each needs a name.
- nftables : NodePorts on the primary IP only.
- Skip
externalIPs(deprecated v1.36) andloadBalancerIP(deprecated v1.24). - v1.37 : names may start with a digit.