Example Nginx plus ingress for sticky sessions during canary deployment

470 Views Asked by At

I’m deploying 2 services to kubernetes pods which simply echo a version number; echo-v1 & echo-v2

Where echo-v2 is considered the canary deployment, I can demonstrate sticky sessions as canary weight is reconfigured from 0 to 100 using canary & canary-weight annotations.

2 ingresses are used: The first routes to echo-v1 with a session cookie annotation. The second routes to echo-v2 with canary true,canary weight and session cookie annotations.

The second ingress I can apply without impacting those sessions started on the first ingress and new sessions follow the canary weighting as expected.

However I’ve since learned that those annotations are for nginx community and won’t work with nginx plus.

How can I achieve the same using ingress(es) with nginx plus?

1

There are 1 best solutions below

0
On

This is the ingress configuration that works for me using Nginx community vs Nginx plus.

Nginx community:

(coffee-v1 service)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/affinity: "cookie"
  name: ingress-coffee
spec:
  rules:
    - http:
        paths:
          - path: /coffee
            pathType: Exact
            backend:
              service:
                name: coffee-v1
                port:
                  number: 80

(coffee-v2 'canary' service)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "100"
  name: ingress-coffee-canary
spec:
  rules:
    - http:
        paths:
          - path: /coffee
            pathType: Exact
            backend:
              service:
                name: coffee-v2
                port:
                  number: 80

Nginx plus:

(coffee-v1 & coffee-v2 as type 'virtualserver' not 'ingress')

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: cafe
spec:
  host: cloudbees-training.group.net
  tls:
    secret:  cloudbees-trn.aks.group.net-tls
  upstreams:
    - name: coffee-v1
      service: coffee-v1-svc
      port: 80
      sessionCookie:
        enable: true
        name: srv_id_v1
        path: /coffee
        expires: 2h
    - name: coffee-v2
      service: coffee-v2-svc
      port: 80
      sessionCookie:
        enable: true
        name: srv_id_v2
        path: /coffee
        expires: 2h
  routes:
    - path: /coffee
      matches:
        - conditions:
            - cookie: srv_id_v1
              value: ~*
          action:
            pass: coffee-v1
        - conditions:
            - cookie: srv_id_v2
              value: ~*
          action:
            pass: coffee-v2
# 3 options to handle new session below:
#
# 1) All new sessions to v1:
#      action:
#        pass: coffee-v1
#
# 2) All new sessions to v2:
#      action:
#        pass: coffee-v2
#
# 3) Split new sessions by weight
#    Note: 0,100 / 100,0 weightings causes sessions 
#    to drop for the 0 weighted service:
#      splits:
#        - weight: 50
#          action:
#            pass: coffee-v1
#        - weight: 50
#          action:
#            pass: coffee-v2