Centos6下haproxy+keepalived构建高可用web集群

前端之家收集整理的这篇文章主要介绍了Centos6下haproxy+keepalived构建高可用web集群前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1)实验拓扑信息

wKioL1gYoFjzQr9jAAEd09D24eo089.png

说明:

a、在客户端使用www.wanwan.com,将由负载均衡器提交给vip1所对应的集群进行处理

b、在客户端使用img.wanwan.com,将由负载均衡器提交给vip1所对应的集群进行处理

c、10.10.10.129和10.10.10.130两台负载均衡器,其中某一台出现故障均不影响整个系统运行


2)haproxy的安装与启动脚本配置

参考我的上一篇博客http://www.jb51.cc/article/p-sytmwgiq-xe.html


3)haproxy的配置(两台负载均衡器10.10.10.129/10.10.10.130均需配置)

  1. [root@MysqL-master~]#adduserhaproxy-s/sbin/nologin-M
  2. [root@MysqL-master~]#cd/usr/local/haproxy/conf/
  3. [root@MysqL-masterconf]#cathaproxy.cfg
  4. global
  5. log127.0.0.1local0info
  6. maxconn4096
  7. userhaproxy
  8. grouphaproxy
  9. daemon
  10. nbproc1
  11. pidfile/usr/local/haproxy/logs/haproxy.pid
  12. defaults
  13. modehttp
  14. retries3
  15. timeoutconnect10s
  16. timeoutclient20s
  17. timeoutserver30s
  18. timeoutcheck5s
  19. frontendwww
  20. bind*:80
  21. modehttp
  22. optionhttplog
  23. optionforwardfor
  24. optionhttpclose
  25. logglobal
  26. #来自www.wanwan.com的请求,均交给htmpool进行处理,来自img.wanwan.com的请求,则提交给imgpool进行处理。默认不指定的话,交给htmpool进行处理
  27. aclhost_wwwhdr_dom(host)-iwww.wanwan.com
  28. aclhost_imghdr_dom(host)-iimg.wanwan.com
  29. use_backendhtmpoolifhost_www
  30. use_backendimgpoolifhost_img
  31. default_backendhtmpool
  32. backendhtmpool
  33. modehttp
  34. optionredispatch
  35. optionabortonclose
  36. balancestatic-rr
  37. cookieSERVERID
  38. optionhttpchkGET/index.html
  39. serverweb0110.10.10.128:80cookieserver1weight6checkinter2000rise2fall3
  40. serverweb0210.10.10.132:80cookieserver2weight6checkinter2000rise2fall3
  41. backendimgpool
  42. modehttp
  43. optionredispatch
  44. optionabortonclose
  45. balancestatic-rr
  46. cookieSERVERID
  47. optionhttpchkGET/index.html
  48. serverimg110.10.10.131:80cookieserver1weight6checkinter2000rise2fall3
  49. serverimg210.10.10.133:80cookieserver2weight6checkinter2000rise2fall3
  50. #配置haproxy的web监控界面
  51. listenadmin_stats
  52. bind0.0.0.0:9188
  53. modehttp
  54. log127.0.0.1local0err
  55. statsrefresh30s
  56. statsuri/haproxy-status
  57. statsrealmwelcomelogin\Haproxy
  58. statsauthadmin:admin~!@
  59. statshide-version
  60. statsadminifTRUE

4)keepalived的配置

负载均衡器配置两个vip:10.10.10.188以及10.10.10.189(在两台负载均衡器上互为主备)

  1. [root@MysqL-masterconf]#cat/etc/keepalived/keepalived.conf
  2. !ConfigurationFileforkeepalived
  3. global_defs{
  4. notification_email{
  5. 314324506@qq.com
  6. }
  7. notification_email_fromAlexandre.Cassen@firewall.loc
  8. smtp_serversmtp.qq.com
  9. smtp_connect_timeout30
  10. router_idLVS_7
  11. }
  12. #配置这个脚本的作用是为了避免haproxy服务停止后,keepalived不释放vip
  13. vrrp_scriptchk_http_port{
  14. script"/opt/check_haproxy.sh"
  15. interval2
  16. weight2
  17. }
  18. vrrp_instanceVI_188{
  19. stateMASTER#主服务器
  20. interfaceeth0
  21. virtual_router_id188
  22. priority150#slave上的数值更小,数值越大,代表优先级越高
  23. advert_int1
  24. authentication{
  25. auth_typePASS
  26. auth_pass1111
  27. }
  28. virtual_ipaddress{
  29. 10.10.10.188/24#vip地址,在系统里面通过ipaddlist可以查看
  30. }
  31. }
  32. vrrp_instanceVI_189{
  33. stateBACKUP#从服务器
  34. interfaceeth0
  35. virtual_router_id189
  36. priority100#master上的数值更大,数值越大,代表优先级越高
  37. advert_int1
  38. authentication{
  39. auth_typePASS
  40. auth_pass1111
  41. }
  42. virtual_ipaddress{
  43. 10.10.10.189/24#vip地址,在系统里面通过ipaddlist可以查看
  44. }
  45. }

