HAproxy+keepalived/pacemaker
Haproxy+keepalived的结合,实现负载均衡与高可用的完美结合,既解决了提供服务的负载均衡,又解决了作负载均衡器的单点故障问题。这样架构就很健壮了。
材料准备: keepalived-1.2.20.tar.gz
步骤:
#tar -zxf keepalived-1.2.20.tar.gz :解压源码包,进行编译安装
#cd keepalived-1.2.20
#yum install -y gcc openssl-devel :安装编译环境以及依赖性
#./configure--prefix=/usr/local/keepalived :指定安装位置
# make ---> make install
作相应文件位置的软件连,有些文件的位置不对。需要连接解决。
# ln -s /usr/local/keepalived/etc/keepalived /etc/
# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# ln -s /usr/local/keepalived/bin/genhash /bin/
#ln -s /usr/local/keepalived/sbin/keepalived /sbin/
#vim keepalived.conf :编辑keepalived的配置文件
vrrp_script check_haproxy {
script "/opt/check_haproxy.sh" :这个脚本要自己写
interval 2 监控间隔为2s
weight 2 权值为2
}
global_defs { :定义全局定义
notification_email {
root@localhost 发邮件给本机root用户
}
notification_email_from keepalived@localhost 邮件发送者
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 16
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.10.200 :虚拟ip地址
}
track_script {
check_haproxy
}
}
MASTER Keepalived配置,BAKUP的配置还需要作两部分修改,1.state BACKUP 2.priority 50
还需要自己编写脚本:
#vim /opt/check_haproxy.sh
#!/bin/bash
/etc/init.d/haproxy status &> /dev/null ||/etc/init.d/haproxy restart&>/dev/null
if
[ $? -ne 0 ]
then
/etc/init.d/keepalived stop &> /dev/null
fi
#scp -r keepalived/ [email protected]:/usr/local/
#scp /opt/check_haproxy.sh [email protected]:/opt/
#scp keepalived.conf [email protected]:/etc/keepalived/
#scp haproxy.cfg [email protected]:/etc/haproxy/
#/etc/init.d/keepalivedstart :两边都启动keepalived服务。
即可实现高可用负载。用上面的步骤来,依次关闭realserver的服务和负载调度器。看是否会有接管服务。
实现haproxy+pacemaker的完美结合,和上面一样,也是为了解决单点故障和负载均衡问题。这种搭配更容易也更简单。
第二种方法:使用pacemaker
材料:crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm
#yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm :解决依赖性的pssh-2.3.1-2.1.x86_64.rpm
#yum install -y pacemaker 安转pacemaker服务
#cd /etc/corosync/
#cp corosync.conf.example corosync.conf 编辑corosync的配置文件
#vim corosync.conf
totem {
version: 2
secauth: off
threads: 0
interface {
ringnumber: 0
bindnetaddr: 172.25.10.0 :修改此处的网段
mcastaddr: 226.94.1.1
mcastport: 2134 :组播地址端口号
ttl: 1
}
service {
name:pacemaker
ver:0
}
#scp corosync.conf [email protected]:/etc/corosync/ 传给备份机
#/etc/init.d/corosync start 两边的主机corosync同时启动。
在haproxy1上输入#crm_mon进行监控,
在haproxy2上进行操作,添加虚拟ip和haproxy服务,并且加到同一个组里面。
# crm
> property stonith-enabled=false :去除脑裂,因为没有装fence机制,
>primitive vip ocf:heartbeat:IPaddr2params ip=172.25.10.100 cidr_netmask=32 op monitor interval=30s
:添加VIP,并且监控时间为每个30s
>primitivehaproxy lsb:haproxy op monitor interval=30s
添加:haproxy服务,监控时间同样为30s
> group hell vip haproxy
:将vip haproxy放到同一个组里面,组的名字叫做hell;
>commit 提交保存生效。