1. 程式人生 > >Use HaProxy for load balancing multiple endpoints Socket.IO

Use HaProxy for load balancing multiple endpoints Socket.IO

The recent time i very busy because i have some projects and problems need to take care, one of them is improve availability of system. I began to find solutions for this problem and i focus to “Load balancing” for system, i use HaProxy, Redis with Socket.IO to build load balancer system. As you know HaProxy is very 

fast and reliable solution offering high availabilityload balancing, and proxying for TCP and HTTP-based applications. Some test case about performance of HaProxy, it so great and it can achieve more 2 million concurrent connection (base on TCP connection). HaProxy is used in many game online system to support load balancing for millions user with real time connection. Today i will guide to you how to install, setup and deploy HaProxy to “load balancing” for multiple nodes run socket.io. I make demo so easy with nodejs socketio server and web socket.io-client.

I will test follow the below model

architech-testI have four servers with

Test1 : 4 core, 16GB Ram Test2 : 4 core, 16GB Ram Test3 : 6 core, 32GB Ram Test4 : 4 core, 16 GB Ram

Ok, let go to build simple example to test HaProxy

Step 1 : Install HaProxy on Test4 server

* yum install haproxy

Step 2 : Open configuration file “/etc/haproxy/haproxy.cfg” and add the below content

frontend http-in    bind *:9199    default_backend socketio-nodes    stats enable    stats uri /?stats    stats auth admin:admin

backend socketio-nodes    option forwardfor    option http-server-close    option forceclose    no option httpclose

   balance url_param session_id check_post 64    cookie SID insert indirect nocache    server node1 test1.azstack.com:9100 weight 1 maxconn 1024 check cookie node1    server node2 test2.azstack.com:9100 weight 1 maxconn 2048 check cookie node2    server node3 test3.azstack.com:9100 weight 2 maxconn 4096 check cookie node3

You need to note a few configuration:

balance url_param session_id check_post 64is selection criteria for load distribution. weight 1: weight of very nodes. maxconn 1024: max concurrent connect on the node.

Step 3 : Start HaProxy 

* /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg 

Endpoint of HaProxy : test4.azstack.com:9199

Step 4 : Create simple socket.io server and listen on port 9100 

duplicate socket.io server instance for the other two nodes and run it

start_socketio_server

* node socketio-server.js 

Step 5 : Create simple websocket client to connect. 

Step 6 : Try to connect to HaProxy (Invite your friends join to test)

HaProxy endpoint (test4.azstack.com:9199)

client-connect

Step 7 : Check log on socket.io server

my connection

have_new_connection_node2

and more connection from my friend

have_new_connection_note3have_new_connection_node1

Ok, you can see ip client allway is 128.199.123.119, that great because all packet is forwarded by HaProxy.

Some advance topics for you

  • How to setup SSL for HaProxy.
  • How to use Redis to run cluster socket.io server.

Maybe i can write above problem, thank for reading. Have a nice day.

Advertisements

Like this:

Like Loading...