天天看點

haproxy 實作負載均衡

haproxy 簡介

haproxy提供高可用性、負載均衡以及基于TCP和HTTP應用的代理,支援虛拟主機,它是免費、快速并且可靠的一種解決方案。HAProxy特别适用于那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運作在目前的硬體上,完全可以支援數以萬計的并發連接配接。并且它的運作模式使得它可以很簡單安全的整合進您目前的架構中,同時可以保護你的web伺服器不被暴露到網絡上.
           

haproxy 的配置

haproxy 配置中分成五部分内容,分别如下:

1 global:參數是程序級的,通常是和作業系統相關。這些參數一般隻設定一次,如果配置無誤,就不需要再次進行修改

2 defaults:配置預設參數,這些參數可以被用到frontend,backend,Listen元件

3 frontend:接收請求的前端虛拟節點,frontend可以更加規則直接指定具體使用後端的backend

4 backend:後端服務叢集的配置,是真實伺服器,一個backend對應一個或者多個實體伺服器

5 Listen Fronted和backend的組合體

####################全局配置資訊######################## 
       #######參數是程序級的,通常和作業系統(OS)相關######### 
global 
       maxconn                    #預設最大連接配接數 
       log  local3            #[err warning info debug] 
       chroot /var/haproxy             #chroot運作的路徑 
       uid                           #所屬運作的使用者uid 
       gid                           #所屬運作的使用者組 
       daemon                          #以背景形式運作haproxy 
       nbproc                         #程序數量(可以設定多個程序提高性能) 
       pidfile /var/run/haproxy.pid    #haproxy的pid存放路徑,啟動程序的使用者必須有權限通路此檔案 
       ulimit-n                   #ulimit的數量限制 

       #####################預設的全局設定###################### 
       ##這些參數可以被利用配置到frontend,backend,listen元件## 
defaults 
       log global 
       mode http                       #所處理的類别 (#7層 http;4層tcp  ) 
       maxconn                    #最大連接配接數 
       option httplog                  #日志類别http日志格式 
       option httpclose                #每次請求完畢後主動關閉http通道 
       option dontlognull              #不記錄健康檢查的日志資訊 
       option forwardfor               #如果後端伺服器需要獲得用戶端真實ip需要配置的參數,可以從Http Header中獲得用戶端ip  
       option redispatch               #serverId對應的伺服器挂掉後,強制定向到其他健康的伺服器  
       option abortonclose             #當伺服器負載很高的時候,自動結束掉目前隊列處理比較久的連接配接 
       stats refresh                 #統計頁面重新整理間隔 
       retries                        #3次連接配接失敗就認為服務不可用,也可以通過後面設定 
       balance roundrobin              #預設的負載均衡的方式,輪詢方式 
      #balance source                  #預設的負載均衡的方式,類似nginx的ip_hash 
      #balance leastconn               #預設的負載均衡的方式,最小連接配接 
       contimeout                  #連接配接逾時 
       clitimeout                 #用戶端逾時 
       srvtimeout                 #伺服器逾時 
       timeout check               #心跳檢測逾時 

       ####################監控頁面的設定####################### 
listen admin_status                    #Frontend和Backend的組合體,監控組的名稱,按需自定義名稱 
        bind :             #監聽端口 
        mode http                      #http的7層模式 
        log  local3 err       #錯誤日志記錄 
        stats refresh s               #每隔5秒自動重新整理監控頁面 
        stats uri /admin?stats         #監控頁面的url 
        stats realm itnihao\ itnihao   #監控頁面的提示資訊 
        stats auth admin:admin         #監控頁面的使用者和密碼admin,可以設定多個使用者名 
        stats auth admin1:admin1       #監控頁面的使用者和密碼admin1 
        stats hide-version             #隐藏統計頁面上的HAproxy版本資訊  
        stats admin if TRUE            #手工啟用/禁用,後端伺服器(haproxy-1.4.9以後版本) 


       errorfile  /etc/haproxy/errorfiles/http 
       errorfile  /etc/haproxy/errorfiles/http 
       errorfile  /etc/haproxy/errorfiles/http 
       errorfile  /etc/haproxy/errorfiles/http 
       errorfile  /etc/haproxy/errorfiles/http 

       #################HAProxy的日志記錄内容設定################### 
       capture request  header Host           len 40 
       capture request  header Content-Length len 10 
       capture request  header Referer        len 200 
       capture response header Server         len 40 
       capture response header Content-Length len 10 
       capture response header Cache-Control  len 8 

       #######################網站監測listen配置##################### 
       ###########此用法主要是監控haproxy後端伺服器的監控狀态############ 
