Kubernetes distributes traffic across replicas using a Service. When you create a Service, it acts as a load balancer that routes traffic to the pods (replicas) associated with that Service. Here's how it works:
-
Service Definition: You define a Service with a selector that matches the labels of the pods you want to expose.
-
Cluster IP: The Service gets a stable IP address (Cluster IP) that clients can use to access the pods.
-
Load Balancing: Kubernetes uses iptables or IPVS (depending on the configuration) to distribute incoming traffic to the pods evenly. This is done by routing requests to the available pod IPs based on the defined Service.
-
Session Affinity: Optionally, you can configure session affinity (sticky sessions) to ensure that requests from the same client are sent to the same pod.
Here’s a simple example of a Service definition:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
In this example, the Service named my-service will route traffic to all pods with the label app: my-app on port 8080.
