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:

  1. Edit the Ingress resource and set the load-balance annotation to your chosen algorithm. Below, we set the service to use the random algorithm:

    example-ingress.yaml
    yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: example-ingress
    annotations:
    load-balance: random
    spec:
    ingressClassName: haproxy
    rules:
    - host: "example.com"
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: example-service
    port:
    number: 8080
    example-ingress.yaml
    yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: example-ingress
    annotations:
    load-balance: random
    spec:
    ingressClassName: haproxy
    rules:
    - host: "example.com"
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: example-service
    port:
    number: 8080
  2. Apply the change with kubectl apply:

    nix
    kubectl apply -f example-ingress.yaml
    nix
    kubectl 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:

  1. 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.yaml
    yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: example-ingress
    annotations:
    cookie-persistence: "example-session-persistence-cookie"
    spec:
    ingressClassName: haproxy
    rules:
    - host: "example.com"
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: example-service
    port:
    number: 8080
    example-ingress.yaml
    yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: example-ingress
    annotations:
    cookie-persistence: "example-session-persistence-cookie"
    spec:
    ingressClassName: haproxy
    rules:
    - host: "example.com"
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: example-service
    port:
    number: 8080
  2. Apply the change with kubectl apply:

    nix
    kubectl apply -f example-ingress.yaml
    nix
    kubectl apply -f example-ingress.yaml

Do you have any suggestions on how we can improve the content of this page?