listen site_status 
       bind 0.0.0.0:1081                    #監聽端口 
       mode http                            #http的7層模式 
       log 127.0.0.1 local3 err             #[err warning info debug] 
       monitor-uri /site_status             #網站健康檢測URL,用來檢測HAProxy管理的網站是否可以用,正常傳回200,不正常傳回503 
       acl site_dead nbsrv(server_web) lt 2 #定義網站down時的政策當挂在負載均衡上的指定backend的中有效機器數小于1台時傳回true 
       acl site_dead nbsrv(server_blog) lt 2 
       acl site_dead nbsrv(server_bbs)  lt 2  
       monitor fail if site_dead            #當滿足政策的時候傳回503,網上文檔說的是500,實際測試為503 
       monitor-net 192.168.16.2/32          #來自192.168.16.2的日志資訊不會被記錄和轉發 
       monitor-net 192.168.16.3/32 

       ########frontend配置############ 
       #####注意,frontend配置裡面可以定義多個acl進行比對操作######## 
frontend http_80_in 
       bind :      #監聽端口,即haproxy提供web服務的端口,和lvs的vip端口類似 
       mode http            #http的7層模式 
       log global           #應用全局的日志配置 
       option httplog       #啟用http的log 
       option httpclose     #每次請求完畢後主動關閉http通道,HA-Proxy不支援keep-alive模式 
       option forwardfor    #如果後端伺服器需要獲得用戶端的真實IP需要配置次參數,将可以從Http Header中獲得用戶端IP 
       ########acl政策配置############# 
       acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$    
       #如果請求的域名滿足正規表達式中的2個域名傳回true -i是忽略大小寫 
       acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn 
       #如果請求的域名滿足www.itnihao.cn傳回true -i是忽略大小寫 
       #acl itnihao    hdr(host) -i itnihao.cn 
       #如果請求的域名滿足itnihao.cn傳回true -i是忽略大小寫 
       #acl file_req url_sub -i  killall= 
       #在請求url中包含killall=,則此控制政策傳回true,否則為false 
       #acl dir_req url_dir -i allow 
       #在請求url中存在allow作為部分位址路徑,則此控制政策傳回true,否則傳回false 
       #acl missing_cl hdr_cnt(Content-length) eq 0 
       #當請求的header中Content-length等于0時傳回true 

       ########acl政策比對相應############# 
       #block if missing_cl 
       #當請求中header中Content-length等于0阻止請求傳回403 
       #block if !file_req || dir_req 
       #block表示阻止請求,傳回403錯誤,目前表示如果不滿足政策file_req,或者滿足政策dir_req,則阻止請求 
       use_backend  server_web  if itnihao_web 
       #當滿足itnihao_web的政策時使用server_web的backend 
       use_backend  server_blog if itnihao_blog 
       #當滿足itnihao_blog的政策時使用server_blog的backend 
       #redirect prefix http://blog.itniaho.cn code 301 if itnihao 
       #當通路itnihao.cn的時候,用http的301挑轉到http://192.168.16.3 
       default_backend server_bbs 
       #以上都不滿足的時候使用預設server_bbs的backend 

       ##########backend的設定############## 
       #下面我将設定三組伺服器 server_web,server_blog,server_bbs
