-----client---------haproxy-------nginx1---------nginx2------
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20
First, install Nginx
[Email protected] ~]# yum-y install Pcre-devel zlib-devel
[Email protected] ~]# useradd-m-s/sbin/nologin nginx
[Email protected] ~]# TAR-ZXVF nginx-1.6.0.tar.gz-c/usr/src/
[Email protected] ~]# cd/usr/src/nginx-1.6.0/
[Email protected] nginx-1.6.0]#/configure--
Prefix=/usr/local/nginx--user=nginx--group=nginx--with-fileaio
--with-http_stub_status_module--with-http_gzip_static_module
--with-http_flv_module--with-http_ssl_module
[[email protected] nginx-1.6.0]# make && make install
[Email protected] ~]# ln-s/usr/local/nginx/sbin/*
/usr/local/sbin/
[Email protected] ~]# nginx-t
[Email protected] ~]# Nginx
[Email protected] ~]# NETSTAT-ANPT | grep 80
[[email protected] ~]# killall-s HUP nginx//Reload
[[email protected] ~]# killall-s QUIT nginx//Shutdown service
[Email protected] ~]# Nginx
Verify:
Web-1:
[[Email protected] ~] #echo "Welcome to 192.168.1.20 Web Server" >
/usr/local/nginx/html/index.html
Web-2:
[[Email protected] ~] #echo "Welcome to 192.168.1.30 Web Server" >
/usr/local/nginx/html/index.html
[email protected] ~]# Firefox http://localhost/&
Second, installation Haproxy
1. Installation
[Email protected] ~]# yum-y install Pcre-devel zlib-devel
[Email protected] ~]# TAR-ZXVF haproxy-1.4.24.tar.gz-c/usr/src/
[Email protected] ~]# cd/usr/src/haproxy-1.4.24/
[email protected] ~]# make target=linux26
Prefix=/usr/local/haproxy
Note: Linux26 refers to the kernel version number of Linux.
[[email protected] ~]# make install Prefix=/usr/local/haproxy
2, Configuration Haproxy
[Email protected] ~]# Mkdir/etc/haproxy
[Email protected] ~]# cp/usr/src/haproxy-
1.4.24/examples/haproxy.cfg/etc/haproxy/
[Email protected] ~]# vim/etc/haproxy/haproxy.cfg
Modify:
Global
Log 127.0.0.1 local0//configuration logging, local0 as log device, default
Store to System log
Log 127.0.0.1 local1 Notice//notice is logging level, usually with 7 levels
#log Loghost local0 Info
Maxconn 4096//default maximum number of connections, consider the ulimit-n limit:
Limits on the number of Ulimit-n 819200 #ulimit can be increased
Chroot/usr/share/haproxy//Run path
UID 99
GID 99
#debug
#quiet
Defaults
Log global//define logs as logs in global
Mode HTTP//pattern for HTTP
Option Httplog//log format with HTTP
Option Dontlognull//Do not record health check log information
Retries 3//Three connection failure is considered to be the server is not available, can also be set by the following
#redispatch
Maxconn 2000//Maximum number of connections
Contimeout 5000//Connection time-out
Clitimeout 50000//Client time-out
Srvtimeout 50000//service-time timeout
Listen stats
Mode http
bind:6677
Stats enable
Stats Hide-version
Stats Uri/haproxyadmin?stats
Stats Realm Haproxy\ Statistics
Stats Auth Admin:admin
Stats Admin If TRUE
Listen Webcluster 0.0.0.0:80//define cluster name, listening address and port
Option Httpchk get/index.html Note: You can delete//check the server's
index.html file
Balance Roundrobin//load Balancing Polling algorithm
Server Inst1 192.168.1.20:80 Check Inter fall 3//Online node
Server Inst2 192.168.1.30:80 Check Inter fall 3//Last Add
Backup means back up the borrowing point
Attention:
If the error occurs at startup:/haproxy.main ()] Cannot
Chroot (/usr/share/haproxy)
is created manually:
[Email protected] ~]# Mkdir/usr/share/haproxy
If an error occurs when booting: Starting proxy cacti:cannot bind socket
Then execute:
[Email protected] ~]# SYSCTL-E net.ipv4.ip_nonlocal_bind=1
3. Start Haproxy
[Email protected] ~]# ln-s/usr/local/haproxy/sbin/*/usr/sbin///
Note the list of soft links
[Email protected]calhost ~]# cp/usr/src/haproxy-
1.4.24/examples/haproxy.init/etc/init.d/haproxy
[Email protected] ~]# chmod +x/etc/init.d/haproxy
[[email protected] ~]#/etc/init.d/haproxy start
[Email protected] ~]#/etc/init.d/haproxy status
[Email protected] ~]# NETSTAT-ANP | grep haproxy//takes up TCP as well.
80 ports
[Email protected] ~]# chkconfig--add haproxy
[Email protected] ~]# chkconfig haproxy on
Http://192.168.56.200:6677/haproxyadmin?stats viewing the status of a cluster
4. Verification:
Client input:
Http://192.168.1.1/
Disconnect one of the nodes, and then access:
Http://192.168.1.1/
5. Set Haproxy Log
[Email protected] ~]# vim/etc/haproxy/haproxy.cfg
Modify:
Log 127.0.0.1 Local3//Set Haproxy log level to 3
[Email protected] ~]# vim/etc/rsyslog.d/haproxy.conf
Add to:
$ModLoad imudp//Loading module
$UDPServerRun 514//Receive UDP 514 ports sent over the log
local3.*/var/log/haproxy.log//definition Haproxy log file
[Email protected] ~]# Vim/etc/sysconfig/rsyslog
Modify:
Syslogd_options= "-C 2-r-M 0"//Allow remote write
[Email protected] ~]#/etc/init.d/rsyslog restart
[Email protected] ~]#/etc/init.d/haproxy restart
Verify:
[[email protected] ~]# tail-f/var/log/haproxy.log//view Log
Third, verify:
Client input:
Http://192.168.1.1/index.html
Keep on refreshing!
View:
[Email protected] ~]# tail-f/var/log/haproxy/haproxy.log
Log records Client access information
Logs record Haproxy start/stop information
Haproxy+keepalived
Compiling and installing keepalived
[Email protected] keepalived-1.2.13]#./configure--prefix=/--withkernel-
dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
[[email protected] keepalived-1.2.13]# make && make install
[[Email protected] ~] #chkconfig--add keepalived
[[Email protected] ~] #chkconfig keepalived on
[[Email protected] ~] #cp/etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf.bak
[[Email protected] ~] #vim/etc/keepalived/keepalived.conf
[Email protected] conf]# vim/etc/keepalived/keepalived.conf
! Configuration File for Keepalived
Global_defs {
Notification_email {//notification e-mail
}
Vrrp_instance vi_1 {//VRRP hot spare
State master backup/hot standby status Master, backup supplemented
Nopreempt//No preemption, Master does not transfer after recovery
interface eth0//physical Interface hosting VIP
VIRTUAL_ROUTER_ID 51//Virtual route number, one per group
Priority 55/priorities, bigger, higher priority
Advert_int 1//Heartbeat frequency, unit seconds
Authentication {//authentication information, consistent within each group
Auth_type PASS//authentication type
Auth_pass 1111//Authentication string
}
virtual_ipaddress {//Drift address VIP. can have multiple
192.168.56.10
}
Notify_master "/etc/init.d/haproxy start"//Become master after
The action of the line
Notify_backup "/etc/init.d/haproxy Stop"//becomes backup after execution
The action
Notify_fault "/etc/init.d/haproxy stop" after//fault executed
Action
}
[[email protected] ~]#/etc/init.d/keepalived start
[[Email protected] ~] #ip addr Show
inet 192.168.56.201/24 BRD 192.168.56.255 Scope Global eth0
inet 192.168.56.10/32 Scope Global eth0
[[Email protected] ~] #netstat-anput | grep 80
This article is from the "11000174" blog, please be sure to keep this source http://11010174.blog.51cto.com/11000174/1890827
Haproxy Proxy Web Service