Sticky Sessions with Wordpress

(First of all sorry for my bad english)

Hello everyone! I am trying to set on my rasperry pi cluster (4 nodes) 3 wordpress replicas and 1 database.

That works but when I'm trying to enter in wordpress I log in everytime 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 helpfull.

I'm doing a degree project and I'm getting stuck in this problem. I'll apreciatte if someone can help me and give me a example with this code below.

version: '3'

services:
  db:
    image: mariadb
    networks:
      - wpnetwork
    volumes:
      - data:/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.hostname==worker3"

  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:
      - 8080:80
    deploy:
      mode: replicated
      replicas: 3
      placement:
        constraints:
          - "node.role==worker"
    networks:
      - wpnetwork

volumes:
  data:
  wordpress:

networks:
  wpnetwork:
    driver: overlay

Hello @codeco1978,

To enable stick sessions, did you try looking in the documentation ?

You might need to add the label to enable it:

    traefik.http.services.wp.loadbalancer.sticky.cookie: true

Thanks for your message Tom! :smile:
I have tried to do what is shown in the .yml file below but it still gives the same problem with sessions. I'm missing something?

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

If you access your wp instance through localhost:8081, it does not go through Traefik, you are accessing it directly.

I suggest you to follow this user guide on how to do a basic web app with Traefik : Traefik Docker Documentation - Traefik

1 Like