本节教大家在centos6系统中配置HAProxy+KeepAlived WEB群集的方法。
网上的好多文章,都是在centos5.x中配置的。
1、环境
CentOS6 (linux Kernel 2.6.32-71.el6.i686)
haproxy 1.4.19
keepalived 1.1.17
2、需要的IP配置
Master IP:10.0.0.8
Backup IP:10.0.0.6
RealServer IP:10.0.0.5、10.0.0.6(兼)
3、安装MASTER机器上的keepalived
复制代码 代码示例:
[root@RServer2 /]#mkdir /soft
[root@RServer2 /]#cd /soft
[root@RServer2 soft]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
[root@RServer2 soft]# tar -zxvf keepalived-1.1.17.tar.gz
[root@RServer2 soft]# cd keepalived-1.1.17
[root@RServer2 soft]# ./configure
[root@RServer2 keepalived-1.1.17]# make;make install
一般可能出现的错误:
1)、OpenSSL,提示可能如下
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.
解决:
运行yum -y installopenssl-devel
2)、提示没有gcc编译器
解决:运行
复制代码 代码示例:
yum installncurses-devel gcc gcc-c++ make rpm-build
[root@RServer2 keepalived-1.1.17]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[root@RServer2 keepalived-1.1.17]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@RServer2 keepalived-1.1.17]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@RServer2 keepalived-1.1.17]# mkdir -p /etc/keepalived/
[root@RServer2 keepalived-1.1.17]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@RServer2 keepalived-1.1.17]# chmod +x /etc/init.d/keepalived
5、编辑Master机器上的/etc/keepalived/keepalived.conf:
打开编辑器,比如VI或gedit /etc/keepalived/keepalived.conf的内容如下,自己修改程序要的内容。
#Master服务器上的配置 /etc/keepalived/keepalived.conf
复制代码 代码示例:
global_defs {
notification_email {
leekexi@gmail.com #可以多个地址
}
notification_email_from leekexi@gmail.com
smtp_server smtp.gmail.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #标示状态为MASTER 备份机为BACKUP
interface
eth0
virtual_router_id 51
priority 100 #MASTER权重要高于BACKUP 比如BACKUP为99
advert_int 1
authentication {
auth_type PASS #主从服务器验证方式
auth_pass 1111
}
virtual_ipaddress {
10.0.0.8 #可以多个虚拟IP,换行即可
}
}
#虚拟服务器 80端口的配置
virtual_server 10.0.0.8 80 {
delay_loop 6
lb_algo rr
protocol TCP
real_server 10.0.0.5 80 {
weight 5
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.0.0.6 80 {
weight 5
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
6、编辑Master机器上安装HAProxy:
下载地址:http://haproxy.1wt.eu/download/1.4/src/
选择合适的版本,一般选择比较新的版本会理想一些。
假设下载到/soft目录下
复制代码 代码示例:
#tar zcvf haproxy-1.4.19.tar.gz
# cd haproxy-1.4.19
# make TARGET=linux26 PREFIX=/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
在安装目录/usr/local/haproxy 建立conf文件夹 之后在该文件夹建立 haproxy.cfg 配置文件 用vi或是gedit打开haproxy.cfg配置文件
配置文件的内容自行定义。
复制代码 代码示例:
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 501
gid 501
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
debug
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats uri /haproxy-stats
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 10.0.0.8:80
server web1_10.0.0.6 10.0.0.6:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2_10.0.0.5 10.0.0.6:80 cookie app1inst2 check inter 2000 rise 2 fall 5
cookie 1表示serverid为1,
check inter 1500 是检测心跳频率
rise 2是2次正确认为服务器可用
fall 3是3次失败认为服务器不可用
weight代表权重
加上日志支持
编辑/etc/syslog.conf在最下边增加
复制代码 代码示例:
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
编辑 /etc/sysconfig/syslog修改:
SYSLOGD_OPTIONS="-r -m 0"
重启日志服务
#service syslog restart
BACKUP机器上与MASTER机器的差别
BACKUP的安装和Master的安装基本一致,只是一些参数略有变化。
变化的参数如下:
1、keepalived.conf文件中
原:state MASTER #标示状态为MASTER 备份机为BACKUP
state BACKUP #标示状态改为BACKUP
原:priority 100 #MASTER权重要高于BACKUP 比如BACKUP为99
priority 99 #MASTER权重要高于BACKUP,MASTER为100,那么BACKUP略小于,比如99
开机启动HAProxy
在开机时加载,创建启动脚本:
# vim /etc/rc.d/init.d/haproxy
复制代码 代码示例:
#! /bin/sh
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.conf
PIDFILE=$PROGDIR/run/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n "Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo "."
}
stop()
{
echo -n "Stopping $DESC: $PROGNAME"
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo "."
}
restart()
{
echo -n "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
加上可执行权限:
# chmod +x /etc/rc.d/init.d/haproxy
以后即可使用 service haproxy start|stop|restart 来控制服务的启动、停止、重启。
加载到开机服务启动列表
# chkconfig --add haproxy
维护HAProxy和Keepalived
1)、keepAlived维护
启动:/etc/init.d/keepalived start
停止:/etc/init.d/keepalived stop
重启:/etc/init.d/keepalived restart
2)、haproxy维护
启动:service haproxy start
停止:service haproxy stop
重启:service haproxy restart