Lesson 07 intermediate

Kubernetes Services

How Services find Pods with label selectors, port vs targetPort, cluster DNS, the ClusterIP, NodePort, LoadBalancer and ExternalName types, headless Services and why a Service is not a proxy.

Pods come and go, and their IP addresses change every time. A Service gives a group of Pods one stable name and one virtual IP that never change, and spreads connections across whichever Pods are healthy right now. Every app that other apps call needs a Service in front of it.

How a Service finds its Pods

A Service does not list Pods by name. It uses a label selector, and any Ready Pod with matching labels becomes a backend.

apiVersion: v1
kind: Service
metadata:
  name: shop-api
spec:
  selector:
    app: shop-api
  ports:
    - port: 80
      targetPort: 8080

Kubernetes stores the matching Pod addresses in EndpointSlice objects:

$ kubectl get endpointslices -l kubernetes.io/service-name=shop-api
NAME             ADDRESSTYPE   PORTS   ENDPOINTS                            AGE
shop-api-x7k2p   IPv4          8080    10.244.1.17,10.244.2.9,10.244.1.23   3d

The number one Service bug: an empty ENDPOINTS column. Nothing will ever answer. There are only two causes: the selector does not match the Pod labels (even app: shop vs app: shop-api breaks it), or the matching Pods are not Ready because their readiness probe fails.

Compare both sides when in doubt:

$ kubectl get svc shop-api -o jsonpath='{.spec.selector}'
{"app":"shop-api"}
$ kubectl get pods --show-labels
NAME                       READY   STATUS    LABELS
shop-api-6d9f7b8c4-kq2xv   1/1     Running   app=shop-api,pod-template-hash=6d9f7b8c4

port, targetPort and nodePort

FieldUsed byMeaning
portClients inside the clusterThe port the Service listens on
targetPortThe ServiceThe port on the Pod that traffic is sent to
nodePortClients outside the clusterA port opened on every node (NodePort and LoadBalancer only)

With port: 80 and targetPort: 8080, callers use http://shop-api on port 80 and the container receives the traffic on 8080. You can also point targetPort at a named port from the Pod spec, such as targetPort: http. It reads better and keeps working if the container port changes.

DNS names

Every Service gets a DNS record from CoreDNS:

  • From the same namespace, the short name works: http://shop-api.
  • From another namespace, add the namespace: http://shop-api.shop.
  • The fully qualified name always works: shop-api.shop.svc.cluster.local.

If a call works from one namespace and fails from another, the namespace part of the name is almost always missing. Test DNS from a throwaway Pod:

$ kubectl run dns-test --rm -it --image=nicolaka/netshoot -- nslookup shop-api.shop
Name:    shop-api.shop.svc.cluster.local
Address: 10.96.142.7

The four Service types

ClusterIP (the default)

Reachable only from inside the cluster. This is the right type for most Services: databases, internal APIs and caches.

NodePort

Opens the same port on every node, from the range 30000 to 32767, and forwards it to the Service:

spec:
  type: NodePort
  selector:
    app: shop-api
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080

Leave out nodePort and Kubernetes picks one for you. NodePort is handy on local clusters, but it is awkward in production: odd port numbers, you still need a load balancer in front of the nodes, and every node listens even if it runs none of the Pods.

LoadBalancer

Asks the cloud provider for an external load balancer that forwards to the Service:

$ kubectl get svc shop-api
NAME       TYPE           CLUSTER-IP    EXTERNAL-IP                                   PORT(S)
shop-api   LoadBalancer   10.96.142.7   a3f9c1e2b7.elb.us-east-1.amazonaws.com        80:31544/TCP

Each LoadBalancer Service gets its own cloud load balancer, with its own cost and address. Ten public apps means ten load balancers. For many HTTP apps behind one address, use Ingress or the Gateway API instead.

If EXTERNAL-IP stays <pending>, nothing is fulfilling the request. On a cloud, check the controller logs and the subnet tags it needs. On bare metal or a laptop there is no cloud: install MetalLB, use minikube tunnel or use port-forward.

ExternalName

No proxying at all. The Service is a DNS alias (a CNAME) to a name outside the cluster:

