haproxy+keepalived搭建
1、介绍:【HAProxy】是高性能的代理服务器,其可以提供7层和4层代理,具有healthcheck,负载均衡等多种特性,性能卓越【KeepAlived】是一个高可用方案,通过VIP(即虚拟IP)和心跳检测来实现高可用,也通过该vip实现访问。其原理是存在一组(两台)服务器默认情况下Master会绑定VIP到自己的网卡上,对外提供服务。如果Backup发现Master宕机,那么Backup会发送ARP包到网关,把VIP绑定到自己的网卡,此时Backup对外提供服务,实现自动化的故障转移,当Master恢复的时候会重新接管服务。
2、环境:web1:192.168.1.78web2:192.168.1.241web3:192.168.1.133web4:192.168.1.244haproxy+keepalived1:192.168.1.22haproxy+keepalived1:192.168.1.9vip1:192.168.1.189(www.inbank.com)vip2:192.168.1.199(image.inbank.com)
3、需求:默认情况下,第一台负载均衡器主要分发www.baison.com.cn的请求,第二台负载均衡器主要分发img.baison.com.cn的请求。任意一台宕机都不会影响网站分发。这样不会导致服务器浪费。
4、keepavlied+haproxy安装略,直接进入haproxy和keepalived配置【192.168.1.22】上的haproxy配置:# cat /usr/local/haproxy/etc/haproxy.cfg |grep -v "#"|sed '/^$/d'
globallog 127.0.0.1 local1 noticemaxconn 4096chroot /usr/share/haproxyuid 99gid 99daemonpidfile /usr/local/haproxy/haproxy.piddefaultslog globalmode httpretries 3maxconn 2000contimeout 5000clitimeout 50000srvtimeout 50000stats uri /haproxy-statsbalance roundrobinfrontend WEB_SITEbind :80acl web hdr(host) -i www.inbank.comacl img hdr(host) -i image.inbank.comuse_backend webserver if webuse_backend imgserver if imgbackend webservermode httpbalance roundrobinserver web_1 192.168.1.78:80 check inter 2000 fall 5 weight 1server web_2 192.168.1.241:80 check inter 2000 fall 5 weight 1backend imgservermode httpoption httpchk /index.phpbalance roundrobinserver web_1 192.168.1.133:80 check inter 2000 fall 5 weight 1server web_1 192.168.1.244:80 check inter 2000 fall 5 weight 1
192.168.1.22上的keepalived配置# cat keepalived.conf|grep -v "#"|sed '/^$/d'
!Configuration File for keepalivedglobal_defs {router_id LVS_DEVEL}vrrp_script chk_haproxy {script "/usr/local/keepalived/check_haproxy.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 88priority 100advert_int 1authentication {auth_type PASSauth_pass 5555}track_script {chk_haproxy}virtual_ipaddress {192.168.1.189}}vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 89 ---#id不能跟VI_1一样priority 99---#advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.1.199}}
检测脚本,为了防止haproxy服务关闭导致keepalived不自动切换。#vim /usr/local/keepalived/check_haproxy.sh#!/bin/bashif [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfgfisleep 2if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then/etc/init.d/keepalived stopfi
4、启动keepalived服务和haproxy,然后查看日志,看看是否有2个viptail -n 30 /var/log/messages
Oct 23 13:49:13 master Keepalived_vrrp: VRRP_Instance(VI_2) Entering BACKUP STATEOct 23 13:49:13 master Keepalived_vrrp: VRRP sockpool: Oct 23 13:49:13 master Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEOct 23 13:49:14 master Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEOct 23 13:49:14 master Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Oct 23 13:49:14 master Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.189Oct 23 13:49:14 master avahi-daemon: Registering new address record for 192.168.1.189 on eth0.Oct 23 13:49:19 master Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.189
##从192.168.1.22上可以看出,VI_2已经进入了backup角色,VI_1已经进入了master,并且已经绑定了192.168.1.189在eth0网卡
# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuelink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000link/ether 00:0c:29:06:ed:78 brd ff:ff:ff:ff:ff:ffinet 192.168.1.22/24 brd 192.168.1.255 scope global eth0inet 192.168.1.189/32 scope global eth0inet6 fe80::20c:29ff:fe06:ed78/64 scope linkvalid_lft forever preferred_lft forever3: sit0: <NOARP> mtu 1480 qdisc nooplink/sit 0.0.0.0 brd 0.0.0.0
####从 192.168.1.9 haproxy和keepalived配置haproxy不变,只是keepalived配置里的角色,权限需要跟master兑换下就可以了vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 88priority 99advert_int 1authentication {auth_type PASSauth_pass 5555}track_script {chk_haproxy}virtual_ipaddress {192.168.1.189}}
vrrp_instance VI_2 {state MASTERinterface eth0virtual_router_id 89priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.1.199}}
启动haproxy和keepalived服务,查看vip是否已经绑定在eth0网卡Oct 23 14:00:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATEOct 23 14:00:26 localhost Keepalived_vrrp: VRRP sockpool: Oct 23 14:00:27 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Transition to MASTER STATEOct 23 14:00:28 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Entering MASTER STATEOct 23 14:00:28 localhost Keepalived_vrrp: VRRP_Instance(VI_2) setting protocol VIPs.Oct 23 14:00:28 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.1.199
# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueuelink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000link/ether 00:0c:29:2b:be:1a brd ff:ff:ff:ff:ff:ffinet 192.168.1.9/24 brd 192.168.1.255 scope global eth0inet 192.168.1.199/32 scope global eth0inet6 fe80::20c:29ff:fe2b:be1a/64 scope linkvalid_lft forever preferred_lft forever3: sit0: <NOARP> mtu 1480 qdisc nooplink/sit 0.0.0.0 brd 0.0.0.0
四、web上的配置在对应web上新增相关虚拟机 www.inbank.com,image.inbank.com此处为了实验方便,就直接yum 安装了apache,在apache配置上的配置如下:
##web1 web2NameVirtualHost *:80<VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot /var/www/html/inbankServerName www.inbank.comErrorLog logs/dummy-host.example.com-error_logCustomLog logs/dummy-host.example.com-access_log common</VirtualHost>
###web3 web4类似在客户端修改hosts,新增以下2条记录192.168.1.189 www.inbank.com192.168.1.199 image.inbank.com然后分别访问这2个网址,如下:# for i in `seq 1 4`;do curl http://image.inbank.com;doneimg_133img_244img_133img_244# for i in `seq 1 4`;do curl http://www.inbank.com;doneinbank_78inbank_241inbank_78inbank_241
--如果出现以上的话,那就木有问题了
接下来测试高可用,停止master上的keepalived服务# /etc/init.d/keepalived stopStopping keepalived: [ OK ]
然后查看backup上的状态Oct 23 14:00:33 localhost Keepalived_vrrp: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.1.199Oct 23 14:08:04 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEOct 23 14:08:05 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEOct 23 14:08:05 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Oct 23 14:08:05 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.189
说明BACKUP已经接替MASTER了,然后再访问这2个网址,没问题的话,就大功告成了
##查看haproxy web 监控页面http://192.168.1.189/haproxy-stats或者http://192.168.1.199/haproxy-stats
#另,内核优化:#haproxy+keepalived做前端,基本是tcp相关的内核优化,如下优化是一朋友线上环境用的,同样也适合lvsnet.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.ip_local_port_range = 4000 65000net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.route.gc_timeout = 100net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_synack_retries = 1net.ipv4.ip_conntrack_max = 25000000net.ipv4.netfilter.ip_conntrack_max=25000000net.ipv4.netfilter.ip_conntrack_max=6553600
#####【另】1、在此HAProxy+Keepalivp负载均衡高可用架构中,我们是如何解决session的问题呢?我们这里采用的是它自身的balance source机制,它跟Nginx的ip_hash机制原理类似,是让客户机访问时始终访问后端的某一台真实的web服务器,这样让session就固定下来了;
2、option httpchk HEAD /index.jsp HTTP/1.0 是网页监控,如果HAProxy检测不到Web的根目录下没有index.jsp,就会产生503报错。
3、有网友配置HAProxy时喜欢用listen IP:80这样的格式,这样其实不好,做负载均衡高可用时由于从机分配不到VIP地址,会导致从机启动不了,我建议用bind *:80的方式代替。
佛曰,色即是空,空即是色!今晚,偶想空一下 是你教会我如何去爱你,可你就要离开了 我不是真的喜欢回忆过去,而是现在的时光没有当初值得怀念 \ ◇◆ 丶你说你爱我ゝ那你用什么来证明呢?
页:
[1]