Montoya
Montoya

Reputation: 3049

Kubernetes Ingress - Load balancer traffic split

I have a kubernetes ingress of class nginx and two load balancers. Running on GKE v1.17.

Sample ingress yaml:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    kubernetes.io/ingress.class: "nginx"
    # Enable client certificate authentication
    nginx.ingress.kubernetes.io/auth-tls-verify-client: "on"
    # Create the secret containing the trusted ca certificates
    nginx.ingress.kubernetes.io/auth-tls-secret: "production/client-cert-secret"
    # Specify the verification depth in the client certificates chain
    nginx.ingress.kubernetes.io/auth-tls-verify-depth: "1"
    # Automatically redirect http to https
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    # Use regex in paths
    nginx.ingress.kubernetes.io/use-regex: "true"
    # Allow larger request body
    nginx.ingress.kubernetes.io/proxy-body-size: 30m
    # For notifications we add the proxy headers
    nginx.ingress.kubernetes.io/configuration-snippet: |  
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
spec:
  tls:
    - hosts:
      - my-domain.com
      secretName: my-tls-certificate
  rules:
  - host: my-domain.com
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: load-balancer-1
          servicePort: 443

I wish to split the traffic reached to the ingress between the two load balancers. For example:

load-balancer-1 will receive 90% of the traffic

load-balancer-2 will receive 10% of the traffic

How can I do that with kubernetes ingress?

Upvotes: 1

Views: 4648

Answers (1)

danielorn
danielorn

Reputation: 6167

The nginx ingress controller supports canary deployments through the Canary Annotations

In some cases, you may want to "canary" a new set of changes by sending a small number of requests to a different service than the production service. The canary annotation enables the Ingress spec to act as an alternative service for requests to route to depending on the rules applied. The following annotations to configure canary can be enabled after nginx.ingress.kubernetes.io/canary: "true" is set:

  • nginx.ingress.kubernetes.io/canary-weight: The integer based (0 - 100) percent of random requests that should be routed to the service specified in the canary Ingress. A weight of 0 implies that no requests will be sent to the service in the Canary ingress by this canary rule. A weight of 100 means implies all requests will be sent to the alternative service specified in the Ingress.

Note that when you mark an ingress as canary, then all the other non-canary annotations will be ignored (inherited from the corresponding main ingress) except nginx.ingress.kubernetes.io/load-balance and nginx.ingress.kubernetes.io/upstream-hash-by.

Known Limitations

Currently a maximum of one canary ingress can be applied per Ingress rule.

In other words, you can introduce a new Ingress Object my-ingress-canary where you set the annotations

  • nginx.ingress.kubernetes.io/canary: "true" (Tells Nginx Ingress to mark this one as “Canary” and associate this ingress with the main ingress by matching host and path.

  • nginx.ingress.kubernetes.io/canary-weight: "10" (Route ten percent traffic to load-balancer-2)

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress-canary
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
  rules:
  - host: my-domain.com
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: load-balancer-2
          servicePort: 443

Upvotes: 7

Related Questions