Middle+
How is load balancing implemented in Kubernetes?
sobes.tech AI
Answer from AI
Load balancing in Kubernetes is implemented at several levels:
-
Service (at the Kube-proxy level):
- Each Pod in a Service receives a unique IP address.
- Kube-proxy, running on each node, monitors changes in Service and EndpointSlice.
- Sets up traffic redirection rules (iptables, ipvs) to the cluster IP of the Service.
- Traffic arriving at the Service's cluster IP is distributed among the Pods of that Service according to the selected algorithm (default Round Robin).
- There are two modes of Kube-proxy operation: iptables (standard, based on Linux netfilter) and ipvs (more efficient for large clusters, based on Virtual Server API).
apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: my-app # Selects Pods with label app: my-app ports: - protocol: TCP port: 80 # Port on the Service targetPort: 8080 # Port on Pods -
Ingress (at L7 level):
- Acts as a single entry point for external HTTP/S traffic load balancing.
- Requires an Ingress controller (e.g., Nginx Ingress Controller) to implement rules.
- Routes traffic to a specific Service inside the cluster based on URL path, hostname, and other L7 rules.
- Allows implementing various load balancing strategies, SSL termination, Name-based virtual hosting.
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress spec: rules: - host: myapp.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service # Routes traffic to this Service port: number: 80 -
LoadBalancer Service (cloud provider level):
- When creating a Service of type
LoadBalancer, Kubernetes interacts with the cloud provider's API (AWS, GCP, Azure, etc.). - The provider creates an external load balancer that directs traffic to the cluster nodes.
- Then Kube-proxy redirects traffic to Pods inside the cluster.
- This Service type provides external access and load balancing at L4 (TCP/UDP) or L7 (HTTP/S) depending on the provider.
apiVersion: v1 kind: Service metadata: name: my-external-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer # Creates an external load balancer - When creating a Service of type
-
Custom Load Balancers (e.g., HAProxy, Nginx as Pods):
- You can deploy a load balancer inside the cluster as a Deployment and Service (ClusterIP or NodePort).
- Configure external traffic to this Service.
- The load balancer will distribute traffic among other Pods independently.
Thus, Kubernetes offers a multi-layered approach to load balancing, combining built-in mechanisms (Service, Kube-proxy) with external solutions (Ingress, Cloud Provider Load Balancers) and the ability to deploy custom load balancers.