环境如下:
192.168.2.199 vm1.example.com HA主服务器
192.168.2.202 vm2.example.com HA备服务器
192.168.2.205 vm3.example.com
192.168.2.175 vm4.example.com
其中1和2做HA,3和4做httpd的lvs。虚拟IP为192.168.2.213
1、使用keepalixed+haproxy负载均衡
lvs 适合应用层负载均衡,nginx haproxy适合做网站的负载均衡,haproxy 吞吐量比较高,一般和lvs结合:user->vip+proxy->realserver[root@vm1 ~]# /etc/init.d/keepalived stop 关闭1和2的keepalived
lftp i:~> get pub/docs/haproxy/haproxy-1.4.24.tar.gz
[root@vm1 ~]# yum install rpm-build pcre-devel -y
[root@vm1 ~]# rpmbuild -tb haproxy-1.4.24.tar.gz
[root@vm1 ~]# rpm -ivh /root/rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm
[root@vm1 ~]# mkdir /usr/share/haproxy
[root@vm1 ~]# vim /etc/haproxy/haproxy.cfg
option redispatch 更改
stats uri /status 添加
listen www.haproxy.com *:80
balance roundrobin
server web1 192.168.2.205:80 cookie app1inst1 check inter 2000 rise 2 fall 3
server web2 192.168.2.175:80 cookie app1inst2 check inter 2000 rise 2 fall 3
之后的全部删除。
[root@vm1 ~]# /etc/init.d/haproxy start
在真机中写入解析,192.168.2.199 www.haproxy.com
2、打开日志记录
[root@vm1 ~]# vim /etc/haproxy/haproxy.cfgglobal
log 127.0.0.1 local0 指定日志设备
#log 127.0.0.1 local1 notice
log 127.0.0.1 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
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
#stats uri /status
listen www.haproxy.com *:80 这里真机访问测试的话,做vm1的解析
balance roundrobin
server web1 192.168.2.205:80 cookie app1inst1 check inter 2000 rise 2 fall 3 #2秒检测,成功判定2次,失败判定3次
server web2 192.168.2.175:80 cookie app1inst2 check inter 2000 rise 2 fall 3
listen status *:8080
stats enable
stats uri /status
stats auth admin:mmmmmm 认证
stats refresh 5s 刷新时间
[root@vm1 ~]# vim /etc/rsyslog.conf 添加
$ModLoad imudp
$UDPServerRun 514
#### RULES ####
local0.* /var/log/haproxy.log
[root@vm1 ~]# /etc/init.d/rsyslog restart
[root@vm1 ~]# /etc/init.d/haproxy restart
[root@vm1 ~]# cat /var/log/haproxy.log
浏览器访问http://192.168.2.199:8080/status
3、结合keepalived高可用和haproxy负载均衡
[root@vm1 ~]# vim /etc/keepalived/keepalived.conf 主结点配置! Configuration File for keepalived
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy"
interval 2 检测脚本运行频率
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@vm1.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 94
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.213
}
track_script {
check_haproxy
}
}
脚本的编写
[root@vm1 ~]# vim /etc/keepalived/check_haproxy
#!/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
[root@vm1 ~]# chmod +x /etc/keepalived/check_haproxy
[root@vm1 ~]# scp /etc/keepalived/check_haproxy vm2.example.com:/etc/keepalived/
[root@vm1 ~]# scp rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm vm2.example.com:
修改备结点的配置文件
[root@vm2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy"
interval 2
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@vm2.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 94
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.213
}
track_script {
check_haproxy
}
}
备结点安装haproxy
[root@vm2 ~]# rpm -ivh haproxy-1.4.24-1.x86_64.rpm
[root@vm2 ~]# mkdir /usr/share/haproxy
[root@vm1 ~]# scp /etc/haproxy/haproxy.cfg vm2.example.com:/etc/haproxy/ 发送配置文件
[root@vm2 ~]# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
#### RULES ####
local0.* /var/log/haproxy.log
[root@vm2 ~]# /etc/init.d/rsyslog restart
启动服务测试
[root@vm1 ~]# /etc/init.d/keepalived start
[root@vm2 ~]# /etc/init.d/keepalived start
这时,在主结点上和备结点就可以看到haproxy启动,主结点拥有虚拟IP:213,使用ip addr show查看,同时查看日志tail -f /var/log/messages
访问192.168.2.213就可以轮寻,这时关闭1的keepalived,2就成为主结点,拥有IP,不影响访问。再次开启1的keepalived会回切回去。
即keep每隔2s检测ha状态,没启动则重启,重启不了就关闭keep,让他跳到备结点。所谓的虚拟ip是跟着haproxy正常的主机的,因此保证了高可用。
最后自己做遗留得问题是:虚拟ip可以添加,但是只有自己可以用w3m访问,真机访问不了,也ping不同。
日志如下
Jun 11 11:31:17 ha1 Keepalived_vrrp[9217]: VRRP_Instance(VI_1) setting protocol VIPs.
Jun 11 11:31:17 ha1 Keepalived_vrrp[9217]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.213
Jun 11 11:31:17 ha1 Keepalived_healthcheckers[9216]: Netlink reflector reports IP 192.168.0.213 added
Jun 11 11:31:22 ha1 Keepalived_vrrp[9217]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.0.213
最后拔掉网线正常了,原因是局域网中存在该虚拟ip。