Haproxy + Apache for Linux Server Load balancer Software
Haproxy provides high availability, Server Load balancer, and reverse proxy Based on TCP and HTTP applications, especially for websites with extremely high loads that usually require session persistence or layer-7 processing. Haproxy runs on the current hardware and supports tens of thousands of concurrent connections. Its running mode enables it to be easily and securely integrated into the architecture, at the same time, it can protect your web server from being exposed to the network.
Environment Planning:
Tong1: 192.168.1.247 haproxy
Tong2: 192.168.1.248 web1
Tong3: 192.168.1.249 web2
1. Network Configuration
Tong1 node:
[Root @ tong1 ~] # Hostname
Tong1
[Root @ tong1 ~] # Ifconfig | grep Mask
Inet addr: 192.168.1.247 Bcast: 192.168.1.255 Mask: 255.255.255.0
Inet addr: 127.0.0.1 Mask: 255.0.0.0
[Root @ tong1 ~] # Vim/etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
[Root @ tong1 ~] #
Same configuration as tong2 and tong3 nodes
2. Install the haproxy software on the tong1 node and enable the haproxy log function.
[Root @ tong1 ~] # Yum install haproxy-y
[Root @ tong1 ~] # Vim/etc/sysconfig/rsyslog -- add-r parameter record haproxy log
SYSLOGD_OPTIONS = "-r-c 2"
[Root @ tong1 ~] # Vim/etc/rsyslog. conf -- Define the log storage path
Local2. */var/log/haproxy. log
[Root @ tong1 ~] #/Etc/init. d/rsyslog restart
Shutting down system logger: [OK]
Starting system logger: [OK]
[Root @ tong1 ~] #
[Root @ tong1 ~] # Cd/etc/haproxy/
[Root @ tong1 haproxy] # vim haproxy. cfg
Global
Log 127.0.0.1 local2 -- enable the log function
Chroot/var/lib/haproxy -- run path
Pidfile/var/run/haproxy. pid -- pid file storage location
Maxconn 4000 -- maximum number of connections
User haproxy -- the user to which the task belongs
Group haproxy -- the user group to which the operation belongs
Daemon-Background running service
Ults
Mode http -- processing mode (http is Layer 7 and tcp is Layer 4)
Log global -- enable global Logging
Option httplog -- Log Type http format
Option dontlognull -- does not record health check logs
Option http-server-close
Option forwardfor partition t 127.0.0.0/8
Option redispatch -- the server corresponding to the serverId is forcibly redirected to another server after it fails.
Retries 3 -- defines that the server is unavailable when the connection fails three times.
Timeout http-request 10 s
Timeout queue 1 m
Timeout connect 10 s -- server connection timeout
Timeout client 1 m -- client Connection timeout
Timeout server 1 m -- server connection timeout
Timeout http-keep-alive 10 s -- persistent connection
Timeout check 10 s -- heartbeat check timeout
Maxconn 3000
Listen admin_status -- custom listener name, arbitrary
Bind 0.0.0.0: 80 -- bind Port
Mode http -- mode
Log 127.0.0.1 local3 err -- Record Error log
Stats refresh 20 s -- refresh every 20 s
Stats hide-version -- hide haproxy version information
Stats uri/haproxy-stats -- add/haproxy-stats after the domain name to view the haproxy monitoring status
Stats auth haproxy: system -- user name and password stats hide-version
Stats admin if TRUE -- the service can be started and stopped manually.
Listen site_status -- check the health of the backend host
Bind 0.0.0.0: 80
Mode http
Log 127.0.0.1 local3 err
Monitor-uri/site-stats -- check by url
Frontend main_status -- Define acl rules
Bind 0.0.0.0: 80 -- bind to port 80
Mode http
Log global
Option httplog
Option forwardfor
Acl web1 hdr_reg (host)-I ^ (www.itnihao.cn | ww1.itnihao.cn) $
-- Match www.itnihao.cn and ww1.itnihao.cn and put them in the web1 variable.
Acl web2 url_sub-I killall = -- put the request containing killall = into the web2 variable.
Acl web3 path_beg-I/static/images/javascript/stylesheets
Use_backend server_web3 if web3 -- if the web3 variable is met, it will be thrown to the virtual host in server_web3.
Default_backend server_web4 -- virtual host in server_web4 if none of them are met
Backend server_web3
Mode http
Balance roundrobin
Option httpchk GET/test.html -- Define the home address
Server ton1 192.168.1.248: 80 check inter 1500 rise 3 fall 3 weight 1
-- For backend servers, inter 1500 is used to check the heartbeat frequency. rise 3 is correctly available three times, fall 3 is unavailable three times, and weight 1 is the weight.
Server ton2 192.168.1.249: 80 check inter 1500 rise 3 fall 3 weight 1
Backend server_web4
Mode http
Balance roundrobin
Option httpchk GET/index.html
Server ton3 192.168.1.248: 80 check inter 1500 rise 3 fall 3 weight 1
Server ton4 192.168.1.249: 80 check inter 1500 rise 3 fall 3 weight 1
[Root @ tong1 haproxy] #/etc/init. d/haproxy restart
Stopping haproxy: [OK]
Starting haproxy: [OK]
[Root @ tong1 haproxy] #
3. Install the apache service on the backend host
Tong2 node:
[Root @ tong2 ~] # Hostname
Tong2
[Root @ tong2 ~] # Yum install httpd-y
[Root @ tong2 ~] # Vim/etc/httpd/conf/httpd. conf
ServerName 127.0.0.1
[Root @ tong2 ~] # Echo 'node2'>/var/www/html/index.html
[Root @ tong2 ~] # Echo 'static node2'>/var/www/html/test.html
[Root @ tong2 ~] #/Etc/init. d/httpd restart
Stopping httpd: [OK]
Starting httpd: [OK]
[Root @ tong2 ~] #
Tong3 node:
[Root @ tong3 ~] # Hostname
Tong3
[Root @ tong3 ~] # Yum install httpd-y
[Root @ tong3 ~] # Vim/etc/httpd/conf/httpd. conf
ServerName 127.0.0.1
[Root @ tong3 ~] # Echo 'node3 '>/var/www/html/index.html
[Root @ tong3 ~] # Echo 'static node3 '>/var/www/html/test.html
[Root @ tong3 ~] #/Etc/init. d/httpd restart
Stopping httpd: [OK]
Starting httpd: [OK]
[Root @ tong3 ~] #
4. Check the haproxy status and monitoring
Background management and monitoring url page
Backend host monitoring page
(1) the backend host is in normal status.
(2) the backend host is abnormal and goes down
Normal Access Node
Haproxy + Keepalived build Weblogic high-availability server Load balancer Cluster
Keepalived + HAProxy configure high-availability Load Balancing
Haproxy + Keepalived + Apache configuration notes in CentOS 6.3
Haproxy + KeepAlived WEB Cluster on CentOS 6
Haproxy + Keepalived build high-availability Load Balancing
For details about HAproxy, click here
HAproxy: click here
This article permanently updates the link address: