Debian Codegraph @

docs/deploy/haproxy.rst

raw
1 Deploy behind HAProxy
2 =====================
3
4 This guide demonstrates a way to load balance connections across multiple
5 websockets server processes running on the same machine with HAProxy_.
6
7 We'll run server processes with Supervisor as described in :doc:`this guide
8 <supervisor>`.
9
10 .. _HAProxy: https://www.haproxy.org/
11
12 Run server processes
13 --------------------
14
15 Save this app to ``app.py``:
16
17 .. literalinclude:: ../../example/deployment/haproxy/app.py
18 :language: python
19
20 Each server process listens on a different port by extracting an incremental
21 index from an environment variable set by Supervisor.
22
23 Save this configuration to ``supervisord.conf``:
24
25 .. literalinclude:: ../../example/deployment/haproxy/supervisord.conf
26
27 This configuration runs four instances of the app.
28
29 Install Supervisor and run it:
30
31 .. code-block:: console
32
33 $ supervisord -c supervisord.conf -n
34
35 Configure and run HAProxy
36 -------------------------
37
38 Here's a simple HAProxy configuration to load balance connections across four
39 processes:
40
41 .. literalinclude:: ../../example/deployment/haproxy/haproxy.cfg
42
43 In the backend configuration, we set the load balancing method to
44 ``leastconn`` in order to balance the number of active connections across
45 servers. This is best for long running connections.
46
47 Save the configuration to ``haproxy.cfg``, install HAProxy, and run it:
48
49 .. code-block:: console
50
51 $ haproxy -f haproxy.cfg
52
53 You can confirm that HAProxy proxies connections properly:
54
55 .. code-block:: console
56
57 $ websockets ws://localhost:8080/
58 Connected to ws://localhost:8080/.
59 > Hello!
60 < Hello!
61 Connection closed: 1000 (OK).