Rate This Document
Findability
Accuracy
Completeness
Readability

Running and Verifying HAProxy

  1. Configure parameters.
    1. Back up the option-http_proxy.cfg file.
      1
      mv /usr/local/haproxy/conf/option-http_proxy.cfg /usr/local/haproxy/conf/option-http_proxy.cfg_bak
      
    2. Create an option-http_proxy.cfg file.
      1
      vi /usr/local/haproxy/conf/option-http_proxy.cfg
      
    3. Modify the file as follows (Replace IP1:PORT1 and IP2:PORT2 with the actual values), and save the file and exit.
      global
      maxconn      20000
      log               127.0.0.1 local0  info
      uid               0
      gid               0
      chroot          /usr/local/haproxy
      nbproc          4
      daemon
      defaults
      mode                       http
      retries                      3
      timeout connect      10s
      timeout client          20s
      timeout server         30s
      timeout check          2s
      frontend test-proxy
      bind            *:80
      mode            http
      log             global
      default_backend test-proxy-srv
      backend test-proxy-srv
      balance        roundrobin
      option http-server-close
      option httpchk   GET /index.html
      http-check expect       status 200
      server          web1    IP1:PORT1 weight 3
      server          web2    IP2:PORT2 weight 3

      Table 1 describes the parameters in the configuration file.

      Table 1 Example configuration file and parameter description

      Parameter

      Description

      global

      -

      maxconn 20000

      Specifies the maximum number of connections.

      log 127.0.0.1 local0 info

      Specifies the log output device. info indicates the log severity level.

      uid 0

      Specifies the ID of the user who runs HAProxy.

      gid 0

      Specifies the ID of the user group to which the user running HAProxy belongs.

      chroot /usr/local/haproxy

      Specifies the chroot running path.

      nbproc 4

      Specifies the number of processes.

      daemon

      Runs HAProxy in the background.

      defaults

      -

      mode http

      Specifies the processed type (layer 7 uses HTTP, and layer 4 uses TCP).

      retries 3

      Specifies the maximum number of retry times for connecting to the backend server. If the number of retry times exceeds the specified value, the backend server is unavailable.

      timeout connect 10s

      Specifies the maximum time allowed before a connection is set up between HAProxy and the backend server

      timeout client 20s

      Specifies the timeout period for keeping an idle connection with the client.

      timeout server 30s

      Specifies the timeout period for keeping an idle connection with the server.

      timeout check 2s

      Specifies the timeout period for checking the server.

      frontend test-proxy

      -

      bind *:80

      Specifies one or more listening sockets. The asterisk (*) indicates all IPv4 addresses.

      mode http

      Specifies the processed type (layer 7 uses HTTP, and layer 4 uses TCP).

      log global

      Inherits the definition of log in the global section.

      default_backend test-proxy-srv

      Specifies the default backend server pool.

      backend test-proxy-srv

      -

      balance roundrobin

      Specifies the load balancing algorithm roundrobin, which is a weight-based polling algorithm and applies to scenarios where server performance is even.

      option http-server-close

      Enables this function when persistent connection is enabled.

      option httpchk GET /index.html

      http-check expect status 200

      Enables HTTP service status check (health check). Checks the returned status code. If "200" is not received, the request is not scheduled to backend servers.

      server web1 IP1:PORT1 weight 3

      server web2 IP2:PORT2 weight 3

      Defines multiple backend servers.

      Format: server <name> <address>:[port] [param*]

      NOTE:

      IP1:PORT1 and IP2:PORT2 indicate the IP addresses and port numbers of the backend servers.

  2. Start HAProxy.
    1
    taskset -c 0-3 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/option-http_proxy.cfg
    

    If the error message "cannot bind socket [0.0.0.0:80]" is displayed, check the process that occupies port 80, stop the process, and then start HAProxy again.

    1
    netstat -anpt | grep 80
    
    1
    kill -9 136508
    

  3. Verify HAProxy.
    1
    ps -ef | grep haproxy
    

    The HAProxy process is displayed.

    Open the browser, enter http://HAProxy IP address:80 in the address box and press Enter. The WebUI page of the backend server is displayed, indicating that HAProxy is running properly. Refresh the page. The page switches between backend servers.

    • (Optional) Run the following command to stop HAProxy. Do not run this command when services are running.
      1
      pkill haproxy