Video in progress
Why do you need a Service in Kubernetes? Can't you just use the Pod IP directly?
Pod IPs change when Pods die and come back. A Service gives a stable name and IP in front of them and spreads traffic across the ready Pods it finds by label.
📖 Answer
Say order-service calls payment-service. If it hard-codes a Pod IP, everything breaks on the first restart.
apiVersion: v1
kind: Service
metadata:
name: payment
spec:
selector:
app: payment-service # sends to pods with this label
ports:
- port: 80
targetPort: 8080
Now another service just calls http://payment (or payment.<namespace>.svc.cluster.local). DNS returns the Service's permanent IP, and it forwards the request to one of the running Pods.
Service types: ClusterIP (the default, inside the cluster only), NodePort, LoadBalancer. To bring HTTP traffic in from outside, you usually use an Ingress or a Gateway.
One more point: a Service sends traffic only to Pods that have passed the readiness check.
Sign up to mark studied questions and save your progress.