1. 程式人生 > >Haproxy+keepalived實現負載均衡

Haproxy+keepalived實現負載均衡

keepalived haproxy

Haproxy簡介:

Haproxy是一個開源的高性能的反向代理或者說是負載均衡服務軟件之一,它支持雙機熱備、虛擬主機、基於TCP和HTTP應用代理等功能。其配置簡單,而且擁有很好的對服務器節點的健康檢查功能(相當於keepalived健康檢查)。

Haproxy實現了一種事件驅動, 單一進程模型,此模型支持非常大的並發連接數,HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬件上,完全可以支持數以萬計的並發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web服務器不被暴露到網絡上。


Keepalived簡介:

Keepalived的作用是檢測服務器的狀態,如果有一臺web服務器宕機,或工作出現故障,Keepalived將檢測到,並將有故障的服務器從系統中剔除,同時使用其他服務器代替該服務器的工作,當服務器工作正常後Keepalived自動將服務器加入到服務器群中,這些工作全部自動完成,不需要人工幹涉,需要人工做的只是修復故障的服務器。



一、實驗環境

(Centos7)

Haporxy1 + keepalived : 10.0.0.128

Haproxy2 + keepalived : 10.0.0.129

VIP1 : 10.0.0.250

Web1 : 10.0.0.130:80

Web2 : 10.0.0.131:80


拓撲圖:

技術分享


二、方案實施


安裝及配置web服務(Web1、Web2).

1.安裝httpd服務

[root@Web1 ~]# yum install -y httpd
[root@Web2 ~]# yum install -y httpd


2.修改配置文件中的ServerName,Web1為img.zxb.com

,Web2為test.zxb.com

Web1: ServerName img.zxb.com:80

Web2: ServerName test.zxb.com:80


3.編輯網頁內容

[root@Web1 ~]# echo "web1:10.0.0.130" >/var/www/html/index.html
[root@Web2 ~]# echo "web2:10.0.0.131" >/var/www/html/index.html

4.啟動web服務

[root@Web1 ~]# systemctl start httpd
[root@Web2 ~]# systemctl start httpd

5.編輯自身hosts文件

[root@Web1 ~]# cat /etc/hosts

10.0.0.130 img.zxb.com

[root@Web2 ~]# cat /etc/hosts

10.0.0.131 test.zxb.com


配置Haproxy及keepalived相關配置

1.安裝Haproxy及keepalived

[root@Haproxy1 ~]# yum install -y keepalived haproxy
[root@Haproxy2 ~]# yum install -y keepalived haproxy


2.配置haproxy(兩臺配置一樣)

[root@Haproxy1 ~]#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
    stats socket /var/lib/haproxy/stats
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    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                 3000
添加
frontend  main *:80 
option forwardfor  except 127.0.0.0/8
rspadd X-via:\ haproxy/www.zxb.com
rspidel Server.* 
    stats enable                    #在frontend添加監控頁面
    stats uri /zxb?stats            #自定義監控頁面的url
    stats realm Stats\ Page\ Area   #去掉空格轉譯
    stats auth zxb:zxb             #配置帳號密碼
    stats refresh 5s                #每5s刷新一次監控頁面
    stats hide-version              #隱藏版本信息
    stats admin if TRUE
    maxconn 10000                    #定義最大並發為10000
acl url_img hdr(host) -i img.zxb.com
acl url_test hdr(host) -i test.zxb.com
use_backend img_web if url_img
use_backend test_web if url_test
backend img_web
balance roundrobin
server web1 10.0.0.130:80 check
backend test_web
balance roundrobin
server web2 10.0.0.131:80 check



4.啟動haproxy

[root@Haproxy1 ~]# systemctl start haproxy
[root@Haproxy2 ~]# systemctl start haproxy


3.配置keepalived

Master:

[root@Haproxy1 ~]#

