Discuss New Concept,New Technic,New Tools, Including EAI,BPM,SOA,Tibco,IBM MQ,Tuxedo, Cloud,Hadoop,NoSQL,J2EE,Ruby,Scala,Python, Performance,Scalability,Distributed,HA, Social Network,Machine Learning.

Load Balancing

May 102012
 

original:http://bbs.chinaunix.net/thread-1978418-1-1.html

wget http://download.fedora.redhat.co … 14.6-1.el5.i386.rpm
wget ftp://rpmfind.net/linux/EPEL/5Se … 14.6-1.el5.i386.rpm
下载源码包编译安装或者直接YUM安装
yum –y install haproxy

3.配置haproxy
vi /etc/haproxy/haproxy.cfg

global
log 127.0.0.1   local3
log 127.0.0.1   local1 notice
maxconn         32000
chroot /var/lib/haproxy
user    haproxy
group haproxy
daemon
ulimit-n        65535
defaults
log     global
mode    http
option  httplog
option  dontlognull
retries 3
option redispatch
maxconn 32000
contimeout      5000
clitimeout      50000
srvtimeout      50000
listen  web  0.0.0.0:80
mode http
stats enable
balance roundrobin
cookie SRV insert indirect nocache           #cookie和session都可以用,这里有6个参数,这个经实验最好使。
option httpclose
option forwardfor
option httpchk HEAD /index.php HTTP/1.0           #检查网页文件是否存在,如果返回200证明WEB服务良好,返回其他证明WEB服务不好(服务器死机了,APACHE停了,主页删除 了…….,那就自动切换到从服务器)
server server1 192.168.0.168:80 cookie A check inter 2000 rise 2 fall 5
server server2 192.168.0.169:80 cookie A check inter 2000 rise 2 fall 5

slave机器上同样配置如此

4.在两台haproxy机器上安装配置好keepalived
Keepalived配置
VIP 192.168.0.150
Realserver  192.168.0.149 (MASTER)
RealServer  192.168.9.170 (SLAVER)

wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure
make && make install
mkdir /etc/keepalived
ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf

分别在两台机器上配置keepalived.conf文件:
global_defs {
notification_email {
opstom@163.com
}
notification_email_from opstom@163.com
smtp_server 192.168.0.162
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER                      \\\注意:第二台服务器写:state SLAVE
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass askwan
}
virtual_ipaddress {
192.168.0.150
}

分别在两台机器上执行:
keepalived

5调试:
tail /var/log/messges
Apr 22 08:32:22 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:32:22 db1 Keepalived: daemon is already running
Apr 22 08:32:24 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:32:24 db1 Keepalived: daemon is already running
Apr 22 08:32:46 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:32:46 db1 Keepalived: daemon is already running
Apr 22 08:37:42 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:37:42 db1 Keepalived: daemon is already running
Apr 22 08:39:34 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:39:34 db1 Keepalived: daemon is already running
Apr 22 08:41:20 db1 sshd(pam_unix)[6317]: session opened for user root by root(uid=0)
Apr 22 08:41:32 db1 Keepalived: Terminating on signal
Apr 22 08:41:32 db1 Keepalived_vrrp: Terminating VRRP child process on signal
Apr 22 08:41:32 db1 Keepalived: Stopping Keepalived v1.1.15 (04/22,200
Apr 22 08:42:03 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:42:03 db1 Keepalived_vrrp: Using MII-BMSR NIC polling thread…
Apr 22 08:42:03 db1 Keepalived_vrrp: Registering Kernel netlink reflector
Apr 22 08:42:03 db1 Keepalived_vrrp: Registering Kernel netlink command channel
Apr 22 08:42:03 db1 Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 22 08:42:03 db1 Keepalived: Starting VRRP child process, pid=6358
Apr 22 08:42:03 db1 Keepalived_vrrp: Opening file ‘/usr/local/keepalived/etc/keepalived/keepalived.conf’.
Apr 22 08:42:03 db1 Keepalived_vrrp: Configuration is using : 33290 Bytes
Apr 22 08:42:03 db1 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,]
Apr 22 08:42:04 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.150
Apr 22 08:42:10 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.150

可以看到此机此时处于MASTER状态

slave机调试信息:
Apr 20 08:43:28 db2 Keepalived: Starting Keepalived v1.1.15 (04/22,200
Apr 22 08:43:28 db2 Keepalived_vrrp: Using MII-BMSR NIC polling thread…
Apr 22 08:43:28 db2 Keepalived_vrrp: Registering Kernel netlink reflector
Apr 22 08:43:28 db2 Keepalived_vrrp: Registering Kernel netlink command channel
Apr 22 08:43:28 db2 Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 22 08:43:28 db2 Keepalived: Starting VRRP child process, pid=5857
Apr 22 08:43:28 db2 Keepalived_vrrp: Opening file ‘/usr/local/keepalived/etc/keepalived/keepalived.conf’.
Apr 22 08:43:28 db2 Keepalived_vrrp: Configuration is using : 36052 Bytes
Apr 22 08:43:28 db2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 22 08:43:28 db2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,]

可以看到此机此时处于BACKUP状态

5.测试:       用浏览器访问192.168.0.150
192.168.0.149宕机后192.168.0.170接管
192.168.0.149恢复后 192.168.0.170移交服务权限给192.168.0.149