skip to Main Content

i’m running a flask-socketio behind an ingress.
here’s the config:

socketio = SocketIO(app, message_queue='redis://redis-server.domain:6379')

in the dockerfile, it runs like so:

CMD ["gunicorn", "-k", "geventwebsocket.gunicorn.workers.GeventWebSocketWorker", "-w", "4", "--bind", "0.0.0.0:2731", "app:app"]

and the ingress is configured like this:

nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/enable-ssl-passthrough: "true"
nginx.ingress.kubernetes.io/session-cookie-name: "route"

i also tried replacing "cookie" with "ip_hash"

whenever i add a second replica pod, it starts returning error 400, as if it contacts the pods interchangeably, and not sticking to the same server, thus breaking the socket.

any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    i've selected Harsh Manvar's ansewer as the correct one, but adding details for future reference:

    in the service, sessionAffinity: ClientIP as suggested.

    the Run command in dockerfile:

    CMD ["gunicorn", "-k", "geventwebsocket.gunicorn.workers.GeventWebSocketWorker", "-w", "1", "--bind", "0.0.0.0:6138", "--timeout", "180", "app:app"]
    

    the requirements.txt:

    gunicorn
    simple-websocket
    gevent-websocket
    gevent
    

    the socketio:

    socketio = SocketIO(app, message_queue='redis://redis-service.redis:6379')
    

    added to ingress:

    nginx.ingress.kubernetes.io/enable-ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/add-base-url: "true"
    nginx.ingress.kubernetes.io/session-cookie-name: "route"
    nginx.ingress.kubernetes.io/websocket-services: service-name
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
    

  2. Ideally, it should have worked but try this .spec.sessionAffinity

    Set it to service

    .spec.sessionAffinity value will be ClientIP

    Service to POD sticky session

    kind: Service
    apiVersion: v1
    metadata:
      name: test-service
    spec:
      selector:
        app: test-app
      ports:
      - name: http
        protocol: TCP
        port: 80
        targetPort: 80
      sessionAffinity: ClientIP
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search