54yg 发表于 2014-6-3 10:11:59

haproxy高可用负载均衡

常见的做负载均衡的机制:nginx,lvs,haproxy
适合做网站调度:nginx,haproxy
适合做应用层,如mysql数据库:lvs
haproxy特点:吞吐量很高

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作

实验环境:rhel6.5 selinux and iptables disabled
实验主机:192.168.22.138 haproxy
192.168.2.135 haproxy
192.168.2.116web1(real server)
192.168.2.160web2 (real server)
Virtual server 192.168.2.252

Haproxy软件包下载地址:http://haproxy.1wt.eu/#down
下载包:haproxy-1.4.24.tar.gz

Haproxy负载均衡
# yum install rpm-build -y

将tar包制作成rpm包:
# rpmbuild -tb haproxy-1.4.24.tar.gz
error: Failed build dependencies:   #需要pcre依赖性
pcre-devel is needed by haproxy-1.4.24-1.x86_64
# yum install pcre-devel -y
# rpm -ivh /root/rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm
Preparing...                ###########################################
   1:haproxy                ###########################################

Haproxy配置文件中需要此目录,故创建:
# mkdir /usr/share/haproxy

# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log 127.0.0.1local0#制定日志设备
log 127.0.0.1local1 notice #制定日志的类型,还有err,warning,debug

#log loghostlocal0 info
maxconn 4096   #并发最大链接数量
chroot /usr/share/haproxy#jail目录,需要手动创建
uid 99   
gid 99
daemon    #后台运行
#debug
#quiet

defaults
logglobal
modehttp         #默认使用http的7层模式
optionhttplog      #http的日志格式
optiondontlognull#禁用空链接日志
retries3            #重试3次失败认为服务器不可用
option redispatch    #当client链接到挂掉的机器时,重新分配到健康的主机
maxconn2000
contimeout5000   #链接超时
clitimeout50000#客户端超时
srvtimeout50000#服务器短超时
stats uri /status      #haproxy的监控页面

listenwww.example.com *:80    #监听的实例名称,地址和端口
balanceroundrobin      #负载均衡算法
serverweb1 192.168.2.116:80 cookie app1inst1 check inter 2000 rise 2 fall 3
serverweb2 192.168.2.160:80 cookie app1inst2 check inter 2000 rise 2 fall 3


#cookie app1inst1:表示 serverid 为 app1inst1
#check inter 2000:检测心跳频率
#rise 2:表示 2 次正确认为服务器可用
#fall 3:表示 3 次失败认为服务器不可用

# /etc/init.d/haproxy start

测试:
访问 http://192.168.2.138 测试负载
刷新出现页面调度则正确

后端服务健康检查:
# /etc/init.d/httpd stop
刷新页面,服务全部下放给了server60,没有出现错误页面则成功

# /etc/init.d/httpd start

访问 haproxy 监控页面:http://192.168.2.138/status


监控页面添加认证:
# vim /etc/haproxy/haproxy.cfg
添加:
listen stats *:8080
stats enable
stats uri /status
stats auth admin:westos
stats refresh 5s
删除或注释掉以下带#号的一行:
      clitimeout50000#客户端超时
      srvtimeout50000#服务器短超时
      #stats uri /status      #haproxy的监控页面

添加日志:
# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514#打开这两项,表示以UDP协议记录日志
#### RULES ####   #在rules中添加如下规则

local0.*                                                /var/log/haproxy.log

# /etc/init.d/haproxy restart
# /etc/init.d/rsyslog start

http://192.168.2.138:8080/status#查看界面



haproxy+keepalived实现高可用
    HAProxy是高性能的代理服务器,其可以提供7层和4层代理,具有healthcheck,负载均衡等多种特性,性能卓越,包括Twitter,Reddit,StackOverflow,GitHub在内的多家知名互联网公司在使用。   KeepAlived是一个高可用方案,通过VIP(即虚拟IP)和心跳检测来实现高可用。其原理是存在一组(两台)服务器,分别赋予Master,Backup两个角色,默认情况下Master会绑定VIP到自己的网卡上,对外提供服务。Master,Backup会在一定的时间间隔向对方发送心跳数据包来检测对方的状态,这个时间间隔一般为2秒钟,如果Backup发现Master宕机,那么Backup会发送ARP包到网关,把VIP绑定到自己的网卡,此时Backup对外提供服务,实现自动化的故障转移,当Master恢复的时候会重新接管服务。
在主节点上:
# vim keepalived.conf
! Configuration File for keepalived

vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}

global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from keepalived@server38.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 27
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      192.168.2.252   #虚拟ip
    }
编写检测脚本:
# vim /etc/keepalived/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/keepalivedstop &>/dev/null
fi

# chmod +x check_haproxy.sh

# scp /etc/haproxy/haproxy.cfg 192.168.2.135:/etc/haproxy/
# scp check_haproxy.sh 192.168.2.135:/etc/keepalived/
#scp rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm 192.168.2.135:

在备节点上:
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}

global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from keepalived@server35.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 27
    priority 50
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      192.168.2.25
    }

    track_script {
      check_haproxy
    }
}

# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514#打开这两项,表示以UDP协议记录日志
#### RULES ####   #在rules中添加如下规则

local0.*                                                /var/log/haproxy.log

# rpm -ivh haproxy-1.4.24-1.x86_64.rpm

# mkdir /usr/share/haproxy/

关闭掉所有服务:
# /etc/init.d/httpd stop
# /etc/init.d/haproxy stop
# /etc/init.d/keepalived stop
# /etc/init.d/httpd stop
# /etc/init.d/haproxy stop
# /etc/init.d/keepalived stop

测试:
# /etc/init.d/keepalived start

# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:61:4e:92 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.138/24 brd 192.168.2.255 scope global eth0
    inet 192.168.2.252/32 scope global eth0
    inet6 fe80::5054:ff:fe61:4e92/64 scope link
       valid_lft forever preferred_lft forever

http://192.168.2.252
出现以下效果证明成功:
1.刷新界面变换
2.# /etc/init.d/keepalived stop
# ip addr show #备机主动接管
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:81:4b:62 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.135/24 brd 192.168.2.255 scope global eth0
    inet 192.168.2.252/32 scope global eth0
    inet6 fe80::5054:ff:fe81:4b62/64 scope link
       valid_lft forever preferred_lft forever
   刷新界面变换

页: [1]
查看完整版本: haproxy高可用负载均衡