You don't need to use the Apache httpd for load-balancing, but you still need to use a load balancer. You can start a dedicated Wildfly server which will work as the load balancer within your domain. There is a ready-to-use profile called load-balancer
within the default domain configuration file (domain.xml
).
Steps to add a WildFly load balancer on the HTTP default port (80):
1) Edit domain.xml
- fill the expected port number in the socket-binding-group
element for load-balancer-sockets
:
<socket-binding-group name="load-balancer-sockets" default-interface="public">
<socket-binding name="http" port="80"/>
<socket-binding name="https" port="443"/>
<socket-binding name="mcmp-management" interface="private" port="${jboss.mcmp.port:8090}"/>
<socket-binding name="modcluster" interface="private" multicast-address="${jboss.modcluster.multicast.address:224.0.1.105}" multicast-port="23364"/>
</socket-binding-group>
2) Edit domain.xml
- add a new server-group
for your load-balancer(s). Use load-balancer
profile and load-balancer-sockets
socket binding group for it. Example:
<server-groups>
<server-group name="main-server-group" profile="ha">
<socket-binding-group ref="ha-sockets"/>
</server-group>
<server-group name="load-balancer-group" profile="load-balancer">
<socket-binding-group ref="load-balancer-sockets"/>
</server-group>
</server-groups>
3) edit host.xml
- add a new server which will be the load balancer - i.e. add it to the new load-balancer-group
:
<servers>
<server name="server-one" group="main-server-group"/>
<server name="server-two" group="main-server-group" auto-start="true">
<socket-bindings port-offset="150"/>
</server>
<server name="load-balancer" group="load-balancer-group" auto-start="true"/>
</servers>
4) If you use port numbers lower than 1024 on Linux like systems, then run the domain with a privileged system account (e.g. by using sudo
):
sudo bin/domain.sh
You should have your domain including the load balancer successfully running now.
Update 1:
Troubleshooting
Make sure, all the bind interfaces are properly published (e.g. the private
one on the load balancer!)
Host controller:
export MY_IP=192.168.105.1
bin/domain.sh --host-config=host-master.xml -b $MY_IP -bmanagement $MY_IP -bprivate $MY_IP
Slave:
export MY_IP=192.168.105.2
bin/domain.sh --host-config=host-slave.xml -b $MY_IP -bmanagement $MY_IP -Djboss.domain.master.address=192.168.105.1
If you use the host-master.xml
as the host config for the load balancer, make sure you've added the public
interface to it.
<interface name="public">
<inet-address value="${jboss.bind.address:127.0.0.1}"/>
</interface>
If your environment doesn't support multicast (e.g. clouds or docker environments), use e.g. static modcluster discovery config:
bin/jboss-cli.sh <<EOT
embed-host-controller
/socket-binding-group=ha-sockets/remote-destination-outbound-socket-binding=proxy1:add(host=192.168.105.1, port=8090)
/profile=ha/subsystem=modcluster/proxy=default:write-attribute(name=advertise, value=false)
/profile=ha/subsystem=modcluster/proxy=default:list-add(name=proxies, value=proxy1)
EOT
Test configuration with a simple <distributable/>
application. I usually use a simple request counter app. It's a single JSP so it's easy to check it source code directly within the war file:
wget https://github.com/kwart/secured-webapp-template/releases/download/single-jsp-counter-distributable/counter-distributable.war
bin/jboss-cli.sh <<EOT
embed-host-controller
deploy counter-distributable.war --server-groups=main-server-group
Then test it by pointing your browser to http://192.168.105.1:8080/counter-distributable/ and also http://192.168.105.1/counter-distributable/