Haproxy: how to switch back to primary server - haproxy

i am using haproxy for sometime and it perfectly works fine. But i had one query from my team. what if primary server comes back online.. the request should got to primary server instead of backend servers. Below are my configuration details. Please help me the resolve the issue.
basically in below configuration my request are going to primary server that is Server1 and Server2 in a roundrobin way. If both the primary server goes down it switches to backup server that is Server3 and Server4. Now if both the Primary server becomes live i want all the request should go to the primary server and not to backup server. How can i do this configuration? help will be highly appreciated.
frontend Local_Server
bind ssbbct1076:39250
mode http
default_backend Web_Server
backend Web_Server
balance roundrobin
option httplog
option log-health-checks
option allbackups
server Server1 s2bbct01:39249 check
server Server2 s2bbct02:39249 check
server Server3 c1bbct01:39249 check backup
server Server4 c1bbct02:39249 check backup

you can use option redispatch to redispatch a request to the active servers

Related

HAProxy : Prevent stickiness to a backup server

I'm facing a configuration issue with HAProxy (1.8).
Context:
In a HAProxy config, I have a several severs in a backend, and an additional backup server in case the other servers are down.
Once a client gets an answer from a server, it must stick to this server for its next queries.
For some good reasons, I can't use a cookie for this concern, and I had to use a stick-table instead.
Problem:
When every "normal" server is down, clients are redirected to the backup server, as expected.
BUT the stick-table is then filled with an association between the client and the id of the backup server.
AND when every "normal" server is back, the clients which are present in the stick table and associated with the id of the backup server will continue to get redirected to the backup server instead of the normal ones!
This is really upsetting me...
So my question is: how to prevent HAProxy to stick clients to a backup server in a backend?
Please find below a configuration sample:
defaults
option redispatch
frontend fe_test
bind 127.0.0.1:8081
stick-table type ip size 1m expire 1h
acl acl_test hdr(host) -i whatever.domain.com
...
use_backend be_test if acl_test
...
backend be_test
mode http
balance roundrobin
stick on hdr(X-Real-IP) table fe_test
option httpchk GET /check
server test-01 server-01.lan:8080 check
server test-02 server-02.lan:8080 check
server maintenance 127.0.0.1:8085 backup
(I've already tried to add a lower weight to the backup server, but it didn't solve this issue.)
I read in the documentation that the "stick-on" keyword has some "if/unless" options, and maybe I can use it to write a condition based on the backend server names, but I have no clue about the syntax to use, or even if it is possible.
Any idea is welcome!
So silly of me! I was so obsessed by the stick table configuration that I didn't think to look in the server options...
There is a simple keyword that perfectly solves my problem: non-stick
Never add connections allocated to this sever to a stick-table. This
may be used in conjunction with backup to ensure that stick-table
persistence is disabled for backup servers.
So the last line of my configuration sample simply becomes:
server maintenance 127.0.0.1:8085 backup non-stick
...and everything is now working as I expected.

haproxy stick to group of backend servers

