Services and networking
Pods come and go and their IPs change. A Service gives a stable name and address in front of them.
Every pod gets its own IP address, but pods are replaced all the time and their addresses change. A Service is a stable virtual address and DNS name that sends traffic to the healthy pods that match a label selector.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80Inside the cluster, other pods reach it as web (or web.default.svc.cluster.local). Cluster DNS resolves that name to the Service address.
Service types
| Type | Who can reach it | Typical use |
|---|---|---|
| ClusterIP (default) | Only inside the cluster | Internal services and databases |
| NodePort | Outside, on a port of every node | Simple tests, small setups |
| LoadBalancer | Outside, through a cloud load balancer | Public apps on a cloud provider |
Ingress
An Ingress routes HTTP and HTTPS traffic by host name and path (for example /api to one Service and / to another). It needs an ingress controller installed in the cluster to work.
NoteFor quick access to a Service from your laptop, use kubectl port-forward service/web 8080:80.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.