! Configuration File for keepalived  
 vrrp_script check_haproxy {  
       script "/etc/keepalived/check_haproxy.sh"
             #檢測腳本,該路徑腳本存放的位置            
   interval 2
        #每隔2秒鐘檢測一次
  # weight 2     #優先級減2
       
} 
global_defs {  
 #全局配置
   notification_email {  
#發生事件切換的時候,發送的郵箱
     [email protected]   
      
 }   
   router_id LVS_DEVEL 
   }  
    vrrp_instance VI_2 {   
       state MASTER
             #兩種角色,MASTER or BACKUP
    interface ens33
                #實例綁定的網卡
    virtual_router_id 50    #VRID 標記   
                
      priority 100        #優先級
                        
    advert_int 1     
    #MASTER 與BACKUP 之間同步檢查的時間間隔,單位為秒  
    authentication {  
     auth_type PASS
                    #PASS#認證的方式,支持 PASS
       auth_pass 1111  
                    #認證的密碼
    } 
   virtual_ipaddress { 
          10.0.0.250
                    #指定漂移地址(VIP)
 }   
  track_script { 
         check_haproxy 
         }
 }



Backup:

[root@Haproxy2 ~]#

! Configuration File for keepalived  
 
vrrp_script check_haproxy {
        script "
/etc/keepalived/check_haproxy.sh
"
        interval 2
        weight 2
} 
global_defs {  
   notification_email {  
     [email protected]  
     
   }  
   router_id LVS_DEVEL  
}  
vrrp_instance VI_2 {  
    state 
BACKUP
    interface ens33
    virtual_router_id 50
    
priority 80
    advert_int 1  
    authentication {  
        auth_type PASS
        auth_pass 1111  
    }
    virtual_ipaddress {
        
10.0.0.250
    }
    track_script {
        check_haproxy
   }
}


4.編輯haproxy檢測腳本(兩臺配置一樣)

[root@Haproxy1 ~]#cat /etc/keepalived/check_haproxy.sh
#!/bin/bash
haproxy=`ps -C haproxy --no-heading| wc -l`
if [ $haproxy -eq 0 ];then
systemctl stop keepalived
fi


三.測試


1.訪問 haproxy 自帶的監控頁面,url為haproxy服務器的IP地址
url用戶名密碼為http://10.0.0.128/zxb?stats

技術分享



2.編輯客戶端10.0.0.150:hosts文件

[root@Client ~]# cat /etc/hosts

10.0.0.100 img.zxb.com test.zxb.com


3.啟動Haproxy1和Haproxy2的keepalived,由於Haproxy1的priority較高,所以VIP首先在Haproxy1
[root@Haproxy1 ~]# systemctl start keepalived
[root@Haproxy2 ~]# systemctl start keepalived

[root@Haproxy2 ~]# systemctl status keepalived

● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2017-10-22 23:34:43 CST; 30min ago
  Process: 17871 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 17872 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─17872 /usr/sbin/keepalived -D
           ├─17873 /usr/sbin/keepalived -D
           └─17874 /usr/sbin/keepalived -D
10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: VRRP_Instance(VI_2) 
Sending/queueing gratuitous ARPs on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250



4.訪問

[root@Client ~]# curl img.zxb.com

web1:10.0.0.130

[root@Client ~]# curl test.zxb.com

web2:10.0.0.131


5.當停止master上的haproxy服務時,keepalived配置文件檢測到haproxy腳本後,停止keepalived服務,VIP從Master飄到Backup

[root@Haproxy1 ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:64:e3:62 brd ff:ff:ff:ff:ff:ff
 
   inet 10.0.0.128/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::8a1e:39ba:9beb:4aa9/64 scope link 
       valid_lft forever preferred_lft forever


[root@Haproxy2 ~]# systemctl status keepalived

● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2017-10-23 17:25:23 CST; 16min ago
  Process: 3915 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3916 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─3916 /usr/sbin/keepalived -D
           ├─3917 /usr/sbin/keepalived -D
           └─3920 /usr/sbin/keepalived -D
10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: VRRP_Instance(VI_2) 
Sending/queueing gratuitous ARPs on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250

[root@zxb2 ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:23:e3:43 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.129/24 brd 10.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    
inet 10.0.0.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::bcdf:e60f:7858:6933/64 scope link 
       valid_lft forever preferred_lft forever


6.再訪問

[root@Client ~]# curl img.zxb.com

web1:10.0.0.130

[root@Client ~]# curl test.zxb.com

web2:10.0.0.131


end
!!!!


本文出自 “XiaoBingZ” 博客,請務必保留此出處http://1767340368.blog.51cto.com/13407496/1975334

Haproxy+keepalived實現負載均衡