Tomcat load balancing with HAProxy in openSUSE

Published on November 3, 2018 by Daniel Lanza

 opensuse  haproxy

This tutorial will explain how to load balancing two Tomcat instances with HAProxy in openSUSE.

The environment will be simulated with three virtual machines and VirtualBox:

#Server FQDNIP
1haproxy-server.dalanzg.com192.168.56.101HAProxy - Port 80
2tomcat-server1.dalanzg.com192.168.56.102Apache Tomcat - Port 8080
3tomcat-server2.dalanzg.com192.168.56.103Apache Tomcat - Port 8080

Check the following post to configure openSUSE with Internet and statick IP address by using YaST on VirtualBox

Steps

Install HAProxy

Install HAProxy in server #1

dlanza@haproxy-server:~> sudo zypper install haproxy
dlanza@haproxy-server:~> sudo systemctl enable haproxy.service 
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
dlanza@haproxy-server:~> sudo systemctl start haproxy.service

Check HAProxy works in server #1 from your host machine.

HAProxy works
HAProxy works

Install Apache Tomcat

Steps for servers #2 and #3:

And create a static html file.

dlanza@tomcat-server1:/opt> mkdir /opt/apache-tomcat-8.5.34/webapps/dalanzg
dlanza@tomcat-server1:/opt> vim /opt/apache-tomcat-8.5.34/webapps/dalanzg/index.html
<html>
  <body>
    <h3>Tomcat server #1</h3>
  </body> 
</html> 

Check you can reach the index page from servers #2 and #3:

Tomcat 1
Tomcat 1
Tomcat 2
Tomcat 2

Load Balancing Across Multiple Backend Servers with HAProxy

The HAProxy configuration will work with the following features:

Edit haproxy.cfg configuration file.

dlanza@haproxy-server:~> su -
haproxy-server:~ # vim /etc/haproxy/haproxy.cfg
global
  log /dev/log daemon
  maxconn 32768
  chroot /var/lib/haproxy
  user haproxy
  group haproxy
  daemon
  stats socket /var/lib/haproxy/stats user haproxy group haproxy mode 0640 level operator
  tune.bufsize 32768
  tune.ssl.default-dh-param 2048
  ssl-default-bind-ciphers ALL:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK:!RC4:!ADH:!LOW@STRENGTH

defaults
  log     global
  mode    http
  option  log-health-checks
  option  log-separate-errors
  option  dontlog-normal
  option  dontlognull
  option  httplog
  option  socket-stats
  retries 3
  option  redispatch
  maxconn 10000
  timeout connect     5s
  timeout client     50s
  timeout server    450s

listen stats
  bind *:9000
  mode http
  stats enable
  stats hide-version   #Hide HAProxy version
  stats show-node
  stats uri /stats   #Stats URI
  stats auth admin:haproxy   #Authentication credentials
  stats refresh 5s
  
frontend tomcat-service
  bind *:80
  acl dalanzg_uri path_beg /dalanzg
  use_backend tomcat-server if dalanzg_uri  #Balance if URI
  default_backend tomcat-server   #Balance by default

backend tomcat-server
  balance roundrobin
  server tomcat1 192.168.56.102:8080 check
  server tomcat2 192.168.56.103:8080 check

Restart HAProxy.

haproxy-server:~ # service haproxy restart

Check stats with authentication in http://192.168.56.101:9000

Authentication for stats
Authentication for stats
Stats page
Stats page

Go several times to http://192.168.56.101/dalanzg

Tomcat worker 1
Tomcat worker 1
Tomcat worker 2
Tomcat worker 2

And now, shutdown Tomcat 2 and check that http://192.168.56.101/dalanzg is still working in Tomcat 1.

dlanza@tomcat-server2:~> /opt/apache-tomcat-8.5.34/bin/shutdown.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.34
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.34
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.34/temp
Using JRE_HOME:        /usr/lib64/jvm/jre
Using CLASSPATH:       /opt/apache-tomcat-8.5.34/bin/bootstrap.jar:/opt/apache-tomcat-8.5.34/bin/tomcat-juli.jar
Tomcat worker 1
Tomcat worker 1

And tomcat2 is down in stats page.

Tomcat 2 down
Tomcat 2 down
comments powered by Disqus

See also

Tomcat load balancing with mod_proxy and mod_proxy_balancer

This tutorial will explain how to load balancing two Tomcat instances with Apache HTTPD server with mod_proxy and mod_proxy_balancer modules. The environment will be simulated with three virtual machines and VirtualBox: # Server FQDN IP 1 apache-server.dalanzg.com 192.168.56.101 Apache HTTPD - Port 80 2 tomcat-server1.dalanzg.com 192.168.56.102 Apache Tomcat - Port 8080 3 tomcat-server2.dalanzg.com 192.168.56.103 Apache Tomcat - Port 8080 Check the following post to configure openSUSE with Internet and statick IP address by using YaST on VirtualBox

Read more

openSUSE with Internet and static IP address by using YaST on VirtualBox

An openSUSE virtual machine will be configured to have a static IP address and Internet connection on VirtualBox. This scenario is really useful if you want to install a specific application in guest and you need to communicate from your host. Therefore, the virtual machine will have two network adapters: NAT -> Internet connection Host-only-adapter -> Static IP address (192.168.56.200) and FQDN server1.dalanzg.com Steps Settings for VirtualBox Virtual machine network settings Settings for network devices Settings for VirtualBox First, create a Host Network Manager in VirtualBox.

Read more

How to install Apache HTTPD in openSUSE

This tutorial will explain how to install Apache HTTPD in openSUSE 15. The original documentation is found in The Apache HTTP Server Project. And the official documentation for openSUSE is in The Apache HTTP Server. Requirements You will need the following: openSUSE Leap 15 Steps Set hostname and domain Install Apache HTTPD Start and stop Apache Start Apache automatically at boot time Set hostname and domain Files /etc/hostname and /etc/hosts will be modified to resolve the following:

Read more