一、keepalived和haproxy
1、keepalived
Keepalived的作用是检测服务器的健康状态,在所有可能出现单点故障的地方为其提供高可用。如果有一台服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
keepalived的核心是vrrp,它是通过脚本来调用服务的,所以在keepalived的使用中,仅需关心两点:配置文件(/etc/keepalived/keepalived.conf)和服务脚本(/etc/rc.d/init.d/keepalived)
2、haproxy
haproxy是一个七层的负载均衡高度器,和nginx是属于一个层次上的,而lvs是一个四层的负载均衡高度器,它最多只能工作在TCP\IP协议栈上,所以对于代理转发,haproxy做的可以比lvs更细腻
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
二、拓扑图
三、前端配置
1、ha1配置(172.16.7.10)
(1)配置keepalived
[root@ha1 ~]# yum -y install keepalived #安装keepalived [root@ha1 ~]# vim /etc/keepalived/keepalived.conf #修改配置文件 ! Configuration File for keepalived global_defs { notification_email { #通知邮件地址 root@localhost shuishui@localhost } notification_email_from warning@localhost smtp_server 127.0.0.1 #邮件服务器地址 smtp_connect_timeout 30 router_id LVS_DEVEL_shuishui } # vrrp_script chk_haproxy { script "killall -0 haproxy" #服务探测,返回0说明服务是正常的 interval 1 #每隔1秒探测一次 weight 2 #haproxy上线,权重加2;下线,权重减2 } # vrrp_instance VI_1 { #双主实例1 state MASTER #ha1(172.16.7.10)为主,ha2(172.16.7.100)为备 interface eth0 virtual_router_id 88 #实例1的VRID为88 garp_master_delay 1 priority 100 #主(172.16.7.10)的优先级为100,从的(172.16.7.100)优先级为99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.88/16 dev eth0 #实例1的VIP } track_interface { eth0 } # track_script { #脚本追踪 chk_haproxy } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } vrrp_instance VI_2 { state BACKUP #实例2在ha1(172.16.7.10)上是备,在ha2(172.16.7.100)上是主 interface eth0 virtual_router_id 188 #实例2的VRID是188 garp_master_delay 1 priority 200 #实例2在ha1上的优先级是200,在ha2上的优先级是201 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.188/16 dev eth0 #实例2的VIP } track_interface { eth0 } # track_script { #脚本追踪 chk_haproxy } } |
为ha1的keepalived提供脚本文件:
[root@ha1 keepalived]# pwd /etc/keepalived [root@ha1 keepalived]# vim notify.sh #!/bin/bash # Author: MageEdu |
(2)配置haproxy
[root@ha1 ~]# yum -y install haproxy #安装haproxy [root@ha1 ~]# vim /etc/haproxy/haproxy.cfg #修改配置文件 global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http #指定haproxy的工作模式为http log global option httplog option dontlognull option http-server-close #当客户端超时时,允许服务器关闭连接 option forwardfor except 127.0.0.0/8 #在响应头部加入forwardfor option redispatch #在使用了基于cookie的会话保持的时候,通常需要 #加这么一项,一旦后端某一server宕机时,能够将 #其会话重新派发到其它的upstream servers retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 10000 #最大并发连接数 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend proxy *:80 #前端代理 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets acl url_static path_end -i .html .jpg .gif .png .css .js acl dynamic_content path_end -i .php use_backend static if url_static default_backend dynamic #--------------------------------------------------------------------- # static backend for serving up p_w_picpaths, stylesheets and such #--------------------------------------------------------------------- backend static #后端静态服务器 balance roundrobin server web1 172.16.7.201:80 inter 3000 rise 2 fall 3 check maxconn 5000 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend dynamic #后端动态服务器 balance roundrobin server web2 172.16.7.202:80 inter 3000 rise 2 fall 3 check maxconn 5000 server web3 172.16.7.200:80 inter 3000 rise 2 fall 3 check maxconn 5000 listen statistics mode http bind *:8080 #把stats页面绑定到8080端口 stats enable #开启stats功能 stats auth admin:admin #认证的用户名和密码 stats uri /admin?stats #指定uri访问路径 stats hide-version #为了安全(版本bug),隐藏版本信息 stats admin if TRUE #如果认证通过了就允许管理 stats refresh 5s #页面5秒刷新一次 acl allow src 172.16.0.0/16 #定义访问控制列表 tcp-request content accept if allow tcp-request content reject |
2、ha2配置(172.16.7.100)
(1)配置keepalived
[root@ha2 ~]# yum -y install keepalived #安装keepalived [root@ha2 ~]# vim /etc/keepalived/keepalived.conf #修改配置文件 ! Configuration File for keepalived global_defs { notification_email { root@localhost shuishui@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL_shuishui } # vrrp_script chk_haproxy { script "killall -0 haproxy" interval 1 weight 2 } # vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 88 garp_master_delay 1 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.88/16 dev eth0 } track_interface { eth0 } # track_script { chk_haproxy } } # vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 188 garp_master_delay 1 priority 201 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.188/16 dev eth0 } track_interface { eth0 } # track_script { chk_haproxy } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } # # #脚本同ha1的(脚本里的vip改为172.16.7.188),最后给执行权限 |
(2)配置haproxy
因为ha1的haproxy与ha2的haproxy是相同的,都是将服务代理至后端服务器,所以直接scp就可以
[root@ha1 ~]# scp /etc/haproxy/haproxy.cfg [email protected]:/etc/haproxy/ |
3、启动keepalived并测试
(1)ha1
(2)ha2
(3)关闭ha1的haproxy服务测试VIP飘移
(4)查看ha2,是否接收到了ha1飘过来的VIP
四、后端配置
1、配置web1(172.16.7.201),静态的
[root@web1 ~]# yum -y install httpd [root@web1 ~]# cd /var/www/html/ [root@web1 html]# vim index.html |
2、配置web2(172.16.7.202),动态的
[root@web2 ~]# yum -y install httpd php [root@web2 ~]# cd /var/www/html/ [root@web2 html]# vim index.php |
3、配置web3(172.16.7.200),动态的
[root@web3 ~]# yum -y install httpd php [root@web3 ~]# cd /var/www/html/ [root@web3 html]# vim index.php |
五、测试
1、keepalived的高可用
上面测试过了,当haproxy服务挂掉的时候,VIP可以飘走;当keepalived服务挂掉的时候,VIP也可以飘走,高可用功能实现
2、haproxy动静分离机制
(1)请求静态内容
首先在web1(172.16.7.201)的网页目录下放入1.jpg
(2)请求动态内容
(3)haproxy统计页面的输出
①、URI及安全验证
②、haproxy统计页面