5)keepalived的启动脚本

  1. #!/bin/sh
  2. #
  3. #StartupscriptfortheKeepaliveddaemon
  4. #
  5. #processname:keepalived
  6. #pidfile:/var/run/keepalived.pid
  7. #config:/etc/keepalived/keepalived.conf
  8. #chkconfig:-2179
  9. #description:StartandstopKeepalived
  10. #Sourcefunctionlibrary
  11. ./etc/rc.d/init.d/functions
  12. #Sourceconfigurationfile(wesetKEEPALIVED_OPTIONSthere)
  13. ./etc/sysconfig/keepalived
  14. RETVAL=0
  15. prog="keepalived"
  16. start(){
  17. echo-n$"Starting$prog:"
  18. daemonkeepalived${KEEPALIVED_OPTIONS}
  19. RETVAL=$?
  20. echo
  21. [$RETVAL-eq0]&&touch/var/lock/subsys/$prog
  22. }
  23. stop(){
  24. echo-n$"Stopping$prog:"
  25. killprockeepalived
  26. RETVAL=$?
  27. echo
  28. [$RETVAL-eq0]&&rm-f/var/lock/subsys/$prog
  29. }
  30. reload(){
  31. echo-n$"Reloading$prog:"
  32. killprockeepalived-1
  33. RETVAL=$?
  34. echo
  35. }
  36. #Seehowwewerecalled.
  37. case"$1"in
  38. start)
  39. start
  40. ;;
  41. stop)
  42. stop
  43. ;;
  44. reload)
  45. reload
  46. ;;
  47. restart)
  48. stop
  49. start
  50. ;;
  51. condrestart)
  52. if[-f/var/lock/subsys/$prog];then
  53. stop
  54. start
  55. fi
  56. ;;
  57. status)
  58. statuskeepalived
  59. ;;
  60. *)
  61. echo"Usage:$0{start|stop|reload|restart|condrestart|status}"
  62. exit1
  63. esac
  64. exit$RETVAL

6)效果测试

a、在客户端测试www.wanwan.com

wKiom1gZJxWwuWznAAA8Ah9SF5I775.png-wh_50

wKioL1gZJxWB5XzGAABBmmVmU_o912.png-wh_50

b、测试img.wanwan.com

wKiom1gZJxaS9BRsAAA-Tvd59Gw368.png-wh_50

wKioL1gZJxbRSm5vAAA534ZI-Jg155.png-wh_50

如上:负载均衡调度的作用已经实现,那么我们在测试下keepalived的相关功能

c、测试keepalived的相关功能

  1. [root@MysqL-master~]#ipaddlist
  2. 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN
  3. link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
  4. inet127.0.0.1/8scopehostlo
  5. inet6::1/128scopehost
  6. valid_lftforeverpreferred_lftforever
  7. 2:eth0:<BROADCAST,MULTICAST,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
  8. link/ether00:0c:29:67:b3:45brdff:ff:ff:ff:ff:ff
  9. inet10.10.10.129/24brd10.10.10.255scopeglobaleth0
  10. inet10.10.10.188/24scopeglobalsecondaryeth0
  11. inet6fe80::20c:29ff:fe67:b345/64scopelink
  12. valid_lftforeverpreferred_lftforever
  13. [root@MysqL-slave~]#ipaddlist
  14. 1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
  15. link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
  16. inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
  17. inet10.10.10.189/24scopeglobalsecondaryeth0
  18. inet6fe80::20c:29ff:fe53:cf52/64scopelink
  19. valid_lftforeverpreferred_lftforever

我们可以看到,两个vip地址,10.10.10.188以及10.10.10.189分别在两台负载均衡器上(仔细看下keepalived的配置可以发现,我们配置两台负载均衡互为主从)

模拟10.10.10.129-主负载均衡器宕机,然后观察ip地址切换以及负载均衡是否正常

  1. [root@MysqL-slave~]#ipaddlist
  2. 1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
  3. link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
  4. inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
  5. inet10.10.10.189/24scopeglobalsecondaryeth0
  6. inet10.10.10.188/24scopeglobalsecondaryeth0
  7. inet6fe80::20c:29ff:fe53:cf52/64scopelink
  8. valid_lftforeverpreferred_lftforever
  9. 如上,我们可以观察到vip已经切换到另外一台负载均衡上了,然后我们观察负载均衡器的使用情况

wKiom1gZLVejz0BsAAA8v6e5pao217.png-wh_50

wKioL1gZLVeBRc95AAA-x-HVzFI895.png-wh_50

  1. 负载正常,keepalived功能也实现了,我们重新开启主负载均衡器
  2. [root@MysqL-masterkeepalived]#ipaddlist
  3. 1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
  4. link/ether00:0c:29:67:b3:45brdff:ff:ff:ff:ff:ff
  5. inet10.10.10.129/24brd10.10.10.255scopeglobaleth0
  6. inet10.10.10.188/24scopeglobalsecondaryeth0
  7. inet6fe80::20c:29ff:fe67:b345/64scopelink
  8. valid_lftforeverpreferred_lftforever
  9. vip地址10.10.10.188已经切换回来了
  10. [root@MysqL-slave~]#ipaddlist
  11. 1:lo:<LOOPBACK,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000
  12. link/ether00:0c:29:53:cf:52brdff:ff:ff:ff:ff:ff
  13. inet10.10.10.130/24brd10.10.10.255scopeglobaleth0
  14. inet10.10.10.189/24scopeglobalsecondaryeth0
  15. inet6fe80::20c:29ff:fe53:cf52/64scopelink
  16. valid_lftforeverpreferred_lftforever

到此,keepalived+haproxy的基本功能已经实现了,由于haproxy配置比较多,这里我就不细讲了,后面会整理一篇关于haproxy常用的参数配置。

猜你在找的CentOS相关文章