Installation and deployment of HAproxy + Keepalived Server Load balancer and user manual 1. Environment Introduction: LB1: 10.10.13.240LB2: 10.10.13.241webserver1: affinity: 10.10.13.244LB1 and LB2 implement keepalived Master/Slave to switch the haproxy; the haproxy service will schedule two webservers, w
HAproxy + Keepalived Server Load balancer installation and deployment and User Manual
1. Environment Introduction:
LB1: 10.10.13.240
LB2: 10.10.13.241
Web server1: 10.10.13.243
Web server2: 10.10.13.244
The LB1 and LB2 enable keepalived master-slave mechanism to switch the haproxy service. The haproxy service schedules two web servers,
The external virtual IP address of the web server is 10.10.13.245.
Ii. 243/244: web Service configurations of two realservers
1. install the web service first: apt-get install apache2
2. Modify the web server log format
Vi/etc/apache2/apache2.conf
# LogFormat "% h % l % u % t \" % r \ "%> s % B \" % {Referer} I \ "\" % {User-Agent} I \ "" combined
LogFormat "% {X-Forwarded-For} I % l % u % t \" % r \ "%> s % B \" % {Referer} I \ "\" % {User-Agent} I \ "" combined
3. comment out the original CustomLog and modify the document root path DocumentRoot to/var/www.
Vi/etc/apache2/sites-available/default
SetEnvIf Request_URI "^/check \. txt $" dontlog
CustomLog/var/log/apache2/access. log combined env =! Dontlog
Echo "The web service provider is 243 | 244">/var/www/index.html
4. Restart the web Server
/Etc/init. d/apache2 restart
5. Create a check file. haproxy uses this file to check the WEB status.
Touch/var/www/check.txt
3. Configure haproxy for two servers
1. Install haproxy
Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
Tar xvf haproxy-1.4.22.tar.gz
Cd haproxy-1.4.22
Make TARGET = 36-Ubuntu
Make PREFIX =/usr/local/haproxy install
2. Edit the configuration file.
Cd/etc
Mkdir haproxy
Vim haproxy/haproxy. cfg
########### Global configuration #########
Global
# Daemon
# Nbproc 1
# Pidfile/var/run/haproxy. pid
Log 127.0.0.1 local0
Log 127.0.0.1 local1 notice
Maxconn 4096
######## Default configuration ############
Ults
Log global
Mode http # default mode {tcp | http | health}, tcp is Layer 4, http is Layer 7, health only returns OK
Retries 3 # If the connection fails twice, the server is considered unavailable. You can also set
# Option redispatch # When the server corresponding to the serverId fails, it is forcibly directed to other healthy servers.
# Option abortonclose # When the server load is high, the link for processing the queue for a long time is automatically terminated.
Option httplog
Option dontlognull
Option redispatch
Maxconn 2000 # default maximum number of connections
Timeout connect 5000 ms # connection timeout
Timeout client 30000 ms # client timeout
Timeout server 30000 ms # server timeout
# Timeout check 2000 # = heartbeat detection timeout
Log 127.0.0.1 local0 err # enable to record haproxy logs
Listen web 10.10.13.245: 80
Mode http
Stats enable
Balance roundrobin
Option httpclose
Option forwardfor
Option httpchk HEAD/check.txt HTTP/1.0
Server nc243 10.10.13.243: 80 cookie A check
Server nc244 10.10.13.244: 80 cookie B check
Or:
Frontend shishi
Bind *: 82
Mode http
Acl api_net url_sub-I sip_apiname = engagecloud
Use_backend engagec1_if api_net
Default_backend engagecloud
Backend engagecloud
Mode http
Balance roundrobin
Server app 10.15.14.130: 80 cookie
Server app1 10.15.14.131: 8080 cookie B
Backend salesapp
Mode http
Server app1 10.15.14.131: 8080 cookie B
Frontend shishi2
Bind *: 822
Mode http
Acl api_net url_sub-I sip_apiname = engagecloud
Use_backend engagec1_if api_net
Default_backend salesapp1
Backend engagecloud1
Mode http
Server app 10.15.14.130: 80 cookie
Backend salesapp1
Mode http
Server app1 10.15.14.131: 8080 cookie B
Server app 10.15.14.130: 80 cookie
Frontend http-ip // frontend node Definition
Bind 10.10.13.245: 80 // The virtual service node listens to port 80.
Mode http
Log global
Option httplog
Option httpclose // proactively disable the http channel after each request is completed. HA-Proxy does not support the keep-alive mode and can only simulate the implementation of this mode.
Option forwardfor // If the backend server needs to obtain the real IP address of the client, the Client IP address can be obtained from the Http Header.
Capture request header Host len 20 // This configuration and a similar configuration are used to capture the parameter records in the Http request to the log.
Capture request header User-Agent len 16
Capture request header Content-Length len 10
Capture request header Referer len 20
Capture response header Content-Length len 10
// Configure the control policy. For more information, see other users' configurations.
Acl api_taobao url_sub-I sip_apiname = taobao. // if the request url contains sip_apiname = taobao, true is returned. Otherwise, false is returned.
Acl api_alisoft url_sub-I sip_apiname = alisoft. // if the request url contains sip_apiname = alisoft, true is returned. Otherwise, false is returned.
Acl invalid_req url_sub-I sip_apiname = // if the request url contains sip_apiname =, true is returned. Otherwise, false is returned.
Acl stat_req url_dir-I admin // if admin exists in the request url as part of the address path, true is returned for this control policy; otherwise, false is returned.
Block if! Invalid_req! Stat_req // block indicates that the request is blocked and the error 403 is returned. The current error indicates that the request is blocked if the policy invalid_req is not met and the policy stat_req is not met. (That is, the sip_apiname parameter must be included in the URL, unless it is the URL for viewing the server status ).
Use_backend alisoft_server if api_alisoft // if the policy api_alisoft is met, alisoft_server is used as the backend service cluster.
Use_backend taobao_server if api_taobao // if the policy api_taobao is met, taobao_server is used as the backend service cluster.
Default_backend alisoft_server // use alisoft_server as the default backend service cluster.
Backend alisoft_server // backend Node 1 Definition
Mode http
Balance roundrobin // Server Load balancer policy configuration
Cookie SERVERID // you can insert a serverid to a cookie. The serverid can be defined later.
Server app1 10.10.13.243: 80 cookie 1 check fall 5 weight 1 // Real server Configuration definition cookie 1 indicates serverid is 1, check indicates status check is required, fall 5 indicates that the server status is unavailable after five failures (not accepting requests), and weight 1 indicates the weight.
Server app2 x. x: 8080 cookie 1 check fall 5 weight 10
Backend taobao_server // backend Node 2 Definition
Mode http
Server app1 10.10.13.244: 80 check fall 5
Listen haproxy-status 10.10.13.245: 8888
Mode http
Option httplog
Stats uri/haproxy-stats
Stats realm Global \ statistics
Stats auth admin: 123456
The haproxy configuration is complete.