[置顶] Keepalived+HAProxy配置高可用负载均衡,解决keepalived无法安装问题

时间:2022-05-18 03:18:31
 

1 系统环境

Keepalived-1.2.2 http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

OS: CentOS 5.7 

HAProxyhttp://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.20.tar.gz

我的ip环境:
haproxy keepalived 主:192.168.200.104:1080
haproxy keepalived 备:192.168.200.101:1080
vip192.168.200.181
web192.168.200.104:8080 192.168.200.104:9090

2 Keepalived安装 

1.tar -zxvf keepalived-1.2.2.tar.gz

2.cd keepalived-1.2.2

3../configura --prefix=/

执行这一步可能出现如下错误:

configure: error:

!!! OpenSSL is not properly installed on your system. !!!

!!! Can not include OpenSSL headers files.

解决办法:

[root@master ~]# yum -y install openssl-devel


 

4.make && make install
5.cd /etc/keepalived/
6.mv keepalived.conf keepalived.conf.default
7.vi keepalived.conf

vrrp_script chk_http_port {

script /etc/keepalived/check_haproxy.sh"

Interval 2

weight 2

global_defs {

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER #192.168.200.104,在192.168.200.101上则改为BACKUP

interface eth0

virtual_router_id 51

priority 200 #192.168.200.104,在192.168.200.101上则改为100
advert_int 1
authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_http_port

}

virtual_ipaddress {

192.168.200.181

}

}

8.vi /etc/keepalived/check_haproxy.sh

#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi


9.chmod 755 /etc/keepalived/check_haproxy.sh

3 安装HAProxy

haproxy的安装见链接:http://blog.csdn.net/zilong00007/article/details/7275235

4 测试

1./etc/init.d/keepalived start (这条命令会自动把haproxy启动)

2.停掉主上的haproxy3秒后keepalived会自动将其再次启动
3.停掉主的keepalived,备机马上接管服务

  相关详细文档请见我的资源(文档形式已上传)