Load balance traffic
The ingress controller load balances traffic across pods without further configuration, but you can customize its behavior, such as to set a different load balancing algorithm.
Change the load balancing algorithm for a specific service
By default, the ingress controller uses the round-robin algorithm to distribute requests across a service’s pods. To change the algorithm for a specific service:
-
Edit the Ingress resource and set the load-balance annotation to your chosen algorithm. Below, we set the service to use the random algorithm:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress annotations: load-balance: random spec: rules: - host: "example.com" http: paths: - path: / pathType: Prefix backend: service: name: example-service port: number: 8080
Enable sticky sessions (session persistence)
In some cases, you may need to route all of a client’s requests to the same backend pod, for example if that pod has stored the client’s server-side session, instead of load balancing the client’s requests across multiple pods. This is called sticky sessions. To enable it for a service:
-
Edit the Ingress resource and set the cookie-persistence annotation to a unique name, which the ingress controller uses when naming the HTTP cookie it stores in the client’s browser. The cookie will contain the selected pod’s IP address, port, and a secret key.
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress annotations: cookie-persistence: "example-session-persistence-cookie" spec: rules: - host: "example.com" http: paths: - path: / pathType: Prefix backend: service: name: example-service port: number: 8080
Next up
Enable logging