1. 程式人生 > 實用技巧 >HAProxy+Keepalived實現雙主高可用負載均衡

HAProxy+Keepalived實現雙主高可用負載均衡

一、規劃

1234567OS:CentOS764bitHAProxy+Keepalived-1:192.168.10.128HAProxy+Keepalived-2:192.168.10.129VIP1:192.168.10.111VIP2:192.168.10.222WEB1:192.168.10.130:80WEB2:192.168.10.131:80


二、配置Keepalived(注:安裝及配置詳見http://wangzhijian.blog.51cto.com/6427016/1701577)


①配置keepalived主配置檔案(配置檔案大部相同)

配置192.168.10.128做192.168.10.111的MASTER,192.168.10.222的BACKUP;

配置192.168.10.128做192.168.10.111的BACKUP,192.168.10.222的MASTER.


a.192.168.10.128配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#vi/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{[email protected][email protected]
##設定郵件報警地址}notification_email_from[email protected]smtp_server127.0.0.1smtp_connect_timeout3router_idLVS_DEVEL}vrrp_scriptcheck_haproxy{##新增haproxy服務監控指令碼script"/etc/keepalived/check_haproxy.sh"interval2weight2}vrrp_instanceVI_1{##配置vrrp例項1stateMASTERinterfaceeno16777736virtual_router_id51##例項1虛擬路由標識priority101advert_int1authentication{auth_typePASSauth_pass1111}track_script{##檢測執行指令碼check_haproxy}virtual_ipaddress{192.168.10.111}}vrrp_instanceVI_2{##配置vrrp例項2stateBACKUPinterfaceeno16777736virtual_router_id52##例項2虛擬路由標識priority100advert_int1garp_master_delay5authentication{auth_typePASSauth_pass1111}virtual_ipaddress{192.168.10.222}


b.192.168.10.129配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#vi/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{[email protected][email protected]##設定郵件報警地址}notification_email_from[email protected]smtp_server127.0.0.1smtp_connect_timeout3router_idLVS_DEVEL}vrrp_scriptcheck_haproxy{##新增haproxy服務監控指令碼script"/etc/keepalived/check_haproxy.sh"interval2weight2}vrrp_instanceVI_1{##配置vrrp例項1stateBACKUPinterfaceeno16777736virtual_router_id51##例項1虛擬路由標識priority100advert_int1garp_master_delay5authentication{auth_typePASSauth_pass1111}track_script{##檢測執行指令碼check_haproxy}virtual_ipaddress{192.168.10.111}}vrrp_instanceVI_2{##配置vrrp例項2stateMASTERinterfaceeno16777736virtual_router_id52##例項2虛擬路由標識priority101advert_int1garp_master_delay5authentication{auth_typePASSauth_pass1111}virtual_ipaddress{192.168.10.222}


②配置HAProxy檢測指令碼

123456789101112#vi/etc/keepalived/check_haproxy.sh#!/bin/bashif[$(ps-Chaproxy--no-header|wc-l)-eq0]thensystemctlstarthaproxyfisleep2if[$(ps-Chaproxy--no-header|wc-l)-eq0]thensystemctlstopkeepalivedfi#chmodu+x/etc/keepalived/check_haproxy.sh


③開啟keepalived並檢視

1#systemctlstartkeepalived


192.168.10.128:

wKiom1YgokTSS74LAAIZJbXVKr4000.jpg

192.168.10.129:

wKiom1YgouqQglk0AAI3K-xTCuQ067.jpg



三、配置HAProxy(注:安裝及配置詳見http://wangzhijian.blog.51cto.com/6427016/1702795)

①配置HAProxy主配置檔案(配置檔案相同)

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#vi/etc/haproxy/haproxy.cfg#thisconfigneedshaproxy-1.1.28orhaproxy-1.2.1global##全域性配置log127.0.0.1local0log127.0.0.1local1notice#logloghostlocal0infomaxconn4096chroot/usr/share/haproxyuid99gid99daemon#debug#quietdefaults##預設配置logglobalmodehttpoptionhttplogoptiondontlognullretries3optionredispatchmaxconn4000timeoutconnect5000timeoutclient50000timeoutserver50000frontendweb##前端代理bind0.0.0.0:80aclwww.zhi.comhdr(host)-iuse_backendwww.zhi.comifwww.zhi.comaclwww.fly.comhdr(host)-iwww.fly.comuse_backendwww.fly.comifbackend##後端www.zhi.com作用域modehttpbalancesource##儲存session值optionhttpchk/index.htmlserver192.168.10.130192.168.10.130:80checkinter2000rise2fall1backend##後端www.fly.com作用域modehttpbalancesource##儲存session值optionhttpchk/index.htmlserver192.168.10.131192.168.10.131:80checkinter2000rise2fall1listenstatus##監控頁面設定modehttpbind0.0.0.0:80statsenablestatshide-versionstatsuri/haproxyadminstatsauthadmin:adminstatsadminifTRUE


②開啟HAProxy並檢視

1#systemctlstarthaproxy

①瀏覽器輸入192.168.10.128/haproxyadmin,輸入賬號密碼檢視

wKiom1Ygqi6S_SGMAAjXpJTlBiE807.jpg

②瀏覽器輸入192.168.10.129/haproxyadmin,輸入賬號密碼檢視

wKioL1YgqqOBI8OeAAjk9oiqM7s591.jpg

③瀏覽器輸入192.168.10.111/haproxyadmin,輸入賬號密碼檢視

wKiom1Ygqqvg3bz1AAjWQhY6Ifg454.jpg

④瀏覽器輸入192.168.10.222/haproxyadmin,輸入賬號密碼檢視

wKioL1YgqveS0PtwAAi5-NsL-H8126.jpg



四、測試


①更改Windows的hosts檔案

12345C:\Windows\System32\drivers\etc\hosts192.168.10.111www.zhi.com192.168.10.111www.zhi.com192.168.10.222www.fly.com192.168.10.222www.fly.com


②測試1

ping www.zhi.com

wKiom1YgrlXQ5s-aAAFgNXgV8o4664.jpg

pingwww.fly.com

wKiom1YgrpSyKL1uAAFKlPgHUe8545.jpg


③測試2

192.168.10.128:

12#systemctlstopkeepalived#ipaddr

wKioL1Ygr6DAdCLHAAH8O4WN8Zg328.jpg

192.168.10.129:

# ip addr

wKiom1Ygr-WQNlldAAJmQZIKcRM741.jpg

虛擬IP192.168.10.111已漂移至192.168.10.129,繼續ping,依然通

wKioL1YgsHXSUaLKAAHBrRleq84587.jpg

OK了

本文出自 “記事本” 部落格,請務必保留此出處http://wangzhijian.blog.51cto.com/6427016/1703552


轉載於:https://blog.51cto.com/lucifer119/1732354