'Docker Swarm sticky sessions with traefik

I am trying to set on my Raspberry Pi cluster (4 nodes) 3 WordPress replicas and 1 database. That works, but when I'm trying to enter in WordPress every time I log in, I refresh or interact with WordPress, that's because Docker swarm's routing mesh

I'm trying to set sticky sessions to solve this problem. I investigated 3 days in many pages, but I didn't find anything helpful. I'm doing a degree project and I'm getting stuck in this problem. I'd appreciate if someone can help me and give me a example with this code below.

version: "3.8"

services:
  traefik:
    image: traefik:v2.3
    deploy:
      mode: global
    networks:
      - traefik-net
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.app.address=:80"
    ports:
      - 3001:80
      - 8080:8080
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    deploy:
      replicas: 1
      placement:
        constraints:
          - "node.role==manager"

  db:
    depends_on:
      - traefik
    image: mariadb
    restart: always
    networks:
      - traefik-net
    volumes:
      - mariadb:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=1234
    ports:
      - 3306:3306
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - "node.labels.ID==wp"

  web:
    image: wordpress
    depends_on:
      - db
    volumes:
      - wordpress:/var/www/html
    environment:
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=1234
      - WORDPRESS_DB_HOST=db
    ports:
      - 8181:80
    deploy:
      mode: replicated
      replicas: 2
      placement:
        constraints:
          - "node.labels.ID==wp"
    networks:
      - traefik-net
    labels:
      - "traefik.enable: true"
      - "traefik.http.services.wp.loadbalancer.sticky.cookie: true"

volumes:
  mariadb:
  wordpress:

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

The tasks running!



Solution 1:[1]

You seem to miss a lot of labels. And your labels have also the incorrect form, since you have colons instead of equal signs.

Here is an example configuration:

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

  whoami:
    image: "traefik/whoami"
    deploy:
      replicas: 3
      endpoint_mode: dnsrr
      labels:
        - "traefik.enable=true"
        - "traefik.port=80"
        - "traefik.http.routers.whoami.entrypoints=web"
        - "traefik.http.routers.whoami.rule=PathPrefix(`/`)"
        - "traefik.http.services.whoami.loadbalancer.sticky=true"
        - "traefik.http.services.whoami.loadbalancer.sticky.cookie.name=REPLICA_ADDRESS"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1