Haproxy load balancing and high availability

HAPROXY Profile
     HAProxy provide high availability, load balancing, and proxy TCP and HTTP-based applications, support for virtual hosts, it's free, fast and reliable a load balancing solution. HAProxy especially for those large load of web sites that usually they need to maintain or seven treatment sessions. HAProxy running on current hardware can support thousands of concurrent connections. And its mode of operation makes it really simple to integrate into your current security architecture, while protecting your web server is not exposed to the network.
2, HAProxy is characterized by:
1, HAProxy support virtual hosts.
2, can complement the advantages HAProxy Nginx some drawbacks, such as support Session holding, guiding the Cookie; support back-end server state detected by obtaining a specified url.
3, HAProxy similar with LVS, itself just a load balancing software; simple HAProxy will have better load balancing in terms of speed from the efficiency ratio Nginx, on concurrent processing is superior to the Nginx.
4, HAProxy TCP protocol supports load balancing forwarding, load balancing can be read on MySQL, MySQL nodes on the back end to detect and load balancing, you can use LVS + Keepalived to do load balancing MySQL master-slave.
5, HAProxy load balancing strategy very much, HAProxy load balancing algorithm is now specific There are eight types:
  ① the RoundRobin, represents a simple poll, This much said, this is basically have load balancing;
  ② static-rr, expressed according to the weight, the proposed attention;
  ③ leastconn, represents the minimum connection, first treatment, the proposed attention;
  ④ source, shows a request source IP, this with IP_hash mechanism Nginx similar, we use it as a way to solve the session problem, we propose a focus on ;
  ⑤ ri, shows a request URI;
  ⑥ rl_param, shows a request parameter URl 'Balance url_param' The requires the URL AN Parameter name;
  ⑦ HDR (name), a locking head shows a HTTP request for each HTTP request;
  ⑧ RDP-Cookie (name), according to data representation to lock cookie (name) and TCP hash every request.
3, haproxy configuration into five parts Detailed
global: process-level parameters, and operating systems are typically. These parameters are typically set once, if the configuration is correct, there is no need to modify it again
defaults: the default configuration parameters that can be used frontend, backend, Listen assembly
frontend: front end virtual node that receives the request, rules can be more directly specified Frontend specific use backend backend
backend: backend services cluster configuration, a real server, a backend servers corresponding to the one or more entities
Listen fronted and combinations backend body
4, haproxy described configuration item
######### ## global configuration #########
global
 log 127.0.0.1 local0 # log output configuration, all log records are present in the system log, by local0 output
 log 127.0.0.1 local1 notice #notice for the log level, usually there are 24 levels (error warringinfo debug)
 nbproc 1 # Set the number of processes, or the number of CPU cores is usually twice
 pidfile /etc/haproxy/haproxy.pid #haproxy process PID file
 maxconn 4096 # maximum number of connections (to be considered limiting ulimit-n)
 #chroot / usr / Share / haproxy #chroot running path
 uid 99 # users uid
 gid # 99 user gid
 daemon # daemon mode, after the station service form allows
 #debug #haproxy debug level is recommended only in the open single-process debugging
######## the default configuration ############
defaults
 log # define global logs for log defined global configuration
 mode http # default mode mode {tcp | http | health} , tcp 4 layers, http 7 layer, health will only return the OK
 the Option HTTPLOG # log category, using http log format log
 #option dontlognull # check the log does not record health information             
 retries 3 # check node server failures, failed three times in a row to reach, then that node is unavailable
 if the back-end server needs to get real client ip parameters need to be configured, can be obtained from the client i forwardfor # Http Header in #option
 #option httpclose after completion of each request # http actively closed passage, haproxy not support the keep-alive, only an analog implementation of this mode
 maxconn 4096 # maximum number of connections
 contimeout connection time # 5000
 clitimeout client timeout # 50000
 srvtimeout 50000 # server timeout time
 #timeout check 2000 # heartbeat timeout
 #timeout http-keep-alive10s # default persistent connection time
 #timeout http-request 10s # default http request time
 #timeoutqueue 1m # default queue timeout
  ######## statistics page layout ########
the listen admin_stats
   the bind 0.0.0.0:1080 # set the name of a combination of Frontend and Backend, the monitoring group, as required custom name
   mode http #http seven-layer model
   option httplog # using http log format
   127.0.0.1 local0 err # #log error log records
  maxconn 10 # default maximum number of connections
  stats refresh 30s # statistics page automatically refresh time
  stats uri / stats # statistics page url
  stats realm Crushlinux \ prompt on the statistics page Haproxy # password text box
  stats auth admin: admin # set the monitoring page of user and password: admin, you can set up multiple user names
  HAProxy on the stats hide-version # hide the statistics page version information
  # stats admin if TRUE # settings manually enable / disable, back-end server (after haproxy-1.4.9 version)
