Services: a stable address for Pods
ClusterIP, NodePort and LoadBalancer.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/services.tar.gz && tar -xzf services.tar.gz, which creates ~/lab/services/. SHA-256: ce7ae12386affc00ab0cc302d048979edea82bcdc6ac585c9f591200890fc561Say a shop front talks to a cart app at 10.244.1.3, the IP of the cart Pod. Then the cart Pod is replaced, as Pods often are, and its replacement comes up at a different address. The shop front now sends every request to an IP nobody answers on. With three cart Pods it would also have to decide which one to call.
A Service is the answer to that. It is one address that stays put, and behind it Kubernetes keeps an up-to-date list of the Pods that should get the traffic. First, see the problem for yourself.
Pod IPs do not last
This Deployment runs three nginx Pods. The args line writes the Pod's own name into the page nginx serves, so later you can see which Pod answered.
# Three nginx Pods. Each one writes its own name into the page it serves,# so you can tell which Pod answered a request.apiVersion: apps/v1kind: Deploymentmetadata:name: webspec:replicas: 3selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.30.6-alpinecommand: ["/bin/sh", "-c"]args: ['echo "answered by $(hostname)" > /usr/share/nginx/html/index.html && exec nginx -g "daemon off;"']ports:- containerPort: 80
Compare the two tables. After kubectl delete pods, the Deployment made three new Pods with new names, and the IP column is different too. Nothing that remembered the old addresses would still work.
One stable address
A Service picks its Pods with a label selector, as lesson 7 (Labels) showed: here the app: web label the Deployment puts on every Pod. port is what clients connect to, targetPort is the port inside the Pod.
# One stable address in front of every Pod labelled app=web.apiVersion: v1kind: Servicemetadata:name: webspec:selector:app: webports:- port: 80targetPort: 80
The Service is type ClusterIP, the default. Its CLUSTER-IP is a virtual address: no Pod or node owns it, and it only works from inside the cluster. The second command shows where the traffic really goes. Kubernetes keeps an EndpointSlice for each Service, and its ENDPOINTS column holds exactly the three Pod IPs from the table above.
Now call the Service from inside the cluster. kubectl run --rm -i starts a one-off busybox Pod, runs a command in it, prints the output and deletes the Pod again. The loop asks for the page six times:
The first two lines are kubectl's usual notes about attaching to a Pod. After a two-second pause (the sleep 2 gives kubectl time to attach before the loop prints anything) six answers arrived, from different Pods, although the client only ever used the one Service IP. kube-proxy, a program on every node, spreads connections across the endpoints. Which Pod answers is down to chance, so your list will look different. The last line is --rm cleaning up.
The endpoints follow the Pods
Replace all the Pods again and look at the Service and its EndpointSlice. Then ask for five Pods instead of three with kubectl scale, which lesson 9 (Scaling) covers in depth:
The CLUSTER-IP is the same as before. The EndpointSlice kept its name but now lists three new addresses. Nobody edited it: the EndpointSlice controller watches Pods that match the selector and rewrites the list as they come and go. After scaling to five, the list has five entries; kubectl shows three and sums up the rest as + 2 more.... Each endpoint also carries a ready flag, and a Pod gets traffic only while its endpoint is marked ready. Lesson 13 (Probes) shows how an app decides that.
NodePort: a port on every node
A ClusterIP Service cannot be reached from outside the cluster. Type NodePort adds a port, here 30080, that is opened on every node and forwards to the same Pods. NodePorts must come from the range 30000-32767. Leave nodePort out and Kubernetes picks a free one.
# The same Pods, also reachable on port 30080 of every node.apiVersion: v1kind: Servicemetadata:name: web-nodeportspec:type: NodePortselector:app: webports:- port: 80targetPort: 80nodePort: 30080
The PORT(S) column now reads 80:30080/TCP: port 80 on the cluster IP, port 30080 on the nodes. The curl went to the control-plane node, which runs none of the web Pods, and still got an answer from one of them. Every node forwards its node port to wherever the Pods are. kube-proxy needs a second or two to open a new node port, and a curl sent straight after kubectl apply can be refused. That is why the command retries. kind nodes are containers on a Docker network, so their IPs are reachable from the machine that runs them.
LoadBalancer: waiting for a cloud
On AWS, Google Cloud or Azure, type LoadBalancer asks the cloud for a real load balancer with a public address, which then sends traffic to the node ports. A kind cluster has no cloud behind it:
# Asks the cluster for an external load balancer.apiVersion: v1kind: Servicemetadata:name: web-lbspec:type: LoadBalancerselector:app: webports:- port: 80targetPort: 80
EXTERNAL-IP says <pending> and stays that way. Kubernetes did its part: the Service got a cluster IP and a node port picked at random (80: followed by a port number in PORT(S)), because a LoadBalancer Service is built on top of the other two types. The missing piece is the program that talks to a cloud and fills in the address, and kind does not ship one. There are no events either, because nothing is even trying. A separate tool, cloud-provider-kind, can play that role on a laptop; this course does not need it.
| Type | Reachable from | Typical use |
|---|---|---|
| ClusterIP | Inside the cluster only | One app calling another |
| NodePort | Any node's IP, port 30000-32767 | Testing, or behind your own load balancer |
| LoadBalancer | An address the cloud hands out | Public traffic on a cloud cluster |
Hands-on
You do not always need a YAML file for a Service. kubectl expose copies a Deployment's selector into a new one.
1. Create a NodePort Service called web2 and look at it: kubectl expose deployment web --name=web2 --port=80 --type=NodePort && kubectl get service web2.
2. Read its node port and the IP of lab-worker2 into two variables, then curl them: PORT=$(kubectl get service web2 -o jsonpath="{.spec.ports[0].nodePort}"), then NODE_IP=$(kubectl get node lab-worker2 -o jsonpath="{.status.addresses[0].address}"), then curl -s --retry 5 --retry-connrefused --retry-delay 1 http://$NODE_IP:$PORT.
3. Scale the Deployment to zero, wait until its Pods are deleted, and look at the EndpointSlice: kubectl scale deployment web --replicas=0 && kubectl wait --for=delete pod -l app=web --timeout=90s && kubectl get endpointslices -l kubernetes.io/service-name=web2. kubectl wait --for=delete returns once the matching objects are gone.
4. In the same shell, curl again without retries: curl -sS --max-time 5 http://$NODE_IP:$PORT.
5. Clean up: kubectl delete deployment web && kubectl delete service web web-nodeport web-lb web2 && kubectl get all.
What you should see: in step 1, web2 gets a random node port between 30000 and 32767 (80:3xxxx/TCP). Step 2 prints answered by and the name of one Pod. In step 3 each of the five Pods reports condition met, and the EndpointSlice for web2 shows <unset> under both PORTS and ENDPOINTS. Step 4 fails with curl: (7) Failed to connect ... Could not connect to server: the Service still exists, but no Pod matches its selector, so the connection is refused. After step 5, kubectl get all lists only the built-in kubernetes Service.
Remember
Pods come and go with new IPs, so clients talk to a Service instead. The Service keeps one address, and its EndpointSlice lists the Pods that match its selector, sending traffic only to those marked ready. NodePort and LoadBalancer build on a ClusterIP, and a LoadBalancer needs a cloud to hand out its address.
<unset> under ENDPOINTS. What is the most likely reason?<pending>.