Nowadays, it is necessary for companies to realize high availability. Among many load balancing cluster solutions, Haproxy is a well-known one. It can implement TCP or Http load balancing. The advantages are as follows:

  • Free and open source, it is much cheaper than hardware load balancing
  • It can maintain a maximum of 40,000 to 50,000 concurrent requests, which is very powerful for software level load balancing
  • Supports multiple load balancing algorithms and session persistence
  • Support for virtual hosting
  • On the server monitoring page, you can learn about the real-time running status of the system

Using the figure on the official website, HA in the figure is the Haproxy software, and PROXY below is the server to be PROXY. The client only needs to access HA, which is equivalent to accessing PROXY.

demo

With software like this, it’s much more useful to get your hands on it than to talk about it. I am still based on the Docker demo and understand that the principle is mainly configuration files, using virtual machines or containers are similar.

  1. Pull Haproxy:1.8 Mirroring
Docker pull haproxy: 1.8Copy the code
  1. Pull the HTTPD image and prepare the Web container
Docker pull HTTPD :2.4 Docker runit-d--name web1 HTTPD :2.4 docker runit-d--name web2 HTTPD :2.4 docker runit-d- the name web - static HTTPD: 2.4 dockerexec -it web1 /bin/bash
Copy the code

After entering the container, modify its HTML pages separately

  1. Run the Haproxy container and prepare its configuration file

The Haproxy configuration file is divided into several parts

  • Global Indicates global configuration information, which is related to processes. If directories are related, create directories in advance
  • Defaults Specifies the default configuration information
  • The frontend of frontend is configured and exposed to users
  • Backend backend is configured to truly improve services

In the official website, all configuration instructions are explained in detail, the url is at the end of the reference.

Our configuration is as follows

global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    logGlobal option Httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch  retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js use_backend staticif url_static
    default_backend             app
    bind :80
backend static
    balance     roundrobin
    server      static 172.17.0.5:80 check

backend app
    balance     roundrobin
    server  app1 172.17.0.3:80 check
    server  app2 172.17.0.4:80 check
Copy the code

The results of

When accessing the same server, you can see that the content of the response is indeed forwarded to a different server on the back end

The last

The paper come zhongjue shallow, and must know this to practice. Haproxy stays in your head until you configure it yourself. There are two problems

  1. The frontend configuration of Haproxy1.5 is slightly different from that of 1.8
  2. If the stats directory in the configuration file does not exist, an error will be reported

Use haproxy -c to verify that the configuration file is correct before making the configuration take effect

reference

  • Haproxy official configuration reference