Ingress tutorials
Load balance traffic
The ingress controller load balances traffic across pods by reading Ingress resources, which group pods under Service resources. But, you can customize its behavior, such as to set a different load balancing algorithm.
Change the load balancing algorithm for a specific service Jump to heading
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 therandom
algorithm:example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:load-balance: randomspec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:load-balance: randomspec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080 -
Apply the change with
kubectl apply
:nixkubectl apply -f example-ingress.yamlnixkubectl apply -f example-ingress.yaml
Enable sticky sessions (session persistence) Jump to heading
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, you would want to use that same pod, rather than load balance their requests across multiple pods. This is called sticky sessions. To enable it for a service:
-
In the Ingress resource, 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.example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:cookie-persistence: "example-session-persistence-cookie"spec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:cookie-persistence: "example-session-persistence-cookie"spec:ingressClassName: haproxyrules:- host: "example.com"http:paths:- path: /pathType: Prefixbackend:service:name: example-serviceport:number: 8080 -
Apply the change with
kubectl apply
:nixkubectl apply -f example-ingress.yamlnixkubectl apply -f example-ingress.yaml
Do you have any suggestions on how we can improve the content of this page?