So I am struggling to find the correct config for my haproxy:
I have Ruby on Rails web application which is served by two physical hosts, each having 3 workers. The two hosts each have a database, and the two databases are synchronised in real time.
I am trying to have sessions stick to the same host, so requests are still load balanced across the 3 workers in each host.
The objective is to avoid two consecutive requests from the same client being sent to different hosts.
My config looks like this:
frontend web_front
bind *:4100
default_backend web_back
backend web_back
cookie SERVERID insert indirect nocache
balance roundrobin
server host_1_web_1 129.168.0.1:3000 maxconn 1 check cookie SRV_1
server host_1_web_2 129.168.0.1:3001 maxconn 1 check cookie SRV_1
server host_1_web_3 129.168.0.1:3002 maxconn 1 check cookie SRV_1
server host_2_web_1 129.168.0.2:3000 maxconn 1 check cookie SRV_2
server host_2_web_2 129.168.0.2:3001 maxconn 1 check cookie SRV_2
server host_2_web_3 129.168.0.2:3002 maxconn 1 check cookie SRV_2
As you can see, I have set the cookie of each host to the same value, hopping that requests would be still load balanced properly accross workers, but now only the first worker of each host seems to be getting requests.
Is there a way around this? Perhaps using sticky-tables?
If I am correctly understanding you requirements, you want two different levels of load balancing:
1. Server load balancing using session persistence
2. Worker load balancing without session persistence.
One solution would be to have a service in the server side listening for HAProxy connections and doing the load balance across the workers.
But you still can do this with HAProxy by using a dummy backend:
frontend web_front
bind *:4100
default_backend web_back
backend web_back
cookie SERVERID insert indirect nocache
balance roundrobin
server server1 127.0.0.1:3001 cookie SRV_1
server server2 127.0.0.1:3002 cookie SRV_2
listen lt_srv1
bind 127.0.0.1:3001
server host_1_web_1 129.168.0.1:3000 check
server host_1_web_2 129.168.0.1:3001 check
server host_1_web_3 129.168.0.1:3002 check
listen lt_srv2
bind 127.0.0.1:3002
server host_2_web_1 129.168.0.2:3000 check
server host_2_web_2 129.168.0.2:3001 check
server host_2_web_3 129.168.0.2:3002 check

haproxy setup issue

I am trying to setup haproxy on EC2 instance but facing below error:
503 Service Unavailable. No server is available to handle this
request.
Any help is highly appreciated. I tried many ways but all in vain.
My haproxy version is 1.5 and this is haproxy.cfg file :
frontend main
bind *:80
default_backend server
backend server
balance roundrobin
server node1 xx.xx.xx.xx:80 check maxconn 32
server node2 xx.xx.xx.xx:80 check maxconn 32
Probably the config file you shared is not complete. It should contain mode http in frontend and backend server if not mentioned in global settings.
Also check if you can access the webserver, it is up and running.
Allow the connection on webserver through firewall.
You can also share full config file so exact issue can be identified.
Hope this helps!

HAProxy - Configuring with prefered server

I am using HAProxy for load balancing my application on RHEL7. I have two servers server1 and server2, of which I want server1 as prefered server. My requirement is: server1 should serve all requests by default; if server1 fails, server2 should be active; when server1 is up, server1 should become active and should process requests. Following is my frontend/backend in haproxy.conf:
frontend frontend_2143
bind *:2143
default_backend backend_2143
backend backend_2143
balance roundrobin
mode tcp
server server1 192.160.0.3:2143 check weight 255
server server2 192.160.0.4:2143 check
With this configuration I am receiving all my request at server1 at begining, and at server2 after server1 is down, but when server1 is up, the requests are still getting received at server2.
Can anyone help here?
Try to use the "backup" directive in your config.
See detailed explanation here: http://blog.haproxy.com/2013/12/23/failover-and-worst-case-management-with-haproxy/
And maybe you should check this too: https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4.2-option%20prefer-last-server

HAProxy - Request getting Broadcast to every server

I am hosting two different application versions on same servers on different ports. In basic version i expect that following configuration should send request in RoundRobin fashion to different ports. But what i am observing is the request is getting broadcasted to ALL of my server endpoints. Meaning in below example my main request to port 8080 gets FWD to both www.myappdemo.com:5001 and www.myappdemo.com:5002... although the response send by proxy is ALWAYS from www.myappdemo.com:5001.
Can anyone tell what is wrong here?
global
debug
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:8080
default_backend servers
backend servers
balance roundrobin
server svr_50301 www.myappdemo.com:5001 maxconn 32 check
server svr_50302 www.myappdemo.com:5002 maxconn 32 check
i can advise you to enable logs and web interface, after that you can provide us more logs and you can check in web interface also if haproxy detects you second server(svr_50302) to be alive.
Reference to HAProxy 1.5 Doc's :
Web Interface - http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4.2-stats%20admin
Good info how to enable login - http://webdevwonders.com/haproxy-load-balancer-setup-including-logging-on-debian/
Best Regards,
Dani