Haproxy+Keepalived+Nginx
高可用负载均衡
官方网站:
http://haproxy.com/
http://www.keepalived.org/
http://nginx.org/
实验环境
OS:CentOS6.5 x64
软件版本:
haproxy-1.5.3
keepalived-1.2.13
nginx-1.6.2
主机:
haproxy VIP(keepalived): 192.168.8.100
haproxy1+keepalive-master: 192.168.8.80,192.168.7.70
haproxy2+keepalive-backup: 192.168.8.81,192.168.7.71
nginx1: 192.168.7.100
nginx2: 192.168.7.200
Nginx
请参看nginx-1.6.2+php-5.5.19+mariadb-10.0.14
HAProxy
一.安装编译依赖库
[root@haproxy1 ~]# yum -y install pcre-devel openssl-devel zlib-devel
二.编译安装
[root@haproxy1 ~]# tar -xvf haproxy-1.5.3.tar.gz -C /usr/local/src/
[root@haproxy1 ~]# cd /usr/local/src/haproxy-1.5.3/
[root@haproxy1 haproxy-1.5.3]# make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
[root@haproxy1 haproxy-1.5.3]# make install
三.配置
1.init控制脚本
[root@haproxy1 haproxy-1.5.3]# ln -s /usr/local/sbin/haproxy* /usr/sbin/
[root@haproxy1 haproxy-1.5.3]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy1 haproxy-1.5.3]# chmod 755 /etc/init.d/haproxy
[root@haproxy1 ~]# chkconfig --add haproxy
[root@haproxy1 ~]# chkconfig haproxy on
[root@haproxy1 ~]# chkconfig --list haproxy
haproxy 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2.配置文件/etc/haproxy/haproxy.cfg
http://www.haproxy.org/download/1.7/doc/configuration.txt
[root@haproxy1 haproxy-1.5.3]# mkdir /etc/haproxy
[root@haproxy1 haproxy-1.5.3]# mkdir /usr/share/haproxy
[root@haproxy1 haproxy-1.5.3]# cp examples/haproxy.cfg /etc/haproxy
[root@haproxy1 haproxy-1.5.3]# 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 loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000
listen nginx-http 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server nginx1 192.168.7.100:80 cookie nginx1 check inter 2000 rise 2 fall 5
server nginx2 192.168.7.200:80 cookie nginx2 check inter 2000 rise 2 fall 5
listen nginx-https 0.0.0.0:443
cookie SERVERID rewrite
balance roundrobin
server nginx1 192.168.7.100:443 cookie nginx1 check inter 2000 rise 2 fall 5
server nginx2 192.168.7.200:443 cookie nginx2 check inter 2000 rise 2 fall 5
补充:如果是调度数据库,如Redis, MariaDB, MongoDB等非web应用时,则非要将模式改为mode tcp,同时cookie也会失效,重启服务后会提示,所以也要删除对应的cookie选项。
[root@haproxy1 haproxy]# /etc/init.d/haproxy restart
Shutting down haproxy: [ OK ]
Starting haproxy: [ OK ]
[root@haproxy1 haproxy]# netstat -tunlp|grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3768/haproxy
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 3768/haproxy
udp 0 0 0.0.0.0:55509 0.0.0.0:*