【问题标题】:Session Persistence using Traefik on Docker Swarm Replicas在 Docker Swarm 副本上使用 Traefik 的会话持久性
【发布时间】:2018-11-09 17:22:51
【问题描述】:

我尝试使用 traefik 在 dockers-swarm 上实现粘性会话,但我无法在同一台机器上的两个副本上实现会话持久性。 在我的 docker-compose.yml 中,我为 traefik 添加了标签并添加了负载均衡器。下面是我的 docker-compose.yml,(虽然这里的缩进可能看起来不合适,但在实际项目中是正确的)

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

我错过了什么吗?

【问题讨论】:

    标签: docker docker-compose docker-swarm traefik docker-swarm-mode


    【解决方案1】:

    一些事情。

    1. .stickydeprecated 支持 traefik.backend.loadbalancer.stickiness=true
    2. 当您只有一个网络连接到该服务时,我认为您不需要使用 traefik.docker.network 设置网络。
    3. 确保您正在使用使用 cookie 的工具进行测试,这就是粘性会话保持粘性的方式。如果使用 curl,请务必使用 -c-b,如 this example
    4. 我使用了test Swarm setup 中的投票应用程序示例,并向“投票”服务添加了粘性会话,它在单个节点上对我有用。如果使用多节点 swarm,您需要在多个 swarm 节点之前使用 LB 以启用粘性。

    【讨论】:

    • 嗨,如果我们需要在像 traefik.domain.com 这样的子域中拥有 traefik 仪表板,似乎我们需要设置 traefik.docker.network。然后,在我修复了前端规则并为每个服务定义了主机规则后,问题就解决了。
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多