0

I'm trying to enable sticky session. I got it working with NodePort but it's not working with ingress.

Environment: BareMetal, Ubuntu, MetalLB, Ingress with nginx.

Details:

  • I installed MetalLB then ingress from the official documentation.
  • then I applied this deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: what-is-my-pod-deployment
  namespace: test
  labels:
    app: what-is-my-pod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: what-is-my-pod
  template:
    metadata:
      labels:
        app: what-is-my-pod
    spec:
      containers:
      - name: what-is-my-pod
        image: ovhplatform/what-is-my-pod:1.0.1
        ports:
        - containerPort: 8080
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  labels:
    app: what-is-my-pod
  name: what-is-my-pod
spec:
  type: ClusterIP
  sessionAffinity: ClientIP
  ports:
 - port: 8080
  selector:
    app: what-is-my-pod
  • then I applied the ingress file
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "test"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/affinity-mode: persistent
    nginx.ingress.kubernetes.io/session-cookie-hash: sha1
  name: ingress
  namespace: pos
spec:
  rules:
 - host: test.****.com
    http:
      paths:
      - backend:
          service:
            name: what-is-my-pod
            port:
              number: 8080
        path: /
        pathType: Prefix
  • now when I test it it doesn't work:
$ curl --cookie cookie.txt --cookie-jar cookie.txt http://test.****.com
Hello "what-is-my-pod-deployment-7f54b98b98-hh2vp"!

$ curl --cookie cookie.txt --cookie-jar cookie.txt http://test.****.com
Hello "what-is-my-pod-deployment-7f54b98b98-mv9sl"!

$ curl --cookie cookie.txt --cookie-jar cookie.txt http://test.****.com
Hello "what-is-my-pod-deployment-7f54b98b98-cckz5"!

But when I change the service type to NodePort

apiVersion: v1
kind: Service
metadata:
  namespace: test
  labels:
    app: what-is-my-pod
  name: what-is-my-pod
spec:
  type: NodePort
  sessionAffinity: ClientIP
  ports:
 - port: 8080
  selector:
    app: what-is-my-pod

it works!

$ curl --cookie cookie.txt --cookie-jar cookie.txt http://***.***.***.***:31545
Hello "what-is-my-pod-deployment-7f54b98b98-hh2vp"!

$ curl --cookie cookie.txt --cookie-jar cookie.txt http://***.***.***.***:31545
Hello "what-is-my-pod-deployment-7f54b98b98-hh2vp"!

$ curl --cookie cookie.txt --cookie-jar cookie.txt http://***.***.***.***:31545
Hello "what-is-my-pod-deployment-7f54b98b98-hh2vp"!
Eslam Ali
  • 71
  • 8

0 Answers0