######## set haproxy error page #####
  errorfile 403 / Home / haproxy / haproxy / errorfiles /403.http
  errorfile 500 /home/haproxy/haproxy/errorfiles/500.http
  502 /home/haproxy/haproxy/errorfiles/502.http errorfile
  errorfile 503 /home/haproxy/haproxy/errorfiles/503.http
  errorfile 504 /home/haproxy/haproxy/errorfiles/504.http
######## frontend front-end configuration ##############
bibind *: 80 # it is recommended to use bind *: 80 ways, or else do high availability cluster when there is a problem, vip can not switch to another machine visited.
  acl web hdr (host) -i is behind www.abc.com #acl rule name, -i is the domain name you want to access,
  acl img HDR (Host) -i img.abc.com # If this visit www.abc.com the domain will be distributed to the following scope webserver.
                       # If you visit img.abc.com.cn distributed to imgserver this scope.
  IF Web webserver use_backend
  use_backend imgserver IF img
######## backend backend configuration ##############
backend webserver #webserver scope
  mode http
  balance roundrobin #banlance roundrobin poll, balance source to save the session value, support for static-rr, leastconn, first, uri parameters
  option httpchk /index.html HTTP / 1.0 # health inspection, test papers, if distributed to the background index.html access less than distribute it no longer
  Server web1 192.168.200.103:80 the Check the cookie 1 weight 1 2 Inter Rise Fall 2000 3
  Server web2 192.168.200.104:80 the cookie weight 1 2 2 Rise Fall 2000 the Check Inter 3
  #cookie 1 is expressed serverid 1, check inter 1500 is to detect heart rate
  #rise 2 times 2 is considered correct server is available, fall 3 is 3 failed think server is unavailable, weight represents the weight
  backend imgserver
  the MODE HTTP
  the Option httpchk /index.php
  Balance the RoundRobin
  server img01 192.168. 200.105: 80 Fall 2000 Inter. 3 Check
  Server img02 192.168.200.106:80 Check Fall 2000 Inter. 3
######## ################# arranged TCP
test1 the listen
  the bind 0.0.0.0:90
  MODE TCP
  Option # tcplog log category using tcplog
  The maxconn 4086
  #log 127.0.0.1 of local0 Debug
  Server S1. 1 weight 192.168.200.107:80
  Server S2 192.168.200.108:80 weight. 1
. 5, constructed based Haproxy cluster load balancing
1, installation configuration Haproxy
mounting Haproxy source package dependencies and compiler installation
[the root @ localhost ~] # yum the install -Y-C GCC GCC ++ PCRE the make-bzip2-devel devel
[the root @ localhost ~] # the tar XF haproxy- -C 1.4.24.tar.gz / usr / the src /
[the root @ localhost ~] # CD /usr/src/haproxy-1.4.24/
[@ localhost HAProxy the root-1.4.24] # && the make the make the TARGET = linux26 install
build haproxy configuration directory and file
[root @ localhost haproxy-1.4.24] # mkdir / etc / haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
haproxy 配置文件修改
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#   log 127.0.0.1   local0
#   log 127.0.0.1   local1 notice
    log /dev/log    local0 info
    log /dev/log    local0 notice
    maxconn 4096
    uid 99
    gid 99
    daemon
defaults
    log global
    mode    http
    option  httplog
    retries 3
    maxconn 4096
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
listen  webcluster 0.0.0.0:80
    option  httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.113:80 check inter 2000 fall 3
    server  inst1 192.168.200.114:80 check inter 2000 fall 3
listen admin_stats
    bind 0.0.0.0:8000
    mode http
    option httplog
    maxconn 100
    stats refresh 30s
    stats uri /stats
    stats realm Crushlinux\ Haproxy
        stats auth admin:admin
    stats hide-version 
准备服务自启动脚本
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [确定]
2、安装配置Web服务Nginx  
Nginx1,Nginx2配置相同,
[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.6.2
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@localhost nginx-1.6.2]# cd /usr/local/nginx/html/
nginx1 :
[root@localhost ~]# echo "111111" > /usr/local/nginx/html/index.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx
nginx2:
[the root @ localhost ~] # echo "222222"> /usr/local/nginx/html/index.html
[the root @ localhost ~] # / usr / local / Nginx / sbin / Nginx
. 6, built on Haporxy + Keepalived high-availability load balancing cluster
1, to prepare four virtual machines two nginx two HAProxy
2, two nginx
[root @ Nginx-1 ~] # yum -y install gcc gcc-c ++ PCRE the make-devel zlib-devel
[root @ ~. 1-nginx] -M # the useradd -s / sbin / nologin Nginx
[the root-nginx @ ~. 1] # Nginx the tar-1.6.2.tar.gz -C XF / usr / the src
[the root-nginx @ ~. 1] CD /usr/src/nginx-1.6.2 #
[@ the Nginx the root-Nginx-1.6.2. 1] # ./configure --prefix = / usr / local / Nginx Nginx --group --user = = && the make Nginx the install the make &&
[@ the Nginx the root-Nginx-1.6.2. 1] # CD / usr / local / Nginx / HTML /
nginx1:
[the root @ localhost ~] # echo "111111"> / usr / local / Nginx / HTML / index .html
[the root @ localhost ~] # / usr / local / Nginx / sbin / Nginx
nginx2:
[the root @ localhost ~] # echo "222222"> /usr/local/nginx/html/index.html
[the root @ localhost ~] # / usr / local / nginx / sbin / nginx
 
