Kubernetes Services: ClusterIP, NodePort, LoadBalancer
Your Deployment from lesson 10 runs three nginx pods, and another app needs to call them. Each pod has its own IP, and as lesson 9 showed, a replacement pod usually gets a new one. This lesson explains how Kubernetes services give those pods one stable address, and how the ClusterIP, NodePort and LoadBalancer types decide who can reach it.
A Service: one stable address for changing pods
A Service is a Kubernetes object that gives a group of pods one stable name and address, and spreads connections across them.
A Service finds its pods with a label selector, the query over labels from lesson 10, such as "every pod with app=web". This file, web-svc.yaml, puts a Service in front of the web Deployment's pods:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80
Unlike a Deployment, a Service lists the labels directly under selector, with no matchLabels. port is the port callers use on the Service, and targetPort is the container port the traffic goes to; leave it out and it equals port. With no type line, you get the default type, ClusterIP.
You create a cluster in lesson 17, so read along for now. With lesson 10's web.yaml applied, the first command creates the Service and the second lists it:
kubectl apply -f web-svc.yaml
kubectl get service web
You should see something like this; your cluster IP and age will differ:
service/web created
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web ClusterIP 10.96.120.15 <none> 80/TCP 5s
A Service is created even if no pod matches its selector, so a selector typo gives no error here; it shows up only later, as an empty list of pods.
How a Service keeps track of its pods
A controller in the control plane keeps EndpointSlices up to date: objects that list the IP address and port of each pod matching a Service's selector. Pods are added as they start and removed as they go. Each slice carries the label kubernetes.io/service-name, so you can list the ones behind web:
kubectl get endpointslices -l kubernetes.io/service-name=web
You should see one slice holding three pod IPs; the name suffix, IPs and age will differ:
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
web-x7k2p IPv4 80 10.244.0.5,10.244.0.6,10.244.0.7 1m
If no pod IPs appear, the usual cause is a selector that does not match the pod labels, such as app: Web for app: web; labels are case-sensitive. Compare with kubectl get pods --show-labels.
kube-proxy, the node agent from lesson 8, turns this list into network rules on every node. For each new connection to the Service, the rules pick one pod, at random in the default mode, and the kernel forwards the traffic there. Only ready pods get traffic; lesson 15 adds readiness checks.
Spreading happens per connection, not per request. A client that reuses one open connection keeps talking to the same pod.
ClusterIP: an address inside the cluster
ClusterIP, the default type, gives the Service a virtual IP: an address, here 10.96.120.15, that belongs to no machine or network card. It exists only in the nodes' rules, so only pods and nodes in the cluster can reach it.
Callers rarely use the IP. CoreDNS, the cluster's DNS server you saw in lesson 8, gives each Service a name of the form <service>.<namespace>.svc.cluster.local, so ours is web.default.svc.cluster.local. Recall that a namespace is a named group of objects; default is used when you name none, and cluster.local is the default cluster domain.
A pod's DNS settings list search domains, suffixes tried in turn after a short name, starting with the pod's own namespace. So a pod in the same namespace can simply use web, and one in another namespace web.default. In the diagram, notice that no machine answers at the ClusterIP: the client's own node swaps it for a pod IP on the way out.
To test it from inside, start a throwaway BusyBox pod that fetches the page. --restart=Never makes it run once, -i shows its output and --rm deletes it afterwards:
kubectl run client --rm -i --restart=Never --image=busybox:1.36 -- wget -qO- http://web
You should see the start of nginx's welcome page, then a line saying the pod was deleted. Trimmed:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
wget: bad address 'web' means DNS knows no such name: check the Service name and the client's namespace. Connection refused usually means no ready pods are behind the Service.
NodePort and LoadBalancer: ways in from outside
The other two types wrap ClusterIP rather than replace it. In the diagram, notice that each layer adds one more way in to the same pods.
NodePort
A NodePort Service also opens one port on every node, from the range 30000 to 32767 by default. Traffic to any node's IP on that port reaches the Service, even on a node running none of its pods. Kubernetes picks a free port unless you set nodePort. Change the spec of web-svc.yaml to this and apply it again:
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080
Now kubectl get service web shows the node port after the Service port, and the same cluster IP:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.96.120.15 <none> 80:30080/TCP 2m
A nodePort outside the range is rejected. On kind, the nodes are Docker containers, so from a Mac or Windows laptop a node port is unreachable unless the cluster maps it; lesson 18 uses kubectl port-forward instead.
LoadBalancer
A LoadBalancer Service asks the cloud provider for an external load balancer: a managed entry point with its own address that forwards traffic into the cluster. On EKS, GKE or AKS, the cloud-controller-manager from lesson 8 creates it and writes its address into the Service. By default the Service also gets a node port and a cluster IP.
On kind, with nothing extra installed, nothing creates that load balancer, so after changing type to LoadBalancer the address stays pending:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web LoadBalancer 10.96.120.15 <pending> 80:30080/TCP 3m
That is not an error: the Service still works as a NodePort and a ClusterIP. On a laptop or your own servers, a controller such as MetalLB or cloud-provider-kind can hand out external addresses. On a cloud, each LoadBalancer Service usually means a separate, billed load balancer.
Choosing a Service type
Start from who has to reach the app. In the flowchart, notice that the public answer is not always a LoadBalancer.
Use ClusterIP for anything only other pods call, such as an internal API, a database or a cache. Use NodePort mostly for testing, since a high port on every node is awkward to hand to users.
For the public, use a LoadBalancer Service or an Ingress: HTTP routing rules that let many Services share one entrance, usually one load balancer in front of an ingress controller (lesson 12). For a quick look from your laptop, kubectl port-forward is often simpler.
Check yourself
During a rolling update, old and new web pods run side by side. Which get traffic from the Service?
Ready pods of both versions. The selector is only app=web, which both ReplicaSets' pods carry; their pod-template-hash labels differ, but the Service does not select on that label.
A pod in the namespace shop runs wget http://web and gets bad address, though the web Service exists in default. Why?
The search domains start with the caller's own namespace, so web is tried as web.shop, which does not exist, and the other suffixes do not match either. Use web.default or the full name.
A NodePort Service uses port 30080, and all its pods run on node A. Someone connects to node B on port 30080. Does it work?
Yes, with the default settings. Every node's rules forward the node port to the Service, which picks a ready pod wherever it runs, here on node A.