apiVersion: v1
kind: Service
metadata:
  name: orders-db
spec:
  type: ExternalName
  externalName: orders.c9x2k1.us-east-1.rds.amazonaws.com

Apps connect to orders-db, and if the database later moves into the cluster you only change the Service.

Headless Services

Set clusterIP: None and Kubernetes allocates no virtual IP. A DNS lookup then returns every Pod IP instead of one. StatefulSets rely on this so that clients can reach db-0 and db-1 individually, and so do clustered systems and database drivers that discover their own topology.

A Service is not a proxy server

There is no process behind a ClusterIP. kube-proxy writes iptables or IPVS rules on every node, and the kernel rewrites packets on the way. Nothing terminates TLS, reads HTTP or retries requests. That has two consequences you will meet in practice:

  • Load balancing is per connection, not per request. A long-lived HTTP/2 or gRPC connection stays pinned to one Pod for its whole life.
  • There is no routing by path or host. A Service only knows ports. HTTP routing needs Ingress or the Gateway API.

sessionAffinity: ClientIP makes a client stick to one Pod, but moving session state out of the Pod (into Redis, for example) is the better fix.

Quick reference

TaskCommand
List Serviceskubectl get services
Pod IPs behind a Servicekubectl get endpointslices -l kubernetes.io/service-name=<name>
Details and eventskubectl describe service <name>
Read the selectorkubectl get svc <name> -o jsonpath='{.spec.selector}'
Compare Pod labelskubectl get pods --show-labels
Reach it from your laptopkubectl port-forward service/<name> 8080:80
Create a Service for a Deploymentkubectl expose deployment <name> --port=80 --target-port=8080
Test DNSkubectl run tmp --rm -it --image=nicolaka/netshoot -- nslookup <svc>

Exercise

A Pod in namespace checkout calls http://shop-api and gets a DNS error. The Service lives in namespace shop. What fixes it?

  1. Changing the Service to NodePort
  2. Calling http://shop-api.shop instead
  3. Restarting CoreDNS
  4. Adding a hostPort to the Pods
Show answer

Answer: B. The short name only resolves inside the Service’s own namespace. Add the namespace, or use the full shop-api.shop.svc.cluster.local.

Exercise

Which kind of Service returns the IP of every Pod in DNS instead of one virtual IP?

  1. NodePort
  2. ExternalName
  3. LoadBalancer
  4. A headless Service with clusterIP: None
Show answer

Answer: D. Without a virtual IP, DNS answers with all Ready Pod IPs. StatefulSets and clients that balance on their own depend on this.

Key takeaways

  • Services select Pods by label; the results live in EndpointSlices.
  • An empty endpoint list means a selector mismatch or Pods that are not Ready.
  • port is what clients dial, targetPort is where the Pod listens.
  • Calls across namespaces need the namespace in the DNS name.
  • ClusterIP for internal traffic; one LoadBalancer per Service gets expensive, so use Ingress or Gateway API for many HTTP apps.
  • Balancing is per connection: long-lived gRPC connections stick to one Pod.
  • Headless Services (clusterIP: None) return Pod IPs, which StatefulSets need.

FAQ

What is the difference between ClusterIP, NodePort and LoadBalancer?

Each builds on the previous one. ClusterIP is internal only. NodePort adds a port from 30000 to 32767 on every node. LoadBalancer adds a cloud load balancer in front of those node ports.

Why does my Service have no endpoints?

Either no Pod has labels matching the selector, or the matching Pods are not Ready. Compare kubectl get svc <name> -o jsonpath='{.spec.selector}' with kubectl get pods --show-labels, then check the readiness probes.

How do I call a Service in another namespace?

Use <service>.<namespace>, for example shop-api.shop, or the full name shop-api.shop.svc.cluster.local. The short name only resolves inside the same namespace.

Why is my LoadBalancer stuck at pending?

No controller is creating the load balancer. On a cloud, check the cloud controller or load balancer controller logs and required subnet tags. Locally, use minikube tunnel, MetalLB or port-forward.

↑ ↓ navigate Enter open Esc close