installation configuration consistent Haproxy two machines:
[the root-Haproxy @ ~. 1] # yum the install -Y-C GCC GCC ++ PCRE the make-bzip2-devel devel
[the root-Haproxy @ ~. 1] the tar-1.4.24.tar.gz HAProxy XF # -C / usr / the src /
[the root-Haproxy @ ~. 1] # CD /usr/src/haproxy-1.4.24/
[@ Haproxy the root-HAProxy-1.4. 1. 24] # make TARGET = linux26 && make install
build haproxy configuration directory and file
[root @ Haproxy-1 haproxy-1.4.24] # mkdir / etc / haproxy
[root @ Haproxy-1 haproxy-1.4.24] # cp examples /haproxy.cfg / etc / haproxy /
 modify haproxy configuration file:
[the root-Haproxy @ ~. 1] # Vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#   log 127.0.0.1   local0
#   log 127.0.0.1   local1 notice
    log /dev/log    local0 info
    log /dev/log    local0 notice
    maxconn 4096
    uid 99
    gid 99
    daemon
defaults
    log global
    mode    http
    option  httplog
    retries 3
    maxconn 4096
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
listen  webcluster 0.0.0.0:80
    option  httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.113:80 check inter 2000 fall 3
    2000 Inter inst1 192.168.200.114:80 the Check Server 3 Fall
the listen admin_stats
    the bind 0.0.0.0:8000
    the MODE HTTP
    the Option HTTPLOG
    maxconn 100
    stats Refresh 30s
    stats uri / stats
    stats realm Crushlinux \ Haproxy
        stats auth ADMIN: ADMIN
    stats hide-Version
created since the launch of script
[the root-Haproxy @ ~. 1] # CP /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[the root-Haproxy @ ~. 1] LN # -s / usr / local / sbin / HAProxy / usr / sbin / HAProxy
[the root-Haproxy @ ~. 1] # X /etc/init.d/haproxy the chmod +
[the root-Haproxy @ ~. 1] # /etc/init.d/haproxy Start
Starting haproxy: [OK]
 
client access test:
use a different browser to access
结果不同即为成功
 
编译安装keepalived服务
[root@Haproxy-1 ~]# yum -y install keepalived
[root@Haproxy-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
        notification_email {
                [email protected]
        }
        notification_email_from [email protected]
        smtp_server 192.168.200.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
        vrrp_skip_check_adv_addr
        vrrp_strict
        vrrp_garp_interval 0
        vrrp_gna_interval 0
}
vrrp_script chk_http_port {
        script "/root/check_haproxy.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
        state MASTER
        interface ens32
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
        chk_http_port
    }
}
 第二台Haproxy配置keepalibed 主配置文件
[root@Haproxy-2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
        notification_email {
                [email protected]
        }
        notification_email_from [email protected]
        smtp_server 192.168.200.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
        vrrp_skip_check_adv_addr
        vrrp_strict
        vrrp_garp_interval 0
        vrrp_gna_interval 0
}
vrrp_script chk_http_port {
        script "/root/check_haproxy.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
        state BACKUP
        interface ens32
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
        the PASS AUTH_TYPE
        AUTH_PASS 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
        chk_http_port
    }
}          
are arranged on both machines script haproxy detecting
[the root-Haproxy @ ~. 1] /etc/keepalived/check_haproxy.sh CAT #
#! / bin / the bash
NUM `--no HAProxy PS -C =-header | WC -l`
IF [NUM -eq $ 0]
the then
    /etc/init.d/haproxy Start
    SLEEP. 3
    ` --no-PS header -C HAProxy IF [| WC -eq 0 -l`]
    the then
        systemctl STOP keepalived
    Fi
Fi
[the root-Haproxy @ ~. 1] # X /root/check_haproxy.sh the chmod +
[root @ Haproxy-1 ~] # service keepalived start
 
 
 
 
 
 
 
 
 
 
 
 
 
 
References: https: //www.cnblogs.com/crushlinux/p/6738982.htm

Guess you like

Origin www.cnblogs.com/elin989898/p/11955540.html