Services, Ingress & networking
Pods come and go, and every new pod gets a new IP address. Services give a group of pods one stable name and address, Ingress brings outside HTTP traffic in by hostname and path, and NetworkPolicies decide who may talk to whom.
The pod network
Every pod gets its own IP, and every pod can reach every other pod directly, across nodes, without NAT. A CNI plugin (Calico, Cilium, Flannel and others) builds that network. Because pod IPs change whenever a pod is replaced, nothing should ever be hard-coded to one: you talk to a Service instead.
Service types
ClusterIP (the default) gives a virtual IP reachable only inside the cluster. Right for anything internal: APIs, databases, caches.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web # all ready pods with this label
ports:
- name: http
port: 80 # the Service's port
targetPort: 3000 # the container's port
NodePort also opens the same port (range 30000-32767) on every node, so http://<any-node-ip>:30080 reaches the pods. Fine for labs, rarely used directly in production.
apiVersion: v1
kind: Service
metadata:
name: web-nodeport
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 3000
nodePort: 30080 # omit it and Kubernetes picks one
LoadBalancer asks the cloud provider (EKS, GKE, AKS) for a real external load balancer with a public IP. Locally, minikube tunnel fills that role; on bare metal you need something like MetalLB, otherwise EXTERNAL-IP stays <pending>.
apiVersion: v1
kind: Service
metadata:
name: web-public
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 3000
port, targetPort, nodePort
targetPort, the Service exposes port, and nodePort exists only for traffic from outside.| Field | Where it listens | Who uses it |
|---|---|---|
port | On the Service's ClusterIP | Other pods: http://web:80 |
targetPort | On the pod (the containerPort); can also be a port name | The Service forwards here. Defaults to port |
nodePort | On every node's IP (30000-32767) | Clients outside the cluster (NodePort/LoadBalancer only) |
Selectors and endpoints
A Service finds its pods purely by label selector, and only pods that pass their readiness probe receive traffic. The list of current pod IPs is kept in EndpointSlices:
$ kubectl describe svc web # "Endpoints:" line
$ kubectl get endpointslices -l kubernetes.io/service-name=web
$ kubectl get pods -l app=web --show-labels # do the labels really match?
A Service with no endpoints is the number one reason for "connection refused": either the selector doesn't match the pod labels (a typo, app: Web vs app: web) or no pod is ready.
Cluster DNS
CoreDNS gives every Service a DNS name of the form <service>.<namespace>.svc.cluster.local (cluster.local is the default cluster domain):
- from the same namespace:
web - from another namespace:
web.shop - fully qualified:
web.shop.svc.cluster.local
$ kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- \
nslookup web.shop.svc.cluster.local
$ kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- \
wget -qO- http://web.shop
A ClusterIP is virtual (it's implemented by kube-proxy's forwarding rules), so it usually doesn't answer ping. Test with a real request instead. A headless Service (clusterIP: None) has no virtual IP at all: its DNS name returns the pod IPs directly, which is what StatefulSets use.
Ingress: HTTP routing from outside
Instead of one LoadBalancer per Service, an Ingress routes many hostnames and paths through one entry point. The Ingress object is only a set of rules: an ingress controller (Traefik, HAProxy, F5 NGINX, your cloud's controller...) must be installed to actually carry them out. Without one, the Ingress just sits there with no address.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
spec:
ingressClassName: traefik # must match: kubectl get ingressclass
tls:
- hosts:
- shop.example.com
secretName: shop-tls # a kubernetes.io/tls Secret
rules:
- host: shop.example.com
http:
paths:
- path: /api
pathType: Prefix # /api and /api/..., not /apix
backend:
service:
name: api
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
$ kubectl create secret tls shop-tls --cert=tls.crt --key=tls.key
$ kubectl get ingress shop # ADDRESS column = controller's IP
$ curl -H "Host: shop.example.com" http://<address>/api/health # test before DNS exists
The longest matching path wins, so /api/health goes to api and everything else to web. pathType can be Prefix, Exact or ImplementationSpecific.
kubectl port-forward
For quick access from your laptop without exposing anything:
$ kubectl port-forward svc/web 8080:80 # localhost:8080 -> Service port 80
$ kubectl port-forward pod/web-7c9d8f6b5-x2kq9 9229:9229 # e.g. a debugger port
It connects to a single pod behind the Service (no load balancing) and stops when you press Ctrl+C. Great for debugging, never for real traffic.
NetworkPolicy: a firewall for pods
By default any pod can reach any other pod. A NetworkPolicy restricts that. Once a pod is selected by a policy for Ingress, everything not explicitly allowed is denied. This lets only the web pods reach the database:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-allow-web
spec:
podSelector:
matchLabels:
app: db # the pods being protected
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: web
ports:
- protocol: TCP
port: 5432
Policies are enforced by the CNI plugin. Calico and Cilium do; plain Flannel doesn't, and there the policy is silently ignored.