#########################backend server_web########################### 
backend server_web 
       mode http            #http的7層模式 
       balance roundrobin   #負載均衡的方式,roundrobin平均方式 
       cookie SERVERID      #允許插入serverid到cookie中,serverid後面可以定義 
       option httpchk GET /index.html #心跳檢測的檔案 
       server web1 : cookie web1 check inter  rise  fall  weight   
       #伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用, 
       #fall 3是3次失敗認為伺服器不可用,weight代表權重 
       server web2 : cookie web2 check inter  rise  fall  weight  
       #伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用, 
       #fall 3是3次失敗認為伺服器不可用,weight代表權重 

#############################backend server_blog################################# 
backend server_blog 
       mode http            #http的7層模式 
       balance roundrobin   #負載均衡的方式,roundrobin平均方式 
       cookie SERVERID      #允許插入serverid到cookie中,serverid後面可以定義 
       option httpchk GET /index.html #心跳檢測的檔案 
       server blog1 : cookie blog1 check inter  rise  fall  weight   
       #伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重 
       server blog2 : cookie blog2 check inter  rise  fall  weight  
        #伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重 

########################backend server_bbs############################

backend server_bbs 
       mode http            #http的7層模式 
       balance roundrobin   #負載均衡的方式,roundrobin平均方式 
       cookie SERVERID      #允許插入serverid到cookie中,serverid後面可以定義 
       option httpchk GET /index.html #心跳檢測的檔案 
       server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1  
       #伺服器定義,cookie 1表示serverid為web1,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重 
       server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2 
        #伺服器定義,cookie 1表示serverid為web2,check inter 1500是檢測心跳頻率rise 3是3次正确認為伺服器可用,fall 3是3次失敗認為伺服器不可用,weight代表權重 
           

配置haproxy負載均衡

安裝:

yum install haproxy -y

whereis haproxy #查找配置檔案

編輯配置檔案

vim /etc/haproxy/haproxy.cfg

global
       maxconn 
       log  local3
       chroot /var/lib/haproxy
       uid 
       gid 
       daemon
       nbproc 
       pidfile /var/run/haproxy.pid
       ulimit-n 
defaults
        log global
        mode http
        maxconn 
        option httplog
        option httpclose
        option dontlognull
        option forwardfor
        option redispatch
        option abortonclose
        stats refresh 
        retries 
        balance roundrobin
        contimeout 
        clitimeout 
        srvtimeout 
        timeout check 

listen  admin_status
        bind :
        mode http
        log  local3 err
        stats refresh s
        stats uri /admin?stats
        stats realm itnihao\ itnihao
        stats auth admin:admin
        stats auth admin1:admin1
        stats hide-version
        stats admin if TRUE
listen  site_status
        bind :
        mode http
        log  local3 err
        monitor-uri /site_status
        acl site_dead nbsrv(server_web) lt 
        monitor fail if site_dead

frontend  http_80_in
        bind :
        mode http
        log global
        option httplog
        option httpclose
        option forwardfor
        acl web hdr_reg(host) -i www.westos.org
        acl bbs hdr_dom(host) -i bbs.westos.org
        use_backend  server_web  if web
        use_backend  server_bbs if bbs
        default_backend server_web

backend server_web
        mode http
        balance roundrobin
        cookie SERVERID
        option httpchk GET /index.html
        server web1 : cookie web1 check inter  rise  fall  weight 
        server web2 : cookie web2 check inter  rise  fall  weight 

backend server_bbs
        mode http
        balance roundrobin
        cookie SERVERID
        option httpchk GET /index.html
        server bbs1 : cookie bbs1 check inter  rise  fall  weight 
        server bbs2 : cookie bbs2 check inter  rise  fall  weight 
           

測試

web伺服器server2、server3 開啟http服務

真機進行域名解析後

haproxy 實作負載均衡

繼續閱讀