I have one Java based web application which is deployed in jboss-10.1.0(wildfly)
. I am using docker swarm mode(docker version 1.12.1
) to scale my application everything works perfectly but the only issue I am facing is session management.
Now let's take scenario.
I have two instance is running for my application(i.e. App1
and App2
).I am using default load balancer provided by docker swarm mode with nginx
to redirect my application from chintan.test.com:9080
to chintan.test.com:80
so that I don't need to write down port with my url and I am able to access directly with this URL chintan.test.com
.
Now the default load balancer is using RR(Round-Robin algorithm
) to serve my web request.So first time I visit the chintan.test.com
it goes to App1
instance and display login page I login with credentials and everything works perfectly after few minutes it's switch to App2
and again the login page comes.
Is there any way or tools(should be Open-source) through which I handle sessions ? So at least I login to App1
and stick to App1
until I logout.
Thank you!
Session affinity is a feature available on load balancers that allows all subsequent traffic and requests from an initial client session to be passed to the same server in the pool. Session affinity is also referred to as session persistence, server affinity, server persistence, or server sticky.
Persistence—otherwise known as stickiness—is a technique implemented by ADCs to ensure requests from a single user are always distributed to the server on which they started. Some load balancing products and services describe this technique as “sticky sessions”, which is a completely appropriate moniker.
Session persistence is a method to direct all requests originating from a single logical client to a single backend web server. Backend servers that use caching to improve performance, or to enable log-in sessions or shopping carts, can benefit from session persistence.
A server can become overloaded if it accumulates too many sessions, or if specific sticky sessions require a high number of resources. This could result in your load balancer having to shift a client to a different server mid-session, resulting in data loss.
Tried using Nginx and HA-Proxy but none of them seems to be work well in SWARM mode. Then I used Traefik in Docker Swarm and it did the trick for me.The only constraint is that Traefik should run on manager node as it needs to be aware of new worker nodes being added or removed. It doesn't require restart also even if you scale up service, add nodes etc.
I have tested the configuration with Docker compose version 3 which is the latest and deployed using Docker stack deploy.Step by step instructions are over here
To start off you need to create a docker-compose.yml (version 3) and add the load balancer Traefik Image. This is how it looks like
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]
and then the Image for which you need session stickiness
whoami:
image: tutum/hello-world
networks:
- net
ports:
- "80"
deploy:
restart_policy:
condition: any
mode: replicated
replicas: 5
placement:
constraints: [node.role == worker]
update_config:
delay: 2s
labels:
- "traefik.docker.network=test_net"
- "traefik.port=80"
- "traefik.frontend.rule=PathPrefix:/hello;"
- "traefik.backend.loadbalancer.sticky=true"
You can follow this link for a detailed explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With