Sticky sessions don't work

I am using Traefik v2 with Docker Swarm. I want to achieve session stickiness. Below are my docker-compose files.

docker-compose-traefik.yml

version: "3.7"

services:
  traefik:
    image: "traefik:v2.0"
    networks:
      - traefik-net
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker.swarmMode=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:5000"
    ports:
      - "80:80"
      - "9000:8080"
      - "5000:5000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

networks:
  traefik-net:
    external:
      name: traefik-net

docker-compose-whoami.yml

version: "3.7"

services:
  whoami:
    image: "jwilder/whoami"
    networks:
      - traefik-net
    deploy:
      replicas: 3
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.whoami.rule=Host(`mydomain.com`)"
        - "traefik.http.routers.whoami.entrypoints=web"
        - "traefik.http.services.whoami.loadbalancer.server.port=8000"
        - "traefik.http.services.whoami.loadbalancer.sticky=true"
        - "traefik.http.services.whoami.loadbalancer.sticky.cookie.name=StickyCookie"
        - "traefik.http.services.whoami.loadbalancer.sticky.cookie.secure=true"

networks:
  traefik-net:
    external:
      name: traefik-net

I created network traefik-net with: docker network create -d bridge traefik-net .

I deployed both stacks with:

  1. docker-stack deploy -c docker-compose-traefik.yml Traefik
  2. docker-stack deploy -c docker-compose-whoami.yml Whoami

When I visit mydomain.com:5000 it always shows different container ID. It should always route the request to the same container because I enabled session stickiness. I can see the cookie in Chrome developer tools:

What am I doing wrong?

Hello,

have you tested without swarm ?

No, I haven't. But isn't it supposed to work in Docker Swarm as well? Am I missing some configuration? The issue is easily reproducable with provided files so everyone can try it out.

I guess it's a member of your team? https://github.com/containous/traefik/issues/5518

That's me. I use different username here.

EDIT: I changed my username here to match the one on GitHub.

so it works.

traefik.http.services.whoami.loadbalancer.sticky.cookie.secure require HTTPS.

Thank you! Removing this line solved my problem. :slight_smile:

Thank you for this discussion. I had faced a similar issue and am brand new to Traefik -- yesterday was my first day using it - and I'd managed to get my application to mostly work, but it would not successfully scale beyond 1 instance of the service. Adding the sticky cookie did the trick.

See: https://stackoverflow.com/questions/64567065/correctly-handle-socket-io-on-docker-swarm-using-either-traefik-or-nginx/64578737#64578737

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.