Session Persistence using Traefik on Docker Swarm Replicas











up vote
1
down vote

favorite












I trying to implement sticky session on dockers-swarm with traefik, but I could not achieve session persistence over two replicas on same machine.
In my docker-compose.yml, I have added labels for traefik and added the loadbalancer as well. Below is my docker-compose.yml, (Although the indentation might not look proper here, but it correct in actual project)



version: '3'

services:
web:
image: php:7.2.11-apache-stretch
ports:
- "8080:80"
volumes:
- ./code/:/var/www/html/hello/
stdin_open: true
tty: true
deploy:
mode: replicated
replicas: 2
restart_policy:
condition: any
update_config:
delay: 2s
labels:
- "traefik.docker.network=docker-test_privnet"
- "traefik.port=80"
- "traefik.backend.loadbalancer.sticky=true"
- "traefik.frontend.rule=PathPrefix:/hello"

networks:
- privnet
loadbalancer:
image: traefik
command:
--docker
--docker.swarmmode
--docker.watch
--web
--loglevel=DEBUG
ports:
- 80:80
- 9090:8080
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
restart_policy:
condition: any
mode: replicated
replicas: 1
update_config:
delay: 2s
placement:
constraints: [node.role == manager]

networks:
- privnet
networks:
privnet:
external: true


Am I missing anything?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I trying to implement sticky session on dockers-swarm with traefik, but I could not achieve session persistence over two replicas on same machine.
    In my docker-compose.yml, I have added labels for traefik and added the loadbalancer as well. Below is my docker-compose.yml, (Although the indentation might not look proper here, but it correct in actual project)



    version: '3'

    services:
    web:
    image: php:7.2.11-apache-stretch
    ports:
    - "8080:80"
    volumes:
    - ./code/:/var/www/html/hello/
    stdin_open: true
    tty: true
    deploy:
    mode: replicated
    replicas: 2
    restart_policy:
    condition: any
    update_config:
    delay: 2s
    labels:
    - "traefik.docker.network=docker-test_privnet"
    - "traefik.port=80"
    - "traefik.backend.loadbalancer.sticky=true"
    - "traefik.frontend.rule=PathPrefix:/hello"

    networks:
    - privnet
    loadbalancer:
    image: traefik
    command:
    --docker
    --docker.swarmmode
    --docker.watch
    --web
    --loglevel=DEBUG
    ports:
    - 80:80
    - 9090:8080
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    deploy:
    restart_policy:
    condition: any
    mode: replicated
    replicas: 1
    update_config:
    delay: 2s
    placement:
    constraints: [node.role == manager]

    networks:
    - privnet
    networks:
    privnet:
    external: true


    Am I missing anything?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I trying to implement sticky session on dockers-swarm with traefik, but I could not achieve session persistence over two replicas on same machine.
      In my docker-compose.yml, I have added labels for traefik and added the loadbalancer as well. Below is my docker-compose.yml, (Although the indentation might not look proper here, but it correct in actual project)



      version: '3'

      services:
      web:
      image: php:7.2.11-apache-stretch
      ports:
      - "8080:80"
      volumes:
      - ./code/:/var/www/html/hello/
      stdin_open: true
      tty: true
      deploy:
      mode: replicated
      replicas: 2
      restart_policy:
      condition: any
      update_config:
      delay: 2s
      labels:
      - "traefik.docker.network=docker-test_privnet"
      - "traefik.port=80"
      - "traefik.backend.loadbalancer.sticky=true"
      - "traefik.frontend.rule=PathPrefix:/hello"

      networks:
      - privnet
      loadbalancer:
      image: traefik
      command:
      --docker
      --docker.swarmmode
      --docker.watch
      --web
      --loglevel=DEBUG
      ports:
      - 80:80
      - 9090:8080
      volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      deploy:
      restart_policy:
      condition: any
      mode: replicated
      replicas: 1
      update_config:
      delay: 2s
      placement:
      constraints: [node.role == manager]

      networks:
      - privnet
      networks:
      privnet:
      external: true


      Am I missing anything?










      share|improve this question













      I trying to implement sticky session on dockers-swarm with traefik, but I could not achieve session persistence over two replicas on same machine.
      In my docker-compose.yml, I have added labels for traefik and added the loadbalancer as well. Below is my docker-compose.yml, (Although the indentation might not look proper here, but it correct in actual project)



      version: '3'

      services:
      web:
      image: php:7.2.11-apache-stretch
      ports:
      - "8080:80"
      volumes:
      - ./code/:/var/www/html/hello/
      stdin_open: true
      tty: true
      deploy:
      mode: replicated
      replicas: 2
      restart_policy:
      condition: any
      update_config:
      delay: 2s
      labels:
      - "traefik.docker.network=docker-test_privnet"
      - "traefik.port=80"
      - "traefik.backend.loadbalancer.sticky=true"
      - "traefik.frontend.rule=PathPrefix:/hello"

      networks:
      - privnet
      loadbalancer:
      image: traefik
      command:
      --docker
      --docker.swarmmode
      --docker.watch
      --web
      --loglevel=DEBUG
      ports:
      - 80:80
      - 9090:8080
      volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      deploy:
      restart_policy:
      condition: any
      mode: replicated
      replicas: 1
      update_config:
      delay: 2s
      placement:
      constraints: [node.role == manager]

      networks:
      - privnet
      networks:
      privnet:
      external: true


      Am I missing anything?







      docker docker-compose docker-swarm traefik docker-swarm-mode






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 17:22









      Swastik Roy

      749




      749
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          A few things.





          1. .sticky is deprecated in favor of traefik.backend.loadbalancer.stickiness=true

          2. I don't think you need to set the network with traefik.docker.network when you only have a single network connected to that service.

          3. Make sure you're testing with a tool that uses cookies, which is how sticky sessions stay sticky. If using curl, be sure to use -c and -b as in this example.

          4. I used the voting app sample from my test Swarm setup and added sticky sessions to the "vote" service and it worked for me on a single node. If using a multi-node swarm you'll need the LB in front of multiple swarm nodes to also enable sticky.






          share|improve this answer

















          • 1




            Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
            – Swastik Roy
            Nov 18 at 4:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230548%2fsession-persistence-using-traefik-on-docker-swarm-replicas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          A few things.





          1. .sticky is deprecated in favor of traefik.backend.loadbalancer.stickiness=true

          2. I don't think you need to set the network with traefik.docker.network when you only have a single network connected to that service.

          3. Make sure you're testing with a tool that uses cookies, which is how sticky sessions stay sticky. If using curl, be sure to use -c and -b as in this example.

          4. I used the voting app sample from my test Swarm setup and added sticky sessions to the "vote" service and it worked for me on a single node. If using a multi-node swarm you'll need the LB in front of multiple swarm nodes to also enable sticky.






          share|improve this answer

















          • 1




            Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
            – Swastik Roy
            Nov 18 at 4:27















          up vote
          1
          down vote













          A few things.





          1. .sticky is deprecated in favor of traefik.backend.loadbalancer.stickiness=true

          2. I don't think you need to set the network with traefik.docker.network when you only have a single network connected to that service.

          3. Make sure you're testing with a tool that uses cookies, which is how sticky sessions stay sticky. If using curl, be sure to use -c and -b as in this example.

          4. I used the voting app sample from my test Swarm setup and added sticky sessions to the "vote" service and it worked for me on a single node. If using a multi-node swarm you'll need the LB in front of multiple swarm nodes to also enable sticky.






          share|improve this answer

















          • 1




            Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
            – Swastik Roy
            Nov 18 at 4:27













          up vote
          1
          down vote










          up vote
          1
          down vote









          A few things.





          1. .sticky is deprecated in favor of traefik.backend.loadbalancer.stickiness=true

          2. I don't think you need to set the network with traefik.docker.network when you only have a single network connected to that service.

          3. Make sure you're testing with a tool that uses cookies, which is how sticky sessions stay sticky. If using curl, be sure to use -c and -b as in this example.

          4. I used the voting app sample from my test Swarm setup and added sticky sessions to the "vote" service and it worked for me on a single node. If using a multi-node swarm you'll need the LB in front of multiple swarm nodes to also enable sticky.






          share|improve this answer












          A few things.





          1. .sticky is deprecated in favor of traefik.backend.loadbalancer.stickiness=true

          2. I don't think you need to set the network with traefik.docker.network when you only have a single network connected to that service.

          3. Make sure you're testing with a tool that uses cookies, which is how sticky sessions stay sticky. If using curl, be sure to use -c and -b as in this example.

          4. I used the voting app sample from my test Swarm setup and added sticky sessions to the "vote" service and it worked for me on a single node. If using a multi-node swarm you'll need the LB in front of multiple swarm nodes to also enable sticky.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 19:46









          Bret Fisher

          3,18111321




          3,18111321








          • 1




            Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
            – Swastik Roy
            Nov 18 at 4:27














          • 1




            Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
            – Swastik Roy
            Nov 18 at 4:27








          1




          1




          Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
          – Swastik Roy
          Nov 18 at 4:27




          Hi, Seems like we need to set traefik.docker.network if we need to have the traefik dashboard in a subdomain like traefik.domain.com. Then, the issue was solved after I fixed my front end rules and defined Host rule for each service.
          – Swastik Roy
          Nov 18 at 4:27


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230548%2fsession-persistence-using-traefik-on-docker-swarm-replicas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini