与docker swarm进行的粘性会话 [英] Sticky Sessions with docker swarm

查看:118
本文介绍了与docker swarm进行的粘性会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用socket.io的dockerized node.js应用

I have a dockerized node.js app that uses socket.io

通过docker撰写,我在同一服务器上的两个不同容器上为该应用程序运行了2个副本.但是,以轮循方式分发到达服务器的请求.

Via docker compose I run 2 replicas for the app on two diffrent containers on the same server. However the requests that come to the servers is distributed in a round robin fashion.

有没有一种方法可以用来实现粘性会话?

Is there a way that I can use to achieve sticky sessions ?

我的docker-compose.yml如下所示

my docker-compose.yml looks like below

version: '3'

services:
 app:
   ports:
     - "3001:3001"
   image: image
   deploy:
     replicas: 2

并且我使用docker stack deploy运行该应用程序的两个副本

and I use the docker stack deploy to run the two replicas for the app

推荐答案

据我了解,存在3种可能的变体来在Docker或Docker Swarm上实现会话亲和力.

As far as I understand, there are 3 possible variations to implement session affinity on Docker or Docker Swarm.

1.添加标签以建立粘性会话,如果您使用的是Docker Enterprise Edition,这是一种非常简单且正式的方法.

1. Add labels to establish a sticky session, which is the very simple and official way if you are using Docker Enterprise Edition.

labels:
   - "com.docker.lb.hosts=example.com"
   - "com.docker.lb.sticky_session_cookie=session" 
   - "com.docker.lb.port=3001"

除非您已购买使用Docker EE的许可证,否则您需要使用以下两种方法之一.

2.使用Traefik负载均衡器.让我们假设部署名为app的服务.容器部署在Docker Swarm中连接的节点192.168.0.1和192.168.0.2中.

2. Use Traefik load balancer. Let's assume deploying a service named app; containers are deployed in 192.168.0.1 and 192.168.0.2 which are the nodes connected in Docker Swarm.

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"
  app:
    image: nginx
    deploy:
      replicas: 2
    networks:
      - traefik-net
    labels:
       - "traefik.enable=true"
       - "traefik.http.routers.app.entrypoints=app"
       - "traefik.http.routers.app.rule=Host(`192.168.0.1`) || Host(`192.168.0.2`)"
       - "traefik.http.services.app-service.loadBalancer.sticky.cookie=true"
       - "traefik.http.services.app-service.loadBalancer.sticky.cookie.name=app_cookie_name"
       - "traefik.http.services.app-service.loadbalancer.server.port=80"
    
networks:
  traefik-net:
    external: true
    name: traefik-net

3.使用Nginx反向代理.假设容器将部署在Docker Swarm中连接的节点192.168.0.1和192.168.0.2中.

3. Use Nginx reverse proxy. Let's assume containers will deploy in 192.168.0.1 and 192.168.0.2 which are the nodes connected in Docker Swarm.

为了绕过路由网格,您需要使用一个副本部署两个服务(app1,app2),因为Docker Swarm的默认负载平衡方法是轮询.

In order to bypass the routing mesh, you need to deploy two services (app1, app2) with one replica because Docker Swarm's default load balance method is round-robin.

version: "3.8"

services:
 app1:
   ports:
     - "3001:3001"
   image: image
   deploy:
     replicas: 1
 app2:
   ports:
     - "3002:3001"
   image: image
   deploy:
     replicas: 1

Nginx配置:您需要注册每种方法才能到达容器,因为Docker Swarm使用路由网格(循环负载均衡器).

Nginx configuration: You need to register each way to reach out to a container because Docker Swarm uses the routing mesh (round-robin load balancer).

http {
    upstream example {
        server 192.168.0.1:3001;
        server 192.168.0.1:3002;
        server 192.168.0.2:3001;
        server 192.168.0.2:3002;
    }

    server {
        listen          80;
        server_name     example.com;
    
        location / {
            proxy_pass http://example/;
        }
    }
}

这篇关于与docker swarm进行的粘性会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