Initial use of haproxy

Keywords: curl yum socket Web Server

Initial use of haproxy

brief introduction

HAProxy provides load balancing and proxy based on tcp and http applications. It is a fast, free and reliable solution
Although the performance and stability of HAProxy are not as good as lvs, it is much better than lvs in function

install

On centos7, HAProxy can be installed using yum or source package

#yum installation mode
]# yum install haproxy

#Source package installation mode
]# wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.5.tar.gz
]# tar -zxvf haproxy-1.8.5.tar.gz
]# cd haproxy-1.8.5/
]# ./configure --prefix=/usr/share/haproxy
]# make && make install

To configure

The main configuration file of haproxy is / etc/haproxy/haproxy.cfg. It includes global configuration section and proxy related configuration section, such as "defaults", "listen", "frontend", "backend"

]# cat /etc/haproxy/haproxy.cfg
global//Global configuration
    log         127.0.0.1 local2 //Log storage mode, log software such as rsyslog needs to be called

    chroot      /var/lib/haproxy //The working directory of haproxy can enhance the security level of haproxy. It must be empty and no user can have write permission
    pidfile     /var/run/haproxy.pid //Specify the runtime pid file
    maxconn     4000 //The maximum number of connections, 4000 by default, can be increased if not enough
    user        haproxy
    group       haproxy
    daemon //Run as a daemons, or run explicitly on the front end if not specified

    stats socket /var/lib/haproxy/stats //s socket 

defaults //Provide default parameters for all other configuration segments
    mode                    http //Set the operation mode or protocol of the instance, optional tcp
    log                     global //Enable event and traffic logging for each instance
    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 //Set the maximum number of concurrent connections for a front end

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets //Define acl, name is url_static, rule is the first part of uri, ignore case, match / static
    acl url_static       path_end       -i .jpg .gif .png .css .js //Define acl, name is URL ﹣ static, rule is the tail of ui, ignore case, match. jpg

    use_backend static          if url_static //Use static back-end server if URL ﹣ static is matched
    default_backend             app //Use app backend server by default

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin //Define the scheduling method as polling
    server      static 127.0.0.1:4331 check //Define the back-end server named static and enable health status detection

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

Experiment

Now it is the simplest to use haproxy. It uses two web servers on the back end of the load balancing of haproxy

  • Environmental Science
    node1:192.168.10.201, scheduler
    node2:192.168.10.203,web server 1
    node3:192.168.10.204,web server 2
    Install haproxy on node1, httpd on node2 and node3

  • To configure
    Modify the configuration file of haproxy on node1 (just modify frontend,backend and delete listen)

    ]# vim /etc/haproxy/haproxy.cfg
    frontend  main *:80
    default_backend             webservers
    
    backend webservers
        balance     roundrobin
        server  web1 192.168.10.203:80 check
        server  web2 192.168.10.204:80 check
    
  • phenomenon

    ]# curl 192.168.10.201
    welcome to web1
    ]# curl 192.168.10.201
    welcome to web2
    ]# curl 192.168.10.201
    welcome to web1
    ]# curl 192.168.10.201
    welcome to web2
    

Posted by Xu Wei Jie on Sat, 04 Apr 2020 14:50:33 -0700