haproxy負載均衡和配合keepalived的快速部署
摘要 簡單易配 haproxy作為代理轉發服務器 keepalived作為高可用
四臺服務器
-
server1.example.com //realserver 本次實驗作為httpd服務器
-
server2.example.com //realserver 本次實驗作為httpd服務器
-
server3.example.com //安裝haproxy 和 keepalived
-
server4.example.com //安裝haproxy 和 keepalived
server1.example.com server2.example.com 上只需要安裝httpd服務即可,因為haproxy僅是個代理而已。
server3(4).example.com 上的操作 ps:本人采用rhel6.5系統
yum源配置高可用 ,安裝keepalived // 具體過程在我的博客《keepalived+lvs快速部署》里有
yum install haproxy -y
vim /etc/haproxy/haproxy.cfg 添加如下
stats uri /status //打開自帶的監控功能,清楚的查看狀態
listen www.example.com *:80
balance roundrobin //輪詢
listen stats_auth 172.25.254.3:80
stats enable
stats uri /status #監控頁面地址
stats auth vision:leaf #管理帳號和密碼
stats refresh 5s #刷新頻率
server web1 172.25.254.1:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 172.25.254.2:80 cookie app1inst2 check inter 2000 rise 2 fall 5 //要監控的兩臺web服務器
keepalived配置如下
! Configuration File for keepalived global_defs { notification_email { root@localhost.com } notification_email_from root@localhost.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER //另一臺BACKUP interface eth0 virtual_router_id 51 priority 150 //另一個備機找個比這小的數字就好了 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.254.100 //作為高可用的虛擬IP } track_script { check_haproxy
opt下編輯這個腳本
vim /opt/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/keepalived stop &> /dev/null fi ~ //腳本是對haproxy作健康檢查,配合keepalived實現服務的VIP轉移后的正常訪問
完成配置后就可以實驗了,
兩臺啟動服務
/etc/init.d/haproxy start
/etc/init.d/keepalived start
web 訪問172.25.254.3 因為3是master所以可以訪問并且轉發輪詢到server1. server2
關閉server3的haproxy ,然后訪問172.25.254.3訪問不到了這時候應該去訪問server4 ,作為備機ip是172.25.254.4
可以看到轉發輪詢到server1. server2